This commit is contained in:
2026-05-16 17:57:54 +02:00
parent 1a2ec26f27
commit b2cefb7d66
55 changed files with 1654 additions and 334 deletions
+39
View File
@@ -66,3 +66,42 @@ where
context.user.clone().ok_or(HTTPError::Unauthorized)
}
}
/// Représente un utilisateur avec les privilèges d'administrateur.
///
/// **Usage :**
/// ```rust
/// pub async fn suppression_globale(admin: Superuser) {
/// // Ici, nous sommes certains que admin.is_superuser est true.
/// }
/// ```
#[derive(Clone, Debug)]
pub struct Superuser(pub user::Model);
impl Deref for Superuser {
type Target = user::Model;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<S> FromRequestParts<S> for Superuser
where
S: Send + Sync,
{
type Rejection = HTTPError;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
// On récupère d'abord l'utilisateur authentifié normalement
let current_user = CurrentUser::from_request_parts(parts, state).await?;
// On vérifie le flag superuser
if current_user.is_superuser {
Ok(Superuser(current_user.0))
} else {
// L'utilisateur est authentifié mais n'a pas les droits
Err(HTTPError::Forbidden)
}
}
}
+16
View File
@@ -23,6 +23,8 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use crate::metrics::{Metrics, MetricsSnapshot};
// ── Compteurs ────────────────────────────────────────────────────────────────
/// Compteurs atomiques du serveur HTTP.
@@ -99,6 +101,14 @@ impl HttpMetrics {
}
}
impl Metrics for HttpMetrics {
type Snapshot = HttpMetricsSnapshot;
fn snapshot(&self) -> HttpMetricsSnapshot {
self.snapshot()
}
}
// ── Snapshot ─────────────────────────────────────────────────────────────────
/// Lecture cohérente des compteurs à un instant T.
@@ -148,6 +158,12 @@ impl HttpMetricsSnapshot {
}
}
impl MetricsSnapshot for HttpMetricsSnapshot {
fn taken_at(&self) -> Instant {
self.taken_at
}
}
// ── Taux ─────────────────────────────────────────────────────────────────────
/// Taux moyens par seconde calculés entre deux snapshots.
+3 -8
View File
@@ -6,7 +6,6 @@
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use axum::middleware as axum_middleware;
use axum::Router;
@@ -16,12 +15,11 @@ use tower_http::catch_panic::CatchPanicLayer;
use tower_http::cors::CorsLayer;
use tower_http::trace::TraceLayer;
use crate::config::{AppConfig, NetworkConfig};
use crate::config::NetworkConfig;
use crate::core::AppState;
use crate::http::OxRouter;
use crate::routes;
use super::metrics::{self, HttpMetrics};
use super::metrics::HttpMetrics;
use super::middleware::context_middleware;
// ── Erreurs ───────────────────────────────────────────────────────────────────
@@ -82,7 +80,7 @@ impl HttpServer {
app_state: AppState,
) -> (Self, broadcast::Sender<()>) {
let bind_addr = SocketAddr::new(network_config.host.into(), network_config.tcp_port);
let metrics = HttpMetrics::new();
let metrics = Arc::clone(&app_state.metrics.http);
let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
(
@@ -106,9 +104,6 @@ impl HttpServer {
/// La future se résout lorsqu'un signal de shutdown est reçu ou qu'une
/// erreur I/O fatale survient.
pub async fn run(mut self) -> Result<(), HttpServerError> {
// Lance le reporter de métriques toutes les 30 secondes
metrics::spawn_reporter(self.metrics.clone(), Duration::from_secs(30));
let metrics = self.metrics.clone();
let app_state = self.app_state.clone();