use axum::{
Router,
http::{HeaderValue, StatusCode, header},
response::IntoResponse,
routing::get,
};
use tower_http::set_header::SetResponseHeaderLayer;
use crate::StartedServices;
pub fn routes_with_services<S>(started_services: StartedServices, version_str: String) -> Router<S>
where
S: Clone + Send + Sync + 'static,
{
Router::new()
.route("/health", get(move || async { health(started_services) }))
.route("/version", get(move || async { version(version_str) }))
.layer(SetResponseHeaderLayer::overriding(
header::CACHE_CONTROL,
HeaderValue::from_static("no-cache"),
))
}
pub fn routes<S>(version_str: String) -> Router<S>
where
S: Clone + Send + Sync + 'static,
{
Router::new()
.route(
"/health",
get(|| async move { (StatusCode::OK, "healthy") }),
)
.route("/version", get(move || async { version(version_str) }))
.layer(SetResponseHeaderLayer::overriding(
header::CACHE_CONTROL,
HeaderValue::from_static("no-cache"),
))
}
fn health(started_services: StartedServices) -> impl IntoResponse {
if started_services.all_started() {
(StatusCode::OK, "healthy")
} else {
(StatusCode::SERVICE_UNAVAILABLE, "starting")
}
}
fn version(version_str: String) -> impl IntoResponse {
(StatusCode::OK, version_str)
}