use axum::Json; use axum::Router; use gnauth_macros::gnauth; use tower_http::request_id::{MakeRequestUuid, PropagateRequestIdLayer, SetRequestIdLayer}; use utoipa::OpenApi; use utoipa_axum::router::OpenApiRouter; use utoipa_axum::routes; pub mod auth; pub mod config; mod db; pub mod embedded; mod error; pub mod indexer; mod gemma; pub mod response; mod routes; pub mod search; mod streaming; mod rqtl; use error::ApiError; use response::{Ctx, Envelope}; #[derive(utoipa::OpenApi)] #[openapi(info( title = "GeneNetwork4 API", description = "GeneNetwork4 REST API", license(name = "AGPL-3.0-or-later") ), security(("bearer_token" = [])), modifiers(&SecurityAddon))] struct ApiDoc; /// Registers the "bearer_token" scheme `ApiDoc`'s own `security(...)` /// (above) requires by default on every operation -- every endpoint /// needs a token unless it opts out with its own empty `security()` /// (`hello_world`, `version`, `routes::dev::get_test_token`: see each). struct SecurityAddon; impl utoipa::Modify for SecurityAddon { fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) { use utoipa::openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme}; if let Some(components) = openapi.components.as_mut() { components.add_security_scheme( "bearer_token", SecurityScheme::Http( HttpBuilder::new() .scheme(HttpAuthScheme::Bearer) .bearer_format("JWT") .build(), ), ); } } } /// 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 mut api_router = OpenApiRouter::with_openapi(ApiDoc::openapi()) .routes(routes!(hello_world)) .routes(routes!(version)) .routes(routes!(routes::accessions::accession)) .routes(routes!(routes::mapping_methods::mapping_methods)) .routes(routes!(routes::species::species)) .routes(routes!(routes::species::species_by_name)) .routes(routes!(routes::species::species_datasets)) .routes(routes!(routes::groups::groups)) .routes(routes!(routes::groups::group_by_name)) .routes(routes!(routes::groups::group_strains)) .routes(routes!(routes::groups::group_trait_values)) .routes(routes!(routes::groups::group_trait_info)) .routes(routes!(routes::groups::group_traits)) .routes(routes!(routes::groups::group_datasets)) .routes(routes!(routes::groups::group_case_attributes)) .routes(routes!(routes::datasets::dataset_by_name)) .routes(routes!(routes::datasets::dataset_probesets)) .routes(routes!(routes::datasets::dataset_probeset_info)) .routes(routes!(routes::datasets::dataset_probeset_values)) .routes(routes!(routes::traits::traits)) .routes(routes!(routes::traits::trait_info)) .routes(routes!(routes::traits::trait_values)) .routes(routes!(routes::publications::publication_traits)) .routes(routes!(routes::search::search)) .routes(routes!(routes::correlation::correlation)) .routes(routes!(routes::gemma::compute_gemma)) .routes(routes!(routes::gemma::compute_gemma_ravanan)) .routes(routes!(routes::gemma::gemma_status)) .routes(routes!(routes::rqtl::compute_rqtl)) .routes(routes!(routes::rqtl::rqtl_status)); // Registered -- and so reachable and listed in /openapi.json -- unless // GENENETWORK4_TEST_TOKEN switches it off; when registered it decides // per request whether the caller is local. See src/routes/dev.rs. if config::Config::load().test_token_enabled { api_router = api_router.routes(routes!(routes::dev::get_test_token)); } let (router, api) = api_router.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)) } // hello_world and version predate the domain-module split and don't // belong to a data domain; kept here as top-level utility routes. // hello_world (a plain-text liveness greeting) is the one handler // whose response is not wrapped in the envelope, like /openapi.json. // Both opt out of ApiDoc's default security requirement with an empty // security() -- neither takes an AuthToken, so documenting them as // needing one would be wrong, not just inconsistent. #[gnauth(disabled)] // below makes that a compile-time assertion, not just this comment. #[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) ), security())] #[gnauth(disabled)] 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 = Envelope), (status = 500, description = "Internal error", body = ApiError) ), security())] #[gnauth(disabled)] async fn version(ctx: Ctx) -> Json> { Json(ctx.envelope(VersionInfo { name: "GeneNetwork4", short_name: "GN4AI", version: 4.0, release: 1, date: env!("GN4_COMMIT_DATE"), git: env!("GN4_GIT_HASH"), })) }