summaryrefslogtreecommitdiff
path: root/server/src/main.rs
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2024-12-29 22:40:12 +0100
committerJoel Klinghed <the_jk@spawned.biz>2024-12-29 22:40:12 +0100
commit776406684bdc591a4c97b58b8d28f689881c285e (patch)
tree7551fcc2d10eb4d75314b2ad93bce6c328481413 /server/src/main.rs
parent7bc8e8b7262a3f3abe3222b3b434838e85cdb2bb (diff)
Add openapi generation using utoipa
Diffstat (limited to 'server/src/main.rs')
-rw-r--r--server/src/main.rs55
1 files changed, 55 insertions, 0 deletions
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<u8> for api_model::ReviewState {
}
}
+#[utoipa::path(
+ responses(
+ (status = 200, description = "Get all projects", body = api_model::Projects),
+ ),
+ security(
+ ("session" = []),
+ ),
+)]
#[get("/projects?<limit>&<offset>")]
async fn projects<'r>(
mut db: Connection<Db>,
@@ -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/<projectid>")]
async fn project<'r>(
mut db: Connection<Db>,
@@ -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/<projectid>/reviews?<limit>&<offset>")]
async fn reviews<'r>(
mut db: Connection<Db>,
@@ -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/<reviewid>")]
async fn review<'r>(
mut db: Connection<Db>,
@@ -258,10 +303,20 @@ async fn run_migrations(rocket: Rocket<Build>) -> 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?;