Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Nov 15, 2023
1 parent 38be474 commit ff3112e
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,35 @@ async fn index_handler(
}
}

async fn run(socket_file_path: &Path) -> anyhow::Result<AppResult> {
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<AppResult> {
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) => {
Expand Down Expand Up @@ -131,19 +159,6 @@ async fn run(socket_file_path: &Path) -> anyhow::Result<AppResult> {
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 {
Expand All @@ -159,19 +174,3 @@ async fn run(socket_file_path: &Path) -> anyhow::Result<AppResult> {

Ok(AppResult::Ok)
}

#[inline]
pub async fn server_main(socket_file_path: &Path) -> anyhow::Result<AppResult> {
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
}

0 comments on commit ff3112e

Please sign in to comment.