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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
use axum::extract::{Extension, Path};
use axum::http::StatusCode;
use axum::{Json, Router};
use serde::Serialize;
use tower_http::request_id::{MakeRequestUuid, PropagateRequestIdLayer, RequestId, SetRequestIdLayer};
use utoipa::OpenApi;
use utoipa_axum::router::OpenApiRouter;
use utoipa_axum::routes;
mod db;
mod error;
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!(species))
.routes(routes!(species_by_name))
.routes(routes!(groups))
.routes(routes!(group_by_name))
.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<String> {
request_id.header_value().to_str().ok().map(str::to_string)
}
#[utoipa::path(get, path = "/", responses(
(status = 200, description = "Greeting", body = String),
(status = 500, description = "Internal error", body = ApiError)
))]
async fn hello_world() -> &'static str {
"Hello, world!"
}
#[derive(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", responses(
(status = 200, description = "Build information", body = VersionInfo),
(status = 500, description = "Internal error", body = ApiError)
))]
async fn version() -> Json<VersionInfo> {
Json(VersionInfo {
name: "GeneNetwork4",
short_name: "GN4AI",
version: 4.0,
release: 1,
date: env!("GN4_COMMIT_DATE"),
git: env!("GN4_GIT_HASH"),
})
}
#[utoipa::path(get, path = "/species", responses(
(status = 200, description = "All species in the database", body = Vec<db::Species>),
(status = 500, description = "Database error", body = ApiError)
))]
async fn species(Extension(request_id): Extension<RequestId>) -> Result<Json<Vec<db::Species>>, ApiError> {
let rid = request_id_string(&request_id);
// mysql's Conn is blocking; run it on a blocking thread so it doesn't
// stall the async runtime.
tokio::task::spawn_blocking(db::get_all_species)
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"species",
)
.with_request_id(rid.clone())
})?
.map(Json)
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"species",
)
.with_request_id(rid)
})
}
#[utoipa::path(get, path = "/species/{species}", params(
("species" = String, Path, description = "Species Name, FullName or SpeciesName")
), responses(
(status = 200, description = "The matching row of the Species table", body = db::SpeciesDetail),
(status = 404, description = "No matching species", body = ApiError),
(status = 500, description = "Database error", body = ApiError)
))]
async fn species_by_name(
Extension(request_id): Extension<RequestId>,
Path(species): Path<String>,
) -> Result<Json<db::SpeciesDetail>, ApiError> {
let rid = request_id_string(&request_id);
let not_found_name = species.clone();
tokio::task::spawn_blocking(move || db::get_species_by_name(&species))
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"species_by_name",
)
.with_request_id(rid.clone())
})?
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"species_by_name",
)
.with_request_id(rid.clone())
})?
.map(Json)
.ok_or_else(|| {
ApiError::new(
StatusCode::NOT_FOUND,
"species_not_found",
"Species not found",
format!("No species matching '{not_found_name}'."),
"species_by_name",
)
.with_request_id(rid)
.with_next("/species")
})
}
#[utoipa::path(get, path = "/groups", responses(
(status = 200, description = "All groups (InbredSet rows) in the database", body = Vec<db::Group>),
(status = 500, description = "Database error", body = ApiError)
))]
async fn groups(Extension(request_id): Extension<RequestId>) -> Result<Json<Vec<db::Group>>, ApiError> {
let rid = request_id_string(&request_id);
tokio::task::spawn_blocking(db::get_all_groups)
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"groups",
)
.with_request_id(rid.clone())
})?
.map(Json)
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"groups",
)
.with_request_id(rid)
})
}
#[utoipa::path(get, path = "/groups/{group}", params(
("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id")
), responses(
(status = 200, description = "The matching row of the InbredSet table", body = db::GroupDetail),
(status = 404, description = "No matching group", body = ApiError),
(status = 500, description = "Database error", body = ApiError)
))]
async fn group_by_name(
Extension(request_id): Extension<RequestId>,
Path(group): Path<String>,
) -> Result<Json<db::GroupDetail>, ApiError> {
let rid = request_id_string(&request_id);
let not_found_name = group.clone();
tokio::task::spawn_blocking(move || db::get_group_by_name(&group))
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"group_by_name",
)
.with_request_id(rid.clone())
})?
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"group_by_name",
)
.with_request_id(rid.clone())
})?
.map(Json)
.ok_or_else(|| {
ApiError::new(
StatusCode::NOT_FOUND,
"group_not_found",
"Group not found",
format!("No group matching '{not_found_name}'."),
"group_by_name",
)
.with_request_id(rid)
.with_next("/groups")
})
}
|