use super::{health, middleware};
use crate::configs::Config;
use axum::{
Router, extract::DefaultBodyLimit, http::StatusCode, middleware as axum_middleware,
response::IntoResponse, routing::get,
};
use tower_http::{catch_panic::CatchPanicLayer, cors::CorsLayer, limit::RequestBodyLimitLayer};
pub fn router(app: Router, config: &Config) -> Router {
let state = middleware::state(config);
let welcome = health::welcome(config);
let app = super::static_files::mount(app, &config.static_files);
let cors = CorsLayer::new()
.allow_origin(config.cors.origins.clone())
.allow_methods(config.cors.methods.clone())
.allow_headers(config.cors.allowed_headers.clone())
.expose_headers(config.cors.exposed_headers.clone())
.allow_credentials(config.cors.allow_credentials)
.max_age(config.cors.max_age);
let app = app.route("/", get(move || { let welcome = welcome.clone(); async move { welcome } }))
.route("/health/live", get(health::check))
.route("/health/ready", get(health::check))
.fallback(|| async { StatusCode::NOT_FOUND })
.layer(DefaultBodyLimit::max(config.body_limit))
.layer(RequestBodyLimitLayer::new(config.body_limit))
.layer(CatchPanicLayer::custom(|_| {
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}))
.layer(axum_middleware::from_fn_with_state(
state,
middleware::guard,
))
.layer(cors);
crate::middleware::configure(xecute::Application::new(app))
.into_router()
.layer(axum_middleware::from_fn(middleware::observe))
}