feat: implement centralized logging and authentication middleware

Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-13 13:03:13 -03:00
parent 1170c5ee47
commit 50150ce23d
4 changed files with 21 additions and 91 deletions

View File

@@ -16,7 +16,7 @@ mod common;
mod api;
mod pages;
pub use common::{ServerConfig, AppState, logging_middleware};
pub use common::{ServerConfig, AppState, logging_middleware, create_auth_middleware};
pub fn mode_server(
_cmd: &mut Command,
@@ -55,21 +55,21 @@ async fn run_server(
};
let app = Router::new()
// Add API, documentation, and pages routes first
.merge(api::add_routes(Router::new()))
.merge(api::add_docs_routes(Router::new()))
.merge(pages::add_routes(Router::new()))
// Apply state
.with_state(state)
// Add middleware layers (applied in reverse order)
.layer(axum::middleware::from_fn(logging_middleware))
.layer(axum::middleware::from_fn(create_auth_middleware(config.password.clone())))
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(CorsLayer::permissive())
);
// Add API, documentation, and pages routes
let app = api::add_routes(app);
let app = api::add_docs_routes(app);
let app = pages::add_routes(app);
// Apply state to the router after all routes are added
let app = app.with_state(state);
let addr: SocketAddr = if config.address.starts_with('/') || config.address.starts_with("./") {
// Unix socket - not supported by axum directly, fall back to TCP
warn!("Unix sockets not yet implemented, falling back to TCP on 127.0.0.1:8080");