From 842f83d386ed94ea6966769f9fb6f2f3e628053e Mon Sep 17 00:00:00 2001 From: Daelon022 Date: Sun, 12 May 2024 18:42:05 +0300 Subject: [PATCH] Add utoipa for api-docs --- Cargo.toml | 2 ++ src/main.rs | 20 ++++++++++++++++++++ src/models.rs | 5 +++-- src/user_flow/requests.rs | 24 ++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f06d93..a51decb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,5 @@ jsonwebtoken = "9.3.0" http = "1.1.0" openssl = "0.10.64" alcoholic_jwt = "4091.0.0" +utoipa = { version = "4.2.3", features = ["actix_extras", "chrono", "yaml", "uuid"] } +utoipa-swagger-ui = { version = "7.0.1", features = ["actix-web"] } diff --git a/src/main.rs b/src/main.rs index 25e2001..48c0abc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,22 @@ use crate::user_flow::requests::{check_token, login, register}; use crate::utils::init_logging; use actix::Actor; use actix_web::web::Data; +use utoipa_swagger_ui::SwaggerUi; + +use utoipa::OpenApi; + +#[derive(OpenApi)] +#[openapi( + paths( + crate::user_flow::requests::login, + crate::user_flow::requests::register + ), + components( + schemas(crate::models::RegisteredUserData), + schemas(crate::models::UserData) + ) +)] +struct ApiDoc; #[actix_web::main] async fn main() -> Result<()> { @@ -22,11 +38,15 @@ async fn main() -> Result<()> { let pool = db::utils::create_connection_pool(database_url).await?; let db = DbService::new(pool); let db = db.start(); + let openapi = ApiDoc::openapi(); actix_web::HttpServer::new(move || { actix_web::App::new() .route("/login", actix_web::web::post().to(login)) .route("/register", actix_web::web::post().to(register)) .route("/check_token", actix_web::web::get().to(check_token)) + .service( + SwaggerUi::new("/swagger-ui/{_:.*}").url("/api-docs/openapi.json", openapi.clone()), + ) .app_data(Data::new(db.clone())) }) .bind("localhost:8080")? diff --git a/src/models.rs b/src/models.rs index e80a662..38ce67f 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,14 +1,15 @@ use serde::{Deserialize, Serialize}; +use utoipa::ToSchema; use uuid::Uuid; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct UserData { pub username: String, pub password: String, pub email: String, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct RegisteredUserData { pub id: Uuid, pub username: String, diff --git a/src/user_flow/requests.rs b/src/user_flow/requests.rs index 97d6cfa..4f633fd 100644 --- a/src/user_flow/requests.rs +++ b/src/user_flow/requests.rs @@ -9,6 +9,14 @@ use actix_web::{HttpRequest, HttpResponse}; use alcoholic_jwt::{token_kid, validate, Validation}; use uuid::Uuid; +#[utoipa::path( + post, + path = "/register", + responses( + (status = 200, description = "User successfully registered", body = UserData), + (status = BAD_REQUEST, description = "User already registered") + ) +)] pub async fn register( user: Json, db: Data>, @@ -38,6 +46,14 @@ pub async fn register( Ok(HttpResponse::Ok().body(serde_json::to_string(&user)?)) } +#[utoipa::path( + post, + path = "/login", + responses( + (status = 200, description = "User successfully login", body = RegisteredUserData), + (status = BAD_REQUEST, description = "User not found") + ) +)] pub async fn login( db: Data>, user: Json, @@ -54,6 +70,14 @@ pub async fn login( } } +#[utoipa::path( + post, + path = "/check_token", + responses( + (status = 200, description = "This is a right token"), + (status = BAD_REQUEST, description = "Not correct token") + ) +)] pub async fn check_token(req: HttpRequest) -> crate::errors::Result { log::info!("Getting request for checking token!"); let token = req