1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
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<VersionInfo>),
(status = 500, description = "Internal error", body = ApiError)
), security())]
#[gnauth(disabled)]
async fn version(ctx: Ctx) -> Json<Envelope<VersionInfo>> {
Json(ctx.envelope(VersionInfo {
name: "GeneNetwork4",
short_name: "GN4AI",
version: 4.0,
release: 1,
date: env!("GN4_COMMIT_DATE"),
git: env!("GN4_GIT_HASH"),
}))
}
|