init
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
use axum::{Router, routing::get};
|
||||
use axum::{routing::get, Router};
|
||||
|
||||
use super::handlers;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route("/attachments", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
"/attachments/:id",
|
||||
"/attachments",
|
||||
get(handlers::get_all).post(handlers::create),
|
||||
)
|
||||
.route(
|
||||
"/attachments/{id}",
|
||||
get(handlers::get_by_id)
|
||||
.put(handlers::update)
|
||||
.delete(handlers::delete),
|
||||
|
||||
@@ -8,11 +8,12 @@ use axum::Json;
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/auth/login",
|
||||
path = "/auth/login",
|
||||
responses(
|
||||
(status = 200, description = "Login successful", body = LoginResponse),
|
||||
(status = 401, description = "Unauthorized")
|
||||
)
|
||||
),
|
||||
tag = "Auth"
|
||||
)]
|
||||
pub async fn login_user_pw(
|
||||
State(state): State<AppState>,
|
||||
@@ -41,11 +42,15 @@ pub async fn login_user_pw(
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/auth/check",
|
||||
path = "/auth/check",
|
||||
responses(
|
||||
(status = 200, description = "Login successful", body = LoginResponse),
|
||||
(status = 401, description = "Unauthorized")
|
||||
)
|
||||
),
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
),
|
||||
tag = "Auth"
|
||||
)]
|
||||
pub async fn check(
|
||||
State(_state): State<AppState>,
|
||||
|
||||
@@ -4,13 +4,7 @@ use axum::routing::post;
|
||||
use axum::Router;
|
||||
|
||||
pub fn router() -> OxRouter {
|
||||
Router::new().route("/login", post(handlers::login_user_pw))
|
||||
|
||||
// .route("/categorys", get(handlers::get_all).post(handlers::create))
|
||||
// .route(
|
||||
// "/categorys/:id",
|
||||
// get(handlers::get_by_id)
|
||||
// .put(handlers::update)
|
||||
// .delete(handlers::delete),
|
||||
// )
|
||||
Router::new()
|
||||
.route("/auth/login", post(handlers::login_user_pw))
|
||||
.route("/auth/check", post(handlers::check))
|
||||
}
|
||||
|
||||
@@ -72,7 +72,10 @@ pub async fn get_by_id(
|
||||
(status = 404, description = "Serveur non trouvé"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Categories"
|
||||
tag = "Categories",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn create(
|
||||
_admin: Superuser,
|
||||
@@ -108,7 +111,10 @@ pub async fn create(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID de la catégorie")
|
||||
),
|
||||
tag = "Categories"
|
||||
tag = "Categories",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn update(
|
||||
_admin: Superuser,
|
||||
@@ -142,7 +148,10 @@ pub async fn update(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID de la catégorie")
|
||||
),
|
||||
tag = "Categories"
|
||||
tag = "Categories",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn delete(
|
||||
_admin: Superuser,
|
||||
|
||||
@@ -7,7 +7,7 @@ pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/categories", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
"/categories/:id",
|
||||
"/categories/{id}",
|
||||
get(handlers::get_by_id)
|
||||
.put(handlers::update)
|
||||
.delete(handlers::delete),
|
||||
|
||||
@@ -70,7 +70,10 @@ pub async fn get_by_id(
|
||||
(status = 400, description = "Données invalides (Serveur ou Catégorie non trouvée)"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channels"
|
||||
tag = "Channels",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn create(
|
||||
_admin: Superuser,
|
||||
@@ -119,7 +122,10 @@ pub async fn create(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du channel")
|
||||
),
|
||||
tag = "Channels"
|
||||
tag = "Channels",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn update(
|
||||
_admin: Superuser,
|
||||
@@ -173,7 +179,10 @@ pub async fn update(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du channel")
|
||||
),
|
||||
tag = "Channels"
|
||||
tag = "Channels",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn delete(
|
||||
_admin: Superuser,
|
||||
|
||||
@@ -6,7 +6,7 @@ pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/channels", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
"/channels/:id",
|
||||
"/channels/{id}",
|
||||
get(handlers::get_by_id)
|
||||
.put(handlers::update)
|
||||
.delete(handlers::delete),
|
||||
|
||||
@@ -10,5 +10,5 @@ pub struct JoinRequest {
|
||||
pub password: String,
|
||||
#[validate(must_match(other = "password", message = "Passwords do not match"))]
|
||||
pub password_valid: String,
|
||||
pub super_admin_token: Option<String>,
|
||||
pub superuser_token: Option<String>,
|
||||
}
|
||||
|
||||
@@ -7,6 +7,16 @@ use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/join",
|
||||
request_body = JoinRequest,
|
||||
responses(
|
||||
(status = 201, description = "User created successfully"),
|
||||
(status = 400, description = "Validation error"),
|
||||
),
|
||||
tag = "Core"
|
||||
)]
|
||||
pub async fn join(
|
||||
State(state): State<AppState>,
|
||||
ValidatedJson(payload): ValidatedJson<JoinRequest>,
|
||||
@@ -24,8 +34,10 @@ pub async fn join(
|
||||
));
|
||||
};
|
||||
|
||||
let user_am = join_request_to_user_am(payload)?;
|
||||
let user_am = join_request_to_user_am(payload, state.init_token)?;
|
||||
|
||||
let user = state.repositories.user.create(user_am).await?;
|
||||
|
||||
state
|
||||
.repositories
|
||||
.server
|
||||
|
||||
@@ -3,12 +3,22 @@ use crate::models::user;
|
||||
use crate::routes::core::dto::JoinRequest;
|
||||
use anyhow::Result as AnyResult;
|
||||
use sea_orm::Set;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn join_request_to_user_am(
|
||||
payload: JoinRequest,
|
||||
superuser_token: Option<Uuid>,
|
||||
) -> AnyResult<user::ActiveModel> {
|
||||
let is_super_admin = match (payload.superuser_token.as_ref(), superuser_token) {
|
||||
(Some(provided), Some(init)) => provided == &init.to_string(),
|
||||
_ => false,
|
||||
};
|
||||
|
||||
pub fn join_request_to_user_am(payload: JoinRequest) -> AnyResult<user::ActiveModel> {
|
||||
Ok(user::ActiveModel {
|
||||
id: Default::default(),
|
||||
username: Set(payload.username),
|
||||
password: Set(hash_password(&payload.password)?),
|
||||
is_superuser: Set(is_super_admin),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::handlers;
|
||||
use crate::http::OxRouter;
|
||||
use axum::{routing::get, Router};
|
||||
use axum::{routing::post, Router};
|
||||
|
||||
pub fn router() -> OxRouter {
|
||||
Router::new().route("/join", get(handlers::join))
|
||||
Router::new().route("/join", post(handlers::join))
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ use crate::http::error::HTTPError;
|
||||
use crate::routes::group::dto::{CreateGroupRequest, GroupResponse, UpdateGroupRequest};
|
||||
use crate::routes::group::mapper;
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
Json,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -68,7 +68,10 @@ pub async fn get_by_id(
|
||||
(status = 404, description = "Serveur non trouvé"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Groups"
|
||||
tag = "Groups",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn create(
|
||||
_admin: Superuser,
|
||||
@@ -104,7 +107,10 @@ pub async fn create(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du groupe")
|
||||
),
|
||||
tag = "Groups"
|
||||
tag = "Groups",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn update(
|
||||
_admin: Superuser,
|
||||
@@ -138,7 +144,10 @@ pub async fn update(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du groupe")
|
||||
),
|
||||
tag = "Groups"
|
||||
tag = "Groups",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn delete(
|
||||
_admin: Superuser,
|
||||
|
||||
@@ -7,7 +7,7 @@ pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/groups", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
"/groups/:id",
|
||||
"/groups/{id}",
|
||||
get(handlers::get_by_id)
|
||||
.put(handlers::update)
|
||||
.delete(handlers::delete),
|
||||
|
||||
@@ -70,7 +70,10 @@ pub async fn get_by_id(
|
||||
(status = 400, description = "Données invalides (canal non trouvé)"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Messages"
|
||||
tag = "Messages",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn create(
|
||||
user: CurrentUser,
|
||||
@@ -119,7 +122,10 @@ pub async fn create(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du message")
|
||||
),
|
||||
tag = "Messages"
|
||||
tag = "Messages",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn update(
|
||||
user: CurrentUser,
|
||||
@@ -159,7 +165,10 @@ pub async fn update(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du message")
|
||||
),
|
||||
tag = "Messages"
|
||||
tag = "Messages",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn delete(
|
||||
user: CurrentUser,
|
||||
|
||||
@@ -6,7 +6,7 @@ pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/messages", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
"/messages/:id",
|
||||
"/messages/{id}",
|
||||
get(handlers::get_by_id)
|
||||
.put(handlers::update)
|
||||
.delete(handlers::delete),
|
||||
|
||||
+12
-3
@@ -1,5 +1,7 @@
|
||||
use crate::http::OxRouter;
|
||||
use axum::Router;
|
||||
use utoipa::OpenApi;
|
||||
use utoipa_swagger_ui::SwaggerUi;
|
||||
|
||||
pub mod attachment;
|
||||
pub mod auth;
|
||||
@@ -8,17 +10,24 @@ pub mod channel;
|
||||
pub mod core;
|
||||
pub mod group;
|
||||
pub mod message;
|
||||
pub mod openapi;
|
||||
pub mod server;
|
||||
pub mod user;
|
||||
|
||||
pub fn router() -> OxRouter {
|
||||
Router::new()
|
||||
let api_routes = Router::new()
|
||||
.merge(auth::routes::router())
|
||||
.merge(server::routes::router())
|
||||
.merge(category::routes::router())
|
||||
.merge(channel::routes::router())
|
||||
.merge(group::routes::router())
|
||||
.merge(message::routes::router())
|
||||
.merge(user::routes::router())
|
||||
// .merge(attachment::routes::router())
|
||||
.merge(user::routes::router());
|
||||
|
||||
Router::new()
|
||||
.nest("/api", api_routes)
|
||||
// .merge(attachment::routes::router())
|
||||
.merge(
|
||||
SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", openapi::ApiDoc::openapi()),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
use crate::models::channel::ChannelType;
|
||||
use crate::routes::*;
|
||||
use utoipa::openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme};
|
||||
use utoipa::{Modify, OpenApi};
|
||||
|
||||
#[derive(OpenApi)]
|
||||
#[openapi(
|
||||
paths(
|
||||
auth::handlers::login_user_pw,
|
||||
auth::handlers::check,
|
||||
user::handlers::get_all,
|
||||
user::handlers::get_by_id,
|
||||
user::handlers::create,
|
||||
user::handlers::update,
|
||||
user::handlers::delete,
|
||||
server::handlers::get_all,
|
||||
server::handlers::get_by_id,
|
||||
server::handlers::create,
|
||||
server::handlers::update,
|
||||
server::handlers::delete,
|
||||
category::handlers::get_all,
|
||||
category::handlers::get_by_id,
|
||||
category::handlers::create,
|
||||
category::handlers::update,
|
||||
category::handlers::delete,
|
||||
channel::handlers::get_all,
|
||||
channel::handlers::get_by_id,
|
||||
channel::handlers::create,
|
||||
channel::handlers::update,
|
||||
channel::handlers::delete,
|
||||
group::handlers::get_all,
|
||||
group::handlers::get_by_id,
|
||||
group::handlers::create,
|
||||
group::handlers::update,
|
||||
group::handlers::delete,
|
||||
message::handlers::get_all,
|
||||
message::handlers::get_by_id,
|
||||
message::handlers::create,
|
||||
message::handlers::update,
|
||||
message::handlers::delete,
|
||||
core::handlers::join,
|
||||
),
|
||||
components(
|
||||
schemas(
|
||||
auth::dto::LoginRequest,
|
||||
auth::dto::LoginResponse,
|
||||
auth::dto::CheckResponse,
|
||||
user::dto::UserResponse,
|
||||
user::dto::CreateUserRequest,
|
||||
user::dto::UpdateUserRequest,
|
||||
server::dto::ServerResponse,
|
||||
server::dto::CreateServerRequest,
|
||||
server::dto::UpdateServerRequest,
|
||||
category::dto::CategoryResponse,
|
||||
category::dto::CreateCategoryRequest,
|
||||
category::dto::UpdateCategoryRequest,
|
||||
channel::dto::ChannelResponse,
|
||||
channel::dto::CreateChannelRequest,
|
||||
channel::dto::UpdateChannelRequest,
|
||||
group::dto::GroupResponse,
|
||||
group::dto::CreateGroupRequest,
|
||||
group::dto::UpdateGroupRequest,
|
||||
message::dto::MessageResponse,
|
||||
message::dto::CreateMessageRequest,
|
||||
message::dto::UpdateMessageRequest,
|
||||
core::dto::JoinRequest,
|
||||
ChannelType,
|
||||
)
|
||||
),
|
||||
modifiers(&SecurityAddon, &ApiPrefixAddon),
|
||||
tags(
|
||||
(name = "Auth", description = "Endpoints d'authentification"),
|
||||
(name = "Users", description = "Gestion des utilisateurs"),
|
||||
(name = "Servers", description = "Gestion des serveurs"),
|
||||
(name = "Categories", description = "Gestion des catégories"),
|
||||
(name = "Channels", description = "Gestion des salons"),
|
||||
(name = "Groups", description = "Gestion des groupes"),
|
||||
(name = "Messages", description = "Gestion des messages"),
|
||||
(name = "Core", description = "Endpoints de base (enregistrement, etc.)"),
|
||||
)
|
||||
)]
|
||||
pub struct ApiDoc;
|
||||
|
||||
struct SecurityAddon;
|
||||
|
||||
impl Modify for SecurityAddon {
|
||||
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
|
||||
if let Some(components) = openapi.components.as_mut() {
|
||||
components.add_security_scheme(
|
||||
"bearerAuth",
|
||||
SecurityScheme::Http(
|
||||
HttpBuilder::new()
|
||||
.scheme(HttpAuthScheme::Bearer)
|
||||
.bearer_format("JWT")
|
||||
.build(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ApiPrefixAddon;
|
||||
|
||||
impl Modify for ApiPrefixAddon {
|
||||
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
|
||||
let mut new_paths = utoipa::openapi::path::Paths::new();
|
||||
for (path, item) in openapi.paths.paths.iter() {
|
||||
new_paths
|
||||
.paths
|
||||
.insert(format!("/api{}", path), item.clone());
|
||||
}
|
||||
openapi.paths = new_paths;
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,10 @@ pub async fn get_by_id(
|
||||
(status = 201, description = "Serveur créé avec succès", body = ServerResponse),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Servers"
|
||||
tag = "Servers",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn create(
|
||||
_admin: Superuser,
|
||||
@@ -97,7 +100,10 @@ pub async fn create(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du serveur")
|
||||
),
|
||||
tag = "Servers"
|
||||
tag = "Servers",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn update(
|
||||
_admin: Superuser,
|
||||
@@ -131,7 +137,10 @@ pub async fn update(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du serveur")
|
||||
),
|
||||
tag = "Servers"
|
||||
tag = "Servers",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn delete(
|
||||
_admin: Superuser,
|
||||
|
||||
@@ -6,7 +6,7 @@ pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/servers", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
"/servers/:id",
|
||||
"/servers/{id}",
|
||||
get(handlers::get_by_id)
|
||||
.put(handlers::update)
|
||||
.delete(handlers::delete),
|
||||
|
||||
@@ -20,7 +20,10 @@ use uuid::Uuid;
|
||||
(status = 403, description = "Interdit"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Users"
|
||||
tag = "Users",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn get_all(
|
||||
_admin: Superuser,
|
||||
@@ -49,7 +52,10 @@ pub async fn get_all(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID de l'utilisateur")
|
||||
),
|
||||
tag = "Users"
|
||||
tag = "Users",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn get_by_id(
|
||||
_admin: Superuser,
|
||||
@@ -78,7 +84,10 @@ pub async fn get_by_id(
|
||||
(status = 403, description = "Interdit"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Users"
|
||||
tag = "Users",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn create(
|
||||
_admin: Superuser,
|
||||
@@ -120,7 +129,10 @@ pub async fn create(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID de l'utilisateur")
|
||||
),
|
||||
tag = "Users"
|
||||
tag = "Users",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn update(
|
||||
_admin: Superuser,
|
||||
@@ -167,7 +179,10 @@ pub async fn update(
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID de l'utilisateur")
|
||||
),
|
||||
tag = "Users"
|
||||
tag = "Users",
|
||||
security(
|
||||
("bearerAuth" = [])
|
||||
)
|
||||
)]
|
||||
pub async fn delete(
|
||||
_admin: Superuser,
|
||||
|
||||
@@ -7,7 +7,7 @@ pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/users", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
"/users/:id",
|
||||
"/users/{id}",
|
||||
get(handlers::get_by_id)
|
||||
.put(handlers::update)
|
||||
.delete(handlers::delete),
|
||||
|
||||
Reference in New Issue
Block a user