Skip to content

Commit

Permalink
Add utoipa for api-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Daelon022 committed May 12, 2024
1 parent 5da0318 commit 842f83d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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")?
Expand Down
5 changes: 3 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/user_flow/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserData>,
db: Data<Addr<DbService>>,
Expand Down Expand Up @@ -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<Addr<DbService>>,
user: Json<RegisteredUserData>,
Expand All @@ -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<HttpResponse> {
log::info!("Getting request for checking token!");
let token = req
Expand Down

0 comments on commit 842f83d

Please sign in to comment.