From 1b01937050da30d97e54cfe0218aa4309bb6ee4d Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Thu, 9 Nov 2023 13:44:04 +0200 Subject: [PATCH] refactor --- catalyst-gateway/bin/src/cli.rs | 7 +++++-- catalyst-gateway/bin/src/logger.rs | 17 ++++++++--------- catalyst-gateway/bin/src/main.rs | 2 +- catalyst-gateway/bin/src/service/api/mod.rs | 2 +- .../src/service/common/responses/resp_5xx.rs | 2 +- catalyst-gateway/bin/src/service/docs/mod.rs | 10 +++------- .../bin/src/service/poem_service.rs | 2 +- .../service/utilities/middleware/tracing_mw.rs | 2 +- .../src/{settings.rs => service_settings.rs} | 6 +++--- 9 files changed, 24 insertions(+), 26 deletions(-) rename catalyst-gateway/bin/src/{settings.rs => service_settings.rs} (98%) diff --git a/catalyst-gateway/bin/src/cli.rs b/catalyst-gateway/bin/src/cli.rs index f63999beb1f..0be3268629f 100644 --- a/catalyst-gateway/bin/src/cli.rs +++ b/catalyst-gateway/bin/src/cli.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use clap::Parser; -use crate::{logger, service, settings::Settings, state::State}; +use crate::{logger, service, service_settings::ServiceSettings, state::State}; #[derive(thiserror::Error, Debug)] /// All service errors @@ -21,7 +21,9 @@ pub(crate) enum Error { /// Simple service CLI options pub(crate) enum Cli { /// Run the service - Run(Settings), + Run(ServiceSettings), + /// Build API docs of the service + Docs, } impl Cli { @@ -46,6 +48,7 @@ impl Cli { service::run(&settings.address, state).await?; Ok(()) }, + Self::Docs => Ok(()), } } } diff --git a/catalyst-gateway/bin/src/logger.rs b/catalyst-gateway/bin/src/logger.rs index 6506a166143..bb5ac8c3053 100644 --- a/catalyst-gateway/bin/src/logger.rs +++ b/catalyst-gateway/bin/src/logger.rs @@ -33,14 +33,13 @@ impl From for tracing::Level { } } -impl LogLevel { - /// Map [`LogLevel`] to [`tracing::Level`] - pub(crate) fn as_log_level(self) -> tracing::log::LevelFilter { - match self { - LogLevel::Info => tracing::log::LevelFilter::Info, - LogLevel::Debug => tracing::log::LevelFilter::Debug, - LogLevel::Warn => tracing::log::LevelFilter::Warn, - LogLevel::Error => tracing::log::LevelFilter::Error, +impl From for tracing::log::LevelFilter { + fn from(val: LogLevel) -> Self { + match val { + LogLevel::Debug => Self::Debug, + LogLevel::Info => Self::Info, + LogLevel::Warn => Self::Warn, + LogLevel::Error => Self::Error, } } } @@ -63,7 +62,7 @@ pub(crate) fn init(log_level: LogLevel) -> Result<(), SetGlobalDefaultError> { .finish(); // Logging is globally disabled by default, so globally enable it to the required level. - tracing::log::set_max_level(log_level.as_log_level()); + tracing::log::set_max_level(log_level.into()); tracing::subscriber::set_global_default(subscriber) } diff --git a/catalyst-gateway/bin/src/main.rs b/catalyst-gateway/bin/src/main.rs index 50e257e9f5c..1a1ebda8735 100644 --- a/catalyst-gateway/bin/src/main.rs +++ b/catalyst-gateway/bin/src/main.rs @@ -6,7 +6,7 @@ mod event_db; mod legacy_service; mod logger; mod service; -mod settings; +mod service_settings; mod state; #[tokio::main] diff --git a/catalyst-gateway/bin/src/service/api/mod.rs b/catalyst-gateway/bin/src/service/api/mod.rs index 6a2828cbb9d..b617284b4f5 100644 --- a/catalyst-gateway/bin/src/service/api/mod.rs +++ b/catalyst-gateway/bin/src/service/api/mod.rs @@ -9,7 +9,7 @@ use test_endpoints::TestApi; use v0::V0Api; use v1::V1Api; -use crate::settings::API_URL_PREFIX; +use crate::service_settings::API_URL_PREFIX; mod health; mod registration; diff --git a/catalyst-gateway/bin/src/service/common/responses/resp_5xx.rs b/catalyst-gateway/bin/src/service/common/responses/resp_5xx.rs index a647d23f461..8883860ece4 100644 --- a/catalyst-gateway/bin/src/service/common/responses/resp_5xx.rs +++ b/catalyst-gateway/bin/src/service/common/responses/resp_5xx.rs @@ -7,7 +7,7 @@ use uuid::Uuid; /// While using macro-vis lib, you will get the `uncommon_codepoints` warning, so you will /// probably want to place this in your crate root -use crate::settings::generate_github_issue_url; +use crate::service_settings::generate_github_issue_url; /// Create a new Server Error Response. /// Logging error message. diff --git a/catalyst-gateway/bin/src/service/docs/mod.rs b/catalyst-gateway/bin/src/service/docs/mod.rs index 39249bffd6b..c6d17e17cec 100644 --- a/catalyst-gateway/bin/src/service/docs/mod.rs +++ b/catalyst-gateway/bin/src/service/docs/mod.rs @@ -8,13 +8,11 @@ use rust_embed::RustEmbed; pub(crate) fn docs( api_service: &OpenApiService, ) -> Route { - let spec = api_service.spec(); - let swagger_ui = api_service.swagger_ui(); let rapidoc_ui = api_service.rapidoc(); let redoc_ui = api_service.redoc(); let openapi_explorer = api_service.openapi_explorer(); - let stoplight_ui = stoplight_elements::create_endpoint(&spec); + let stoplight_ui = stoplight_elements::create_endpoint(&api_service.spec()); Route::new() .at("/", get(stoplight_ui)) @@ -22,10 +20,8 @@ pub(crate) fn docs( .nest("/redoc", redoc_ui) .nest("/rapidoc", rapidoc_ui) .nest("/openapi_explorer", openapi_explorer) - .at( - "/cat-gateway.json", - poem::endpoint::make_sync(move |_| spec.clone()), - ) + .at("/cat-gateway.json", api_service.spec_endpoint()) + .at("/cat-gateway.yml", api_service.spec_endpoint_yaml()) } /// Embed static files. diff --git a/catalyst-gateway/bin/src/service/poem_service.rs b/catalyst-gateway/bin/src/service/poem_service.rs index a60294a11aa..523fd227fed 100644 --- a/catalyst-gateway/bin/src/service/poem_service.rs +++ b/catalyst-gateway/bin/src/service/poem_service.rs @@ -25,7 +25,7 @@ use crate::{ }, Error, }, - settings::{get_api_host_names, API_URL_PREFIX}, + service_settings::{get_api_host_names, API_URL_PREFIX}, state::State, }; diff --git a/catalyst-gateway/bin/src/service/utilities/middleware/tracing_mw.rs b/catalyst-gateway/bin/src/service/utilities/middleware/tracing_mw.rs index cfd2b0e5b5b..c5dbb3449f0 100644 --- a/catalyst-gateway/bin/src/service/utilities/middleware/tracing_mw.rs +++ b/catalyst-gateway/bin/src/service/utilities/middleware/tracing_mw.rs @@ -18,7 +18,7 @@ use tracing::{error, field, Instrument, Level, Span}; use ulid::Ulid; use uuid::Uuid; -use crate::settings::CLIENT_ID_KEY; +use crate::service_settings::CLIENT_ID_KEY; /// Labels for the metrics const METRIC_LABELS: [&str; 3] = ["endpoint", "method", "status_code"]; diff --git a/catalyst-gateway/bin/src/settings.rs b/catalyst-gateway/bin/src/service_settings.rs similarity index 98% rename from catalyst-gateway/bin/src/settings.rs rename to catalyst-gateway/bin/src/service_settings.rs index 388e6a1e9f6..a5c5bbe5ade 100644 --- a/catalyst-gateway/bin/src/settings.rs +++ b/catalyst-gateway/bin/src/service_settings.rs @@ -41,7 +41,7 @@ const API_URL_PREFIX_DEFAULT: &str = "/api"; /// the URL to the `PostgreSQL` event database, /// and the logging level. #[derive(Args, Clone)] -pub(crate) struct Settings { +pub(crate) struct ServiceSettings { /// Server binding address #[clap(long, default_value = ADDRESS_DEFAULT)] pub(crate) address: SocketAddr, @@ -256,10 +256,10 @@ mod tests { // } #[test] - fn generate_github_issue_url() { + fn generate_github_issue_url_test() { let title = "Hello, World! How are you?"; assert_eq!( - super::generate_github_issue_url(title).unwrap().as_str(), + generate_github_issue_url(title).unwrap().as_str(), "https://github.com/input-output-hk/catalyst-core/issues/new?template=bug_report.md&title=Hello%2C+World%21+How+are+you%3F" ); }