use axum::Json; use axum::Router; use tower_http::request_id::{MakeRequestUuid, PropagateRequestIdLayer, RequestId, SetRequestIdLayer}; use utoipa::OpenApi; use utoipa_axum::router::OpenApiRouter; use utoipa_axum::routes; pub mod config; mod db; mod error; pub mod response; mod routes; use error::ApiError; #[derive(utoipa::OpenApi)] #[openapi(info( title = "GeneNetwork4 API", description = "GeneNetwork4 REST API", license(name = "AGPL-3.0-or-later") ))] struct ApiDoc; /// Build the application router. Public so integration tests (tests/) and /// the binary (src/main.rs) can both use it without binding a real socket. pub fn app() -> Router { let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi()) .routes(routes!(hello_world)) .routes(routes!(version)) .routes(routes!(routes::species::species)) .routes(routes!(routes::species::species_by_name)) .routes(routes!(routes::groups::groups)) .routes(routes!(routes::groups::group_by_name)) .routes(routes!(routes::groups::group_strains)) .routes(routes!(routes::correlation::correlation)) .split_for_parts(); router .route("/openapi.json", axum::routing::get(|| async { Json(api) })) // Assigns each request an x-request-id (if the client didn't // already send one) and copies it onto the response, so it can be // included in ApiError bodies (see error.rs) and correlated // with logs. .layer(PropagateRequestIdLayer::x_request_id()) .layer(SetRequestIdLayer::x_request_id(MakeRequestUuid)) } fn request_id_string(request_id: &RequestId) -> Option { request_id.header_value().to_str().ok().map(str::to_string) } // hello_world and version predate the domain-module split and don't // belong to a data domain; kept here as top-level utility routes. #[utoipa::path(get, path = "/", summary = "Greeting; confirms the server is up", responses( (status = 200, description = "Greeting", body = String), (status = 500, description = "Internal error", body = ApiError) ))] async fn hello_world() -> &'static str { "Hello, world!" } #[derive(serde::Serialize, utoipa::ToSchema)] struct VersionInfo { name: &'static str, short_name: &'static str, version: f64, release: u32, date: &'static str, git: &'static str, } #[utoipa::path(get, path = "/version", summary = "Build information: version, git commit, commit date", responses( (status = 200, description = "Build information", body = VersionInfo), (status = 500, description = "Internal error", body = ApiError) ))] async fn version() -> Json { Json(VersionInfo { name: "GeneNetwork4", short_name: "GN4AI", version: 4.0, release: 1, date: env!("GN4_COMMIT_DATE"), git: env!("GN4_GIT_HASH"), }) }