From 776406684bdc591a4c97b58b8d28f689881c285e Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 29 Dec 2024 22:40:12 +0100 Subject: Add openapi generation using utoipa --- server/src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'server/src/main.rs') diff --git a/server/src/main.rs b/server/src/main.rs index 223d861..124d914 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -8,14 +8,25 @@ use rocket::response::status::NotFound; use rocket::serde::json::Json; use rocket::{futures, Build, Rocket}; use rocket_db_pools::{sqlx, Connection, Database}; +use utoipa::OpenApi; +use utoipa_swagger_ui::SwaggerUi; mod api_model; mod auth; +use auth::AuthApiAddon; + #[derive(Database)] #[database("eyeballs")] struct Db(sqlx::MySqlPool); +#[derive(OpenApi)] +#[openapi( + paths(projects, project, reviews, review,), + modifiers(&AuthApiAddon), +)] +pub struct MainApi; + enum Role { Reviewer, Watcher, @@ -52,6 +63,14 @@ impl TryFrom for api_model::ReviewState { } } +#[utoipa::path( + responses( + (status = 200, description = "Get all projects", body = api_model::Projects), + ), + security( + ("session" = []), + ), +)] #[get("/projects?&")] async fn projects<'r>( mut db: Connection, @@ -93,6 +112,15 @@ async fn projects<'r>( }) } +#[utoipa::path( + responses( + (status = 200, description = "Get project", body = api_model::Project), + (status = 404, description = "No such project"), + ), + security( + ("session" = []), + ), +)] #[get("/project/")] async fn project<'r>( mut db: Connection, @@ -130,6 +158,14 @@ async fn project<'r>( Ok(Json(project)) } +#[utoipa::path( + responses( + (status = 200, description = "Get all reviews for project", body = api_model::Reviews), + ), + security( + ("session" = []), + ), +)] #[get("/project//reviews?&")] async fn reviews<'r>( mut db: Connection, @@ -180,6 +216,15 @@ async fn reviews<'r>( }) } +#[utoipa::path( + responses( + (status = 200, description = "Get review", body = api_model::Review), + (status = 404, description = "No such review"), + ), + security( + ("session" = []), + ), +)] #[get("/review/")] async fn review<'r>( mut db: Connection, @@ -258,10 +303,20 @@ async fn run_migrations(rocket: Rocket) -> fairing::Result { async fn main() -> Result<(), rocket::Error> { let basepath = "/api/v1"; + let mut api = MainApi::openapi(); + api.merge(auth::AuthApi::openapi()); + api.servers = Some(vec![utoipa::openapi::ServerBuilder::new() + .url(basepath) + .build()]); + let _rocket = rocket::build() .attach(Db::init()) .attach(AdHoc::try_on_ignite("Database Migrations", run_migrations)) .mount(basepath, routes![projects, project, reviews, review]) + .mount( + "/", + SwaggerUi::new("/openapi/ui/<_..>").url("/openapi/openapi.json", api), + ) .attach(auth::stage(basepath.to_string())) .launch() .await?; -- cgit v1.2.3-70-g09d2