pub mod circuit_breaker;
pub(crate) mod extractors;
pub(crate) mod handlers;
pub(crate) mod middleware;
pub mod openapi;
pub(crate) mod routes;
pub mod state;
use axum::Router;
use state::AppState;
pub fn create_app(app_state: AppState) -> Router {
let request_timeout =
std::time::Duration::from_millis(app_state.config.request_timeout_ms as u64);
let public_routes = Router::new()
.merge(routes::health_routes())
.merge(routes::docs_routes());
let mut protected_routes = Router::new().nest("/v1", routes::api_v1_routes());
if app_state.config.config_endpoint_enabled {
protected_routes = protected_routes.merge(routes::config_routes());
}
let protected_routes = protected_routes.layer(axum::middleware::from_fn_with_state(
app_state.clone(),
middleware::auth::conditional_auth,
));
Router::new()
.merge(public_routes)
.merge(protected_routes)
.layer(axum::middleware::from_fn(
middleware::request_id::add_request_id,
))
.layer(tower_http::timeout::TimeoutLayer::with_status_code(
axum::http::StatusCode::REQUEST_TIMEOUT,
request_timeout,
))
.layer(
tower_http::cors::CorsLayer::new()
.allow_origin(tower_http::cors::Any)
.allow_methods(tower_http::cors::Any)
.allow_headers(tower_http::cors::Any),
)
.layer(tower_http::trace::TraceLayer::new_for_http())
.with_state(app_state)
}
#[cfg(feature = "test-utils")]
pub fn create_test_app(app_state: AppState) -> Router {
let test_timeout = std::time::Duration::from_millis(app_state.config.request_timeout_ms as u64)
+ std::time::Duration::from_secs(90);
let mut public_routes = Router::new().merge(routes::health_routes());
#[cfg(feature = "web-api")]
{
public_routes = public_routes.merge(routes::docs_routes());
}
let mut protected_routes = Router::new().nest("/v1", routes::api_v1_routes());
if app_state.config.config_endpoint_enabled {
protected_routes = protected_routes.merge(routes::config_routes());
}
let protected_routes = protected_routes.layer(axum::middleware::from_fn_with_state(
app_state.clone(),
middleware::auth::conditional_auth,
));
Router::new()
.merge(public_routes)
.merge(protected_routes)
.layer(axum::middleware::from_fn(
middleware::request_id::add_request_id,
))
.layer(tower_http::timeout::TimeoutLayer::with_status_code(
axum::http::StatusCode::REQUEST_TIMEOUT,
test_timeout,
))
.layer(
tower_http::cors::CorsLayer::new()
.allow_origin(tower_http::cors::Any)
.allow_methods(tower_http::cors::Any)
.allow_headers(tower_http::cors::Any),
)
.layer(tower_http::trace::TraceLayer::new_for_http())
.with_state(app_state)
}