Skip to content

Commit

Permalink
swagger WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bragov4ik committed Aug 13, 2024
1 parent 9018f04 commit 81f2d6c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
56 changes: 56 additions & 0 deletions stats/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions stats/stats-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down Expand Up @@ -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"
11 changes: 11 additions & 0 deletions stats/stats-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ struct HttpRouter<S: StatsService> {
health: Arc<HealthService>,
}

async fn serve_swagger(_req: actix_web::HttpRequest) -> actix_web::Result<actix_files::NamedFile> {
let path = std::path::PathBuf::from("../stats-proto/swagger/stats.swagger.yaml");
Ok(actix_files::NamedFile::open(path)?)
}

impl<S: StatsService> launcher::HttpRouter for HttpRouter<S> {
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),
);
});
}
}

Expand Down
42 changes: 42 additions & 0 deletions stats/stats-server/tests/swagger.rs
Original file line number Diff line number Diff line change
@@ -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!();
}

0 comments on commit 81f2d6c

Please sign in to comment.