diff --git a/stats/Cargo.lock b/stats/Cargo.lock index fde86d4c8..26f0f5457 100644 --- a/stats/Cargo.lock +++ b/stats/Cargo.lock @@ -34,6 +34,29 @@ dependencies = [ "smallvec", ] +[[package]] +name = "actix-files" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0773d59061dedb49a8aed04c67291b9d8cf2fe0b60130a381aab53c6dd86e9be" +dependencies = [ + "actix-http", + "actix-service", + "actix-utils", + "actix-web", + "bitflags 2.5.0", + "bytes", + "derive_more", + "futures-core", + "http-range", + "log", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", + "v_htmlescape", +] + [[package]] name = "actix-http" version = "3.7.0" @@ -1774,6 +1797,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" + [[package]] name = "httparse" version = "1.8.0" @@ -2246,6 +2275,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -4130,6 +4169,7 @@ dependencies = [ name = "stats-server" version = "0.1.0" dependencies = [ + "actix-files", "actix-web", "anyhow", "async-trait", @@ -4144,6 +4184,7 @@ dependencies = [ "liquid-json", "paste", "pretty_assertions", + "reqwest", "sea-orm", "serde", "serde_json", @@ -4697,6 +4738,15 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.15" @@ -4781,6 +4831,12 @@ dependencies = [ "serde", ] +[[package]] +name = "v_htmlescape" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e8257fbc510f0a46eb602c10215901938b5c2a7d5e70fc11483b1d3c9b5b18c" + [[package]] name = "valuable" version = "0.1.0" diff --git a/stats/stats-server/Cargo.toml b/stats/stats-server/Cargo.toml index 6d393e2ae..2a2a312c8 100644 --- a/stats/stats-server/Cargo.toml +++ b/stats/stats-server/Cargo.toml @@ -8,6 +8,7 @@ stats = { path = "../stats" } stats-proto = { path = "../stats-proto" } async-trait = "0.1" actix-web = "4" +actix-files = "0.6.6" tonic = "0.8" serde = { version = "1", features = ["derive"] } serde_with = { version = "2.0", features = ["hex", "base64"] } @@ -36,3 +37,4 @@ paste = "1.0" stats = { path = "../stats", features = ["test-utils"] } blockscout-service-launcher = { workspace = true, features = [ "database-0_12", "test-server" ] } pretty_assertions = "1.3" +reqwest = "0.11" diff --git a/stats/stats-server/src/server.rs b/stats/stats-server/src/server.rs index 150dc6ec5..ad1b3cda0 100644 --- a/stats/stats-server/src/server.rs +++ b/stats/stats-server/src/server.rs @@ -27,11 +27,22 @@ struct HttpRouter { health: Arc, } +async fn serve_swagger(_req: actix_web::HttpRequest) -> actix_web::Result { + let path = std::path::PathBuf::from("../stats-proto/swagger/stats.swagger.yaml"); + Ok(actix_files::NamedFile::open(path)?) +} + impl launcher::HttpRouter for HttpRouter { fn register_routes(&self, service_config: &mut actix_web::web::ServiceConfig) { service_config .configure(|config| route_health(config, self.health.clone())) .configure(|config| route_stats_service(config, self.stats.clone())); + service_config.configure(|config| { + config.route( + "/api/v1/docs/swagger.yaml", + actix_web::web::get().to(serve_swagger), + ); + }); } } diff --git a/stats/stats-server/tests/swagger.rs b/stats/stats-server/tests/swagger.rs new file mode 100644 index 000000000..ca118e971 --- /dev/null +++ b/stats/stats-server/tests/swagger.rs @@ -0,0 +1,42 @@ +use blockscout_service_launcher::{ + launcher::ConfigSettings, + test_server::{get_test_server_settings, init_server, send_get_request}, +}; +use chrono::NaiveDate; +use stats::tests::{init_db::init_db_all, mock_blockscout::fill_mock_blockscout_data}; +use stats_proto::blockscout::stats::v1::Counters; +use stats_server::{stats, Settings}; +use std::{collections::HashSet, path::PathBuf, str::FromStr}; + +#[tokio::test] +#[ignore = "needs database"] +async fn test_swagger_ok() { + let (stats_db, blockscout_db) = init_db_all("test_swagger_ok").await; + std::env::set_var("STATS__CONFIG", "./tests/config/test.toml"); + let mut settings = Settings::build().expect("Failed to build settings"); + let (server_settings, base) = get_test_server_settings(); + settings.server = server_settings; + settings.charts_config = PathBuf::from_str("../config/charts.json").unwrap(); + settings.layout_config = PathBuf::from_str("../config/layout.json").unwrap(); + settings.update_groups_config = PathBuf::from_str("../config/update_groups.json").unwrap(); + settings.db_url = stats_db.db_url(); + settings.blockscout_db_url = blockscout_db.db_url(); + + init_server(|| stats(settings), &base).await; + + // Sleep until server will start and calculate all values + tokio::time::sleep(std::time::Duration::from_secs(5)).await; + + let request = reqwest::Client::new().request( + reqwest::Method::GET, + base.join("/api/v1/docs/swagger.yaml").unwrap(), + ); + let response = request + .send() + .await + .unwrap_or_else(|_| panic!("Failed to send request")); + + println!("{:?}", response); + println!("{}", response.text().await.unwrap()); + panic!(); +}