feature: added utoipa api swagger for auth routes

This commit is contained in:
FrenchGithubUser
2025-04-10 12:33:59 +02:00
parent 4298eb2abb
commit 2396dbca42
8 changed files with 47 additions and 46 deletions

View File

@@ -29,7 +29,6 @@ actix-cors = "0.7.1"
actix-http = "3.10.0"
env_logger = "0.11.8"
thiserror = "2.0.12"
# not working yet : https://github.com/juhaku/utoipa/issues/1361
# utoipa = { version = "5", features = ["actix_extras"] }
# utoipa-swagger-ui = { version = "9.0.0", features = ["actix-web"] }
# utoipa-actix-web = "0.1.2"
utoipa = { version = "5", features = ["actix_extras"] }
utoipa-swagger-ui = { version = "9.0.1", features = ["actix-web"] }
utoipa-actix-web = "0.1.2"

17
src/api_doc.rs Normal file
View File

@@ -0,0 +1,17 @@
use utoipa::OpenApi;
use crate::{
handlers::auth_handler::RegisterQuery,
models::user::{Login, Register},
};
#[derive(OpenApi)]
#[openapi(
info(title = "arcadia-index API",),
paths(
crate::handlers::auth_handler::register,
crate::handlers::auth_handler::login,
),
components(schemas(Register, RegisterQuery, Login),)
)]
pub struct ApiDoc;

View File

@@ -20,19 +20,20 @@ use jsonwebtoken::{EncodingKey, Header, encode};
use serde::Deserialize;
use sqlx::types::ipnetwork::IpNetwork;
use std::env;
use utoipa::ToSchema;
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, ToSchema)]
pub struct RegisterQuery {
invitation_key: Option<String>,
}
// #[utoipa::path(
// get,
// path = "/api/register",
// responses(
// (status = 200, description = "Successfully registered the user", body = Register),
// )
// )]
#[utoipa::path(
get,
path = "/api/register",
responses(
(status = 200, description = "Successfully registered the user", body = Register),
)
)]
pub async fn register(
new_user: web::Json<Register>,
arc: web::Data<Arcadia>,
@@ -86,6 +87,13 @@ pub async fn register(
Ok(HttpResponse::Created().json(serde_json::json!(user)))
}
#[utoipa::path(
get,
path = "/api/login",
responses(
(status = 200, description = "Successfully logged in", body = Login),
)
)]
pub async fn login(arc: web::Data<Arcadia>, user_login: web::Json<Login>) -> Result<HttpResponse> {
let user = find_user_with_password(&arc.pool, &user_login).await?;

View File

@@ -1,3 +1,4 @@
pub mod api_doc;
pub mod handlers;
pub mod models;
pub mod repositories;

View File

@@ -9,10 +9,10 @@ use dotenv;
use routes::init;
use sqlx::postgres::PgPoolOptions;
use std::env;
// use utoipa_actix_web::AppExt;
// use utoipa_swagger_ui::SwaggerUi;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
use arcadia_index::{Arcadia, Error, OpenSignups, Result};
use arcadia_index::{Arcadia, Error, OpenSignups, Result, api_doc::ApiDoc};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
@@ -47,11 +47,10 @@ async fn main() -> std::io::Result<()> {
open_signups,
}))
.configure(init) // Initialize routes
// .into_utoipa_app()
// .openapi_service(|api| {
// SwaggerUi::new("/swagger-ui/{_:.*}").url("/api/openapi.json", api)
// })
// .into_app()
.service(
SwaggerUi::new("/swagger-ui/{_:.*}")
.url("/swagger-json/openapi.json", ApiDoc::openapi()),
)
})
.bind(format!("{}:{}", host, port))?
.run()

View File

@@ -1,22 +0,0 @@
// use utoipa::OpenApi;
// use crate::models::{CreateUser, ErrorResponse, User};
// #[derive(OpenApi)]
// #[openapi(
// info(
// title = "Arcadia-index API",
// version = "",
// ),
// servers(
// (url = "http://localhost:8080", description = "Local development server"),
// ),
// components(
// schemas(User, CreateUser, ErrorResponse),
// ),
// paths(
// get_user_by_id,
// create_user,
// ),
// )]
// pub struct ApiDoc;

View File

@@ -1,4 +1,3 @@
pub mod api_doc;
pub mod artist;
pub mod collage;
pub mod edition_group;

View File

@@ -2,7 +2,7 @@ use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use sqlx::prelude::FromRow;
use sqlx::types::ipnetwork::IpNetwork;
// use utoipa::ToSchema;
use utoipa::ToSchema;
#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct User {
@@ -39,7 +39,7 @@ pub struct User {
pub bonus_points: i64,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct Register {
pub username: String,
pub password: String,
@@ -47,7 +47,7 @@ pub struct Register {
pub email: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct Login {
pub username: String,
pub password: String,