From 38be4745b2647276237f5c761abcd89b97df7679 Mon Sep 17 00:00:00 2001 From: Magic Len Date: Wed, 15 Nov 2023 10:11:11 +0800 Subject: [PATCH 1/2] remove unused code --- src/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.rs b/src/server.rs index 432b218..3a4498f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -131,7 +131,7 @@ async fn run(socket_file_path: &Path) -> anyhow::Result { uds }; - let app: Router = Router::new() + let app = Router::new() .route("/", any(index_handler)) .layer(SetResponseHeaderLayer::overriding( header::CACHE_CONTROL, From ff3112ea6ffb23801c7c039a43b824bc264d8a2f Mon Sep 17 00:00:00 2001 From: Magic Len Date: Wed, 15 Nov 2023 10:14:58 +0800 Subject: [PATCH 2/2] refactor --- src/server.rs | 59 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/server.rs b/src/server.rs index 3a4498f..23ab395 100644 --- a/src/server.rs +++ b/src/server.rs @@ -101,7 +101,35 @@ async fn index_handler( } } -async fn run(socket_file_path: &Path) -> anyhow::Result { +fn create_app() -> Router { + Router::new() + .route("/", any(index_handler)) + .layer(SetResponseHeaderLayer::overriding( + header::CACHE_CONTROL, + HeaderValue::from_static("no-store"), + )) + .layer( + TraceLayer::new_for_http() + .make_span_with(DefaultMakeSpan::new().level(Level::INFO)) + .on_request(DefaultOnRequest::new().level(Level::INFO)) + .on_response(DefaultOnResponse::new().level(Level::INFO)), + ) +} + +pub async fn server_main(socket_file_path: &Path) -> anyhow::Result { + let mut ansi_color = io::stdout().is_terminal(); + + if ansi_color && enable_ansi_support::enable_ansi_support().is_err() { + ansi_color = false; + } + + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer().with_ansi(ansi_color)) + .with(EnvFilter::builder().with_default_directive(Level::INFO.into()).from_env_lossy()) + .init(); + + let app = create_app(); + let uds = { match fs::metadata(socket_file_path).await { Ok(metadata) => { @@ -131,19 +159,6 @@ async fn run(socket_file_path: &Path) -> anyhow::Result { uds }; - let app = Router::new() - .route("/", any(index_handler)) - .layer(SetResponseHeaderLayer::overriding( - header::CACHE_CONTROL, - HeaderValue::from_static("no-store"), - )) - .layer( - TraceLayer::new_for_http() - .make_span_with(DefaultMakeSpan::new().level(Level::INFO)) - .on_request(DefaultOnRequest::new().level(Level::INFO)) - .on_response(DefaultOnResponse::new().level(Level::INFO)), - ); - tracing::info!("listening on {socket_file_path:?}"); axum::Server::builder(ServerAccept { @@ -159,19 +174,3 @@ async fn run(socket_file_path: &Path) -> anyhow::Result { Ok(AppResult::Ok) } - -#[inline] -pub async fn server_main(socket_file_path: &Path) -> anyhow::Result { - let mut ansi_color = io::stdout().is_terminal(); - - if ansi_color && enable_ansi_support::enable_ansi_support().is_err() { - ansi_color = false; - } - - tracing_subscriber::registry() - .with(tracing_subscriber::fmt::layer().with_ansi(ansi_color)) - .with(EnvFilter::builder().with_default_directive(Level::INFO.into()).from_env_lossy()) - .init(); - - run(socket_file_path).await -}