Skip to content

Commit 7d1d977

Browse files
committed
some api doc cleanup
1 parent 1506d44 commit 7d1d977

16 files changed

Lines changed: 173 additions & 106 deletions

File tree

src/config.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
sync::Arc,
1111
};
1212
use tracing_subscriber::fmt::writer::MakeWriterExt;
13+
use utoipa::ToSchema;
1314

1415
fn tls_cert() -> String {
1516
"cert.pem".to_string()
@@ -93,7 +94,7 @@ fn docker_log_config_config() -> std::collections::BTreeMap<String, String> {
9394
])
9495
}
9596

96-
#[derive(Deserialize, Serialize, DefaultFromSerde)]
97+
#[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)]
9798
pub struct Tls {
9899
#[serde(default)]
99100
pub enabled: bool,
@@ -104,7 +105,7 @@ pub struct Tls {
104105
}
105106

106107
nestify::nest! {
107-
#[derive(Deserialize, Serialize, DefaultFromSerde)]
108+
#[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)]
108109
pub struct InnerConfig {
109110
#[serde(default)]
110111
pub debug: bool,
@@ -125,60 +126,70 @@ nestify::nest! {
125126
pub disk_check_concurrency: usize,
126127

127128
#[serde(default)]
128-
pub postgres: #[derive(Deserialize, Serialize, DefaultFromSerde)] pub struct Postgres {
129+
#[schema(inline)]
130+
pub postgres: #[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)] pub struct Postgres {
129131
#[serde(default = "postgres_enabled")]
130132
pub enabled: bool,
131133
#[serde(default = "postgres_bind")]
134+
#[schema(value_type = String)]
132135
pub bind: SocketAddr,
133136
#[serde(default)]
134137
pub tls: Tls,
135138
},
136139

137140
#[serde(default)]
138-
pub mariadb: #[derive(Deserialize, Serialize, DefaultFromSerde)] pub struct Mariadb {
141+
#[schema(inline)]
142+
pub mariadb: #[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)] pub struct Mariadb {
139143
#[serde(default = "mariadb_enabled")]
140144
pub enabled: bool,
141145
#[serde(default = "mariadb_bind")]
146+
#[schema(value_type = String)]
142147
pub bind: SocketAddr,
143148
#[serde(default)]
144149
pub tls: Tls,
145150
},
146151

147152
#[serde(default)]
148-
pub mongodb: #[derive(Deserialize, Serialize, DefaultFromSerde)] pub struct Mongodb {
153+
#[schema(inline)]
154+
pub mongodb: #[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)] pub struct Mongodb {
149155
#[serde(default = "mongodb_enabled")]
150156
pub enabled: bool,
151157
#[serde(default = "mongodb_bind")]
158+
#[schema(value_type = String)]
152159
pub bind: SocketAddr,
153160
#[serde(default)]
154161
pub tls: Tls,
155162
},
156163

157164
#[serde(default)]
158-
pub redis: #[derive(Deserialize, Serialize, DefaultFromSerde)] pub struct Redis {
165+
#[schema(inline)]
166+
pub redis: #[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)] pub struct Redis {
159167
#[serde(default = "redis_enabled")]
160168
pub enabled: bool,
161169
#[serde(default = "redis_bind")]
170+
#[schema(value_type = String)]
162171
pub bind: SocketAddr,
163172
#[serde(default)]
164173
pub tls: Tls,
165174
},
166175

167176
#[serde(default)]
168-
pub database: #[derive(Deserialize, Serialize, DefaultFromSerde)] pub struct DatabaseConfig {
177+
#[schema(inline)]
178+
pub database: #[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)] pub struct DatabaseConfig {
169179
#[serde(default = "database_url")]
170180
pub url: String,
171181
#[serde(default = "database_migrate")]
172182
pub migrate: bool,
173183
},
174184

175185
#[serde(default)]
176-
pub docker: #[derive(Deserialize, Serialize, DefaultFromSerde)] pub struct Docker {
186+
#[schema(inline)]
187+
pub docker: #[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)] pub struct Docker {
177188
#[serde(default = "docker_socket")]
178189
pub socket: String,
179190

180191
#[serde(default)]
181-
pub registries: std::collections::HashMap<String, #[derive(Deserialize, Serialize, Clone)] pub struct DockerRegistry {
192+
pub registries: std::collections::HashMap<String, #[derive(ToSchema, Deserialize, Serialize, Clone)] pub struct DockerRegistry {
182193
pub username: String,
183194
pub password: String,
184195
}>,
@@ -193,7 +204,8 @@ nestify::nest! {
193204
pub userns_mode: String,
194205

195206
#[serde(default)]
196-
pub log_config: #[derive(Deserialize, Serialize, DefaultFromSerde)] pub struct DockerLogConfig {
207+
#[schema(inline)]
208+
pub log_config: #[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)] pub struct DockerLogConfig {
197209
#[serde(default = "docker_log_config_type")]
198210
pub r#type: String,
199211
#[serde(default = "docker_log_config_config")]
@@ -202,7 +214,8 @@ nestify::nest! {
202214
},
203215

204216
#[serde(default)]
205-
pub api: #[derive(Deserialize, Serialize, DefaultFromSerde)] pub struct Api {
217+
#[schema(inline)]
218+
pub api: #[derive(ToSchema, Deserialize, Serialize, DefaultFromSerde)] pub struct Api {
206219
#[serde(default = "api_bind")]
207220
pub bind: String,
208221
#[serde(default)]
@@ -216,6 +229,7 @@ nestify::nest! {
216229
pub tls: Tls,
217230

218231
#[serde(default)]
232+
#[schema(value_type = Vec<String>)]
219233
pub trusted_proxies: Vec<cidr::IpCidr>,
220234
},
221235
}

src/routes/api/databases/_database_/export.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use utoipa_axum::{router::OpenApiRouter, routes};
44
mod get {
55
use crate::{
66
response::{ApiResponse, ApiResponseResult},
7-
routes::api::databases::_database_::GetDatabase,
7+
routes::{ApiError, api::databases::_database_::GetDatabase},
88
subsystems::database::identifier::DbIdentifier,
99
};
1010
use axum::{extract::Query, http::StatusCode};
@@ -17,7 +17,8 @@ mod get {
1717
}
1818

1919
#[utoipa::path(get, path = "/", responses(
20-
(status = OK, body = String, content_type = "application/octet-stream"),
20+
(status = OK, body = String),
21+
(status = NOT_FOUND, body = ApiError),
2122
), params(
2223
("database" = uuid::Uuid, description = "The database uuid"),
2324
(

src/routes/api/databases/_database_/import.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use utoipa_axum::{router::OpenApiRouter, routes};
44
mod post {
55
use crate::{
66
response::{ApiResponse, ApiResponseResult},
7-
routes::api::databases::_database_::GetDatabase,
7+
routes::{ApiError, api::databases::_database_::GetDatabase},
88
subsystems::database::identifier::DbIdentifier,
99
};
1010
use axum::{extract::Query, http::StatusCode};
@@ -22,13 +22,14 @@ mod post {
2222

2323
#[utoipa::path(post, path = "/", responses(
2424
(status = OK, body = inline(Response)),
25+
(status = NOT_FOUND, body = ApiError),
2526
), params(
2627
("database" = uuid::Uuid, description = "The database uuid"),
2728
(
2829
"db" = Option<String>, Query,
2930
description = "The db to import into, the dump decides if omitted",
3031
),
31-
), request_body(content = String, content_type = "application/octet-stream"))]
32+
), request_body = String)]
3233
pub async fn route(
3334
database: GetDatabase,
3435
Query(params): Query<Params>,

src/routes/api/databases/_database_/logs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use utoipa_axum::{router::OpenApiRouter, routes};
44
mod get {
55
use crate::{
66
response::{ApiResponse, ApiResponseResult},
7-
routes::api::databases::_database_::GetDatabase,
7+
routes::{ApiError, api::databases::_database_::GetDatabase},
88
};
99
use axum::extract::Query;
1010
use serde::Deserialize;
@@ -17,6 +17,7 @@ mod get {
1717

1818
#[utoipa::path(get, path = "/", responses(
1919
(status = OK, body = String),
20+
(status = NOT_FOUND, body = ApiError),
2021
), params(
2122
("database" = uuid::Uuid, description = "The database uuid"),
2223
(

src/routes/api/databases/_database_/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub async fn auth(
5656
mod get {
5757
use crate::{
5858
response::{ApiResponse, ApiResponseResult},
59-
routes::api::databases::_database_::GetDatabase,
59+
routes::{ApiError, api::databases::_database_::GetDatabase},
6060
};
6161
use serde::Serialize;
6262
use utoipa::ToSchema;
@@ -68,6 +68,7 @@ mod get {
6868

6969
#[utoipa::path(get, path = "/", responses(
7070
(status = OK, body = inline(Response)),
71+
(status = NOT_FOUND, body = ApiError),
7172
), params(
7273
("database" = uuid::Uuid, description = "The database uuid"),
7374
))]
@@ -83,7 +84,7 @@ mod patch {
8384
use crate::{
8485
database::data::StoredDatabaseUpdate,
8586
response::{ApiResponse, ApiResponseResult},
86-
routes::api::databases::_database_::GetDatabase,
87+
routes::{ApiError, api::databases::_database_::GetDatabase},
8788
};
8889
use serde::Serialize;
8990
use utoipa::ToSchema;
@@ -93,6 +94,7 @@ mod patch {
9394

9495
#[utoipa::path(patch, path = "/", responses(
9596
(status = OK, body = inline(Response)),
97+
(status = NOT_FOUND, body = ApiError),
9698
), params(
9799
("database" = uuid::Uuid, description = "The database uuid"),
98100
), request_body = inline(StoredDatabaseUpdate))]
@@ -125,7 +127,7 @@ mod patch {
125127
mod delete {
126128
use crate::{
127129
response::{ApiResponse, ApiResponseResult},
128-
routes::{GetState, api::databases::_database_::GetDatabase},
130+
routes::{ApiError, GetState, api::databases::_database_::GetDatabase},
129131
};
130132
use serde::Serialize;
131133
use utoipa::ToSchema;
@@ -135,6 +137,7 @@ mod delete {
135137

136138
#[utoipa::path(delete, path = "/", responses(
137139
(status = OK, body = inline(Response)),
140+
(status = NOT_FOUND, body = ApiError),
138141
), params(
139142
("database" = uuid::Uuid, description = "The database uuid"),
140143
))]

src/routes/api/databases/_database_/power.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use utoipa_axum::{router::OpenApiRouter, routes};
44
mod post {
55
use crate::{
66
response::{ApiResponse, ApiResponseResult},
7-
routes::api::databases::_database_::GetDatabase,
7+
routes::{ApiError, api::databases::_database_::GetDatabase},
88
};
99
use axum::http::StatusCode;
1010
use serde::{Deserialize, Serialize};
@@ -28,36 +28,41 @@ mod post {
2828
struct Response {}
2929

3030
#[utoipa::path(post, path = "/", responses(
31-
(status = ACCEPTED, body = inline(Response)),
31+
(status = OK, body = inline(Response)),
32+
(status = EXPECTATION_FAILED, body = ApiError),
33+
(status = NOT_FOUND, body = ApiError),
3234
), params(
3335
("database" = uuid::Uuid, description = "The database uuid"),
3436
), request_body = inline(Payload))]
3537
pub async fn route(
3638
database: GetDatabase,
3739
crate::Payload(data): crate::Payload<Payload>,
3840
) -> ApiResponseResult {
39-
tokio::spawn(async move {
40-
let result = match data.action {
41-
PowerAction::Start => database.start().await,
42-
PowerAction::Stop => database.stop().await,
43-
PowerAction::Kill => database.kill().await,
44-
PowerAction::Restart => match database.stop().await {
45-
Ok(()) => database.start().await,
46-
Err(err) => Err(err),
47-
},
48-
};
41+
let result = match data.action {
42+
PowerAction::Start => database.start().await,
43+
PowerAction::Stop => database.stop().await,
44+
PowerAction::Kill => database.kill().await,
45+
PowerAction::Restart => match database.stop().await {
46+
Ok(()) => database.start().await,
47+
Err(err) => Err(err),
48+
},
49+
};
4950

50-
if let Err(err) = result {
51-
tracing::error!(
52-
"failed to run power action on database {}: {err}",
53-
database.uuid
54-
);
55-
}
56-
});
51+
if let Err(err) = result {
52+
tracing::error!(
53+
"failed to run power action on database {}: {err}",
54+
database.uuid
55+
);
5756

58-
ApiResponse::new_serialized(Response {})
59-
.with_status(StatusCode::ACCEPTED)
60-
.ok()
57+
return ApiResponse::error(&format!(
58+
"failed to run power action on database {}: {err}",
59+
database.uuid
60+
))
61+
.with_status(StatusCode::EXPECTATION_FAILED)
62+
.ok();
63+
}
64+
65+
ApiResponse::new_serialized(Response {}).ok()
6166
}
6267
}
6368

src/routes/api/databases/_database_/query.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use utoipa_axum::{router::OpenApiRouter, routes};
44
mod post {
55
use crate::{
66
response::{ApiResponse, ApiResponseResult},
7-
routes::api::databases::_database_::GetDatabase,
7+
routes::{ApiError, api::databases::_database_::GetDatabase},
88
subsystems::database::{connection::QueryResult, identifier::DbIdentifier},
99
};
1010
use axum::http::StatusCode;
@@ -24,6 +24,7 @@ mod post {
2424

2525
#[utoipa::path(post, path = "/", responses(
2626
(status = OK, body = inline(Response)),
27+
(status = NOT_FOUND, body = ApiError),
2728
), params(
2829
("database" = uuid::Uuid, description = "The database uuid"),
2930
), request_body = inline(Payload))]

src/routes/api/databases/_database_/users/_user_/mod.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,38 @@ pub async fn auth(
5151
Ok(next.run(req).await)
5252
}
5353

54+
mod get {
55+
use crate::{
56+
response::{ApiResponse, ApiResponseResult},
57+
routes::{ApiError, api::databases::_database_::users::_user_::GetUser},
58+
};
59+
use serde::Serialize;
60+
use utoipa::ToSchema;
61+
62+
#[derive(ToSchema, Serialize)]
63+
struct Response {
64+
user: crate::database::data::StoredDatabaseUser,
65+
}
66+
67+
#[utoipa::path(get, path = "/", responses(
68+
(status = OK, body = inline(Response)),
69+
(status = NOT_FOUND, body = ApiError),
70+
), params(
71+
("database" = uuid::Uuid, description = "The database uuid"),
72+
("user" = uuid::Uuid, description = "The database user uuid"),
73+
))]
74+
pub async fn route(user: GetUser) -> ApiResponseResult {
75+
ApiResponse::new_serialized(Response { user: user.0 }).ok()
76+
}
77+
}
78+
5479
mod delete {
5580
use crate::{
5681
response::{ApiResponse, ApiResponseResult},
57-
routes::api::databases::_database_::{GetDatabase, users::_user_::GetUser},
82+
routes::{
83+
ApiError,
84+
api::databases::_database_::{GetDatabase, users::_user_::GetUser},
85+
},
5886
};
5987
use serde::Serialize;
6088
use utoipa::ToSchema;
@@ -64,6 +92,7 @@ mod delete {
6492

6593
#[utoipa::path(delete, path = "/", responses(
6694
(status = OK, body = inline(Response)),
95+
(status = NOT_FOUND, body = ApiError),
6796
), params(
6897
("database" = uuid::Uuid, description = "The database uuid"),
6998
("user" = uuid::Uuid, description = "The database user uuid"),
@@ -78,6 +107,7 @@ mod delete {
78107
pub fn router(state: &State) -> OpenApiRouter<State> {
79108
OpenApiRouter::new()
80109
.nest("/rotate-password", rotate_password::router(state))
110+
.routes(routes!(get::route))
81111
.routes(routes!(delete::route))
82112
.route_layer(axum::middleware::from_fn(auth))
83113
.with_state(state.clone())

0 commit comments

Comments
 (0)