mirror of
https://github.com/R0m1k3/Socialflow.git
synced 2026-10-23 23:18:12 +02:00
Serveur Rust : formatage et corrections clippy
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013qMexTfVRaegbPAgsagCAJ
This commit is contained in:
17 files changed
+690
-207
No files matched your search
@@ -17,7 +17,10 @@ pub fn router() -> Router<AppState> {
|
||||
.route("/api/auth/login", post(login))
|
||||
.route("/api/auth/logout", post(logout))
|
||||
.route("/api/auth/session", get(session))
|
||||
.route("/api/auth/default-password-status", get(default_password_status))
|
||||
.route(
|
||||
"/api/auth/default-password-status",
|
||||
get(default_password_status),
|
||||
)
|
||||
}
|
||||
|
||||
async fn login(
|
||||
@@ -28,8 +31,14 @@ async fn login(
|
||||
const BAD_CREDENTIALS: &str = "Nom d'utilisateur ou mot de passe incorrect";
|
||||
|
||||
// passport-local : champs absents ou vides → « Missing credentials »
|
||||
let username = body.get("username").and_then(Value::as_str).filter(|s| !s.is_empty());
|
||||
let password = body.get("password").and_then(Value::as_str).filter(|s| !s.is_empty());
|
||||
let username = body
|
||||
.get("username")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|s| !s.is_empty());
|
||||
let password = body
|
||||
.get("password")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|s| !s.is_empty());
|
||||
let (Some(username), Some(password)) = (username, password) else {
|
||||
return Err(AppError::unauthorized("Missing credentials"));
|
||||
};
|
||||
@@ -53,14 +62,19 @@ async fn login(
|
||||
Ok(auth::with_cookie(auth::user_json(&user), cookie))
|
||||
}
|
||||
|
||||
async fn logout(axum::extract::State(state): axum::extract::State<AppState>, headers: HeaderMap) -> ApiResult {
|
||||
async fn logout(
|
||||
axum::extract::State(state): axum::extract::State<AppState>,
|
||||
headers: HeaderMap,
|
||||
) -> ApiResult {
|
||||
if let Some(session) = auth::load_session(&state, &headers).await {
|
||||
auth::destroy_session(&state, &session.sid)
|
||||
.await
|
||||
.map_err(|_| AppError::internal("Erreur lors de la déconnexion"))?;
|
||||
}
|
||||
let mut response = Json(json!({ "message": "Déconnecté avec succès" })).into_response();
|
||||
response.headers_mut().append(SET_COOKIE, auth::clear_session_cookie());
|
||||
response
|
||||
.headers_mut()
|
||||
.append(SET_COOKIE, auth::clear_session_cookie());
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
@@ -72,7 +86,9 @@ async fn session(MaybeUser(user): MaybeUser) -> Response {
|
||||
}
|
||||
|
||||
/// Le mot de passe de `admin` est-il toujours « admin » ?
|
||||
async fn default_password_status(axum::extract::State(state): axum::extract::State<AppState>) -> ApiResult<Json<Value>> {
|
||||
async fn default_password_status(
|
||||
axum::extract::State(state): axum::extract::State<AppState>,
|
||||
) -> ApiResult<Json<Value>> {
|
||||
let admin = state
|
||||
.db
|
||||
.get_user_by_username("admin")
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
//! Assemblage des routes HTTP.
|
||||
|
||||
mod analytics;
|
||||
mod auth;
|
||||
mod core;
|
||||
mod analytics;
|
||||
mod external;
|
||||
mod facebook;
|
||||
mod legal;
|
||||
@@ -35,7 +35,11 @@ pub fn router(state: AppState) -> Router {
|
||||
.merge(legal::router());
|
||||
|
||||
let config = &state.config;
|
||||
for dir in [&config.uploads_dir, &config.audio_upload_dir, &config.temp_upload_dir] {
|
||||
for dir in [
|
||||
&config.uploads_dir,
|
||||
&config.audio_upload_dir,
|
||||
&config.temp_upload_dir,
|
||||
] {
|
||||
let _ = std::fs::create_dir_all(dir);
|
||||
}
|
||||
for sub in ["media", "logos", "stories"] {
|
||||
@@ -51,7 +55,10 @@ pub fn router(state: AppState) -> Router {
|
||||
api.merge(uploads)
|
||||
.fallback(fallback)
|
||||
.layer(DefaultBodyLimit::max(MAX_JSON_BODY))
|
||||
.layer(middleware::from_fn_with_state(state.clone(), http::rate_limit))
|
||||
.layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
http::rate_limit,
|
||||
))
|
||||
.layer(middleware::from_fn(http::log_requests))
|
||||
.layer(middleware::from_fn(http::security_headers))
|
||||
.with_state(state)
|
||||
@@ -59,7 +66,10 @@ pub fn router(state: AppState) -> Router {
|
||||
|
||||
/// Sonde de santé (Docker) : le serveur répond et la base est joignable.
|
||||
async fn healthz(State(state): State<AppState>) -> Response {
|
||||
match sqlx::query_scalar::<_, i32>("SELECT 1").fetch_one(&state.db.pool).await {
|
||||
match sqlx::query_scalar::<_, i32>("SELECT 1")
|
||||
.fetch_one(&state.db.pool)
|
||||
.await
|
||||
{
|
||||
Ok(_) => (StatusCode::OK, "ok").into_response(),
|
||||
Err(_) => (StatusCode::SERVICE_UNAVAILABLE, "database unavailable").into_response(),
|
||||
}
|
||||
@@ -70,16 +80,27 @@ async fn fallback(State(state): State<AppState>, headers: HeaderMap, request: Re
|
||||
let path = request.uri().path().to_string();
|
||||
if path == "/api" || path.starts_with("/api/") {
|
||||
return if crate::auth::current_user(&state, &headers).await.is_none() {
|
||||
(StatusCode::UNAUTHORIZED, axum::Json(json!({ "error": "Non authentifié" }))).into_response()
|
||||
(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
axum::Json(json!({ "error": "Non authentifié" })),
|
||||
)
|
||||
.into_response()
|
||||
} else {
|
||||
(StatusCode::NOT_FOUND, axum::Json(json!({ "error": "Route introuvable" }))).into_response()
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
axum::Json(json!({ "error": "Route introuvable" })),
|
||||
)
|
||||
.into_response()
|
||||
};
|
||||
}
|
||||
|
||||
// Vérifications de domaine (TikTok, Google, Meta…), sans les fichiers cachés
|
||||
let is_hidden = path.split('/').any(|part| part.starts_with('.'));
|
||||
if !is_hidden && path != "/" {
|
||||
let candidate = state.config.public_root_dir.join(path.trim_start_matches('/'));
|
||||
let candidate = state
|
||||
.config
|
||||
.public_root_dir
|
||||
.join(path.trim_start_matches('/'));
|
||||
if candidate.is_file() {
|
||||
return serve(ServeFile::new(candidate), request).await;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user