Skip to content

Commit 03b0eba

Browse files
authored
Add utoipa Swagger UI support (#4602)
* Add utoipa Swagger UI support * remove unused code * remove unused code * consistency with trailing slash
1 parent 707ff21 commit 03b0eba

File tree

13 files changed

+253
-87
lines changed

13 files changed

+253
-87
lines changed

Cargo.lock

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ tracing-subscriber = "0.3.20"
192192
typed-path = "0.12.0"
193193
url = "2.5.7"
194194
urlencoding = "2.1.3"
195+
utoipa = { version = "5.4.0", features = ["actix_extras", "chrono", "decimal"] }
196+
utoipa-actix-web = { version = "0.1.2" }
197+
utoipa-swagger-ui = { version = "9.0.2", features = ["actix-web", "vendored"] }
195198
uuid = "1.18.1"
196199
validator = "0.20.0"
197200
webp = { version = "0.3.1", default-features = false }

apps/labrinth/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ tracing-ecs = { workspace = true }
120120
tracing-subscriber = { workspace = true }
121121
url = { workspace = true }
122122
urlencoding = { workspace = true }
123+
utoipa = { workspace = true }
124+
utoipa-actix-web = { workspace = true }
125+
utoipa-swagger-ui = { workspace = true }
123126
uuid = { workspace = true, features = ["fast-rng", "serde", "v4"] }
124127
validator = { workspace = true, features = ["derive"] }
125128
webp = { workspace = true }

apps/labrinth/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ pub fn app_config(
345345
.default_service(web::get().wrap(default_cors()).to(routes::not_found));
346346
}
347347

348+
pub fn utoipa_app_config(
349+
cfg: &mut utoipa_actix_web::service_config::ServiceConfig,
350+
_labrinth_config: LabrinthConfig,
351+
) {
352+
cfg.configure(routes::v3::utoipa_config);
353+
}
354+
348355
// This is so that env vars not used immediately don't panic at runtime
349356
pub fn check_env_vars() -> bool {
350357
let mut failed = false;

apps/labrinth/src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use labrinth::util::anrok;
1414
use labrinth::util::env::parse_var;
1515
use labrinth::util::gotenberg::GotenbergClient;
1616
use labrinth::util::ratelimit::rate_limit_middleware;
17+
use labrinth::utoipa_app_config;
1718
use labrinth::{check_env_vars, clickhouse, database, file_hosting};
1819
use std::ffi::CStr;
1920
use std::str::FromStr;
@@ -25,6 +26,9 @@ use tracing_ecs::ECSLayerBuilder;
2526
use tracing_subscriber::EnvFilter;
2627
use tracing_subscriber::layer::SubscriberExt;
2728
use tracing_subscriber::util::SubscriberInitExt;
29+
use utoipa::OpenApi;
30+
use utoipa_actix_web::AppExt;
31+
use utoipa_swagger_ui::SwaggerUi;
2832

2933
#[cfg(target_os = "linux")]
3034
#[global_allocator]
@@ -293,13 +297,23 @@ async fn main() -> std::io::Result<()> {
293297
.wrap(from_fn(rate_limit_middleware))
294298
.wrap(actix_web::middleware::Compress::default())
295299
.wrap(sentry_actix::Sentry::new())
300+
.into_utoipa_app()
301+
.configure(|cfg| utoipa_app_config(cfg, labrinth_config.clone()))
302+
.openapi_service(|api| SwaggerUi::new("/docs/swagger-ui/{_:.*}")
303+
.config(utoipa_swagger_ui::Config::default().try_it_out_enabled(true))
304+
.url("/docs/openapi.json", ApiDoc::openapi().merge_from(api)))
305+
.into_app()
296306
.configure(|cfg| app_config(cfg, labrinth_config.clone()))
297307
})
298308
.bind(dotenvy::var("BIND_ADDR").unwrap())?
299309
.run()
300310
.await
301311
}
302312

313+
#[derive(utoipa::OpenApi)]
314+
#[openapi(info(title = "Labrinth"))]
315+
struct ApiDoc;
316+
303317
fn log_error(err: &actix_web::Error) {
304318
if err.as_response_error().status_code().is_client_error() {
305319
tracing::debug!(

apps/labrinth/src/routes/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,8 @@ pub fn root_config(cfg: &mut web::ServiceConfig) {
7777
}.boxed_local()
7878
})
7979
);
80-
cfg.service(
81-
web::scope("")
82-
.wrap(default_cors())
83-
.service(index::index_get)
84-
.service(Files::new("/", "assets/")),
85-
);
80+
cfg.service(index::index_get);
81+
cfg.service(Files::new("/", "assets/"));
8682
}
8783

8884
#[derive(thiserror::Error, Debug)]

0 commit comments

Comments
 (0)