2025-12-03 19:33:26 +00:00
|
|
|
use axum::extract::ws::{Message as WsMessage, WebSocket};
|
|
|
|
|
use axum::{
|
2025-12-06 12:18:12 +00:00
|
|
|
extract::{Extension, Query, State, WebSocketUpgrade},
|
2025-12-03 19:33:26 +00:00
|
|
|
http::StatusCode,
|
2025-12-06 12:18:12 +00:00
|
|
|
middleware,
|
2025-12-03 19:33:26 +00:00
|
|
|
response::Response,
|
|
|
|
|
routing::{get, post},
|
|
|
|
|
Json, Router,
|
|
|
|
|
};
|
|
|
|
|
use chat_server::{
|
|
|
|
|
config::SecurityConfig,
|
|
|
|
|
database::pool::create_pool_from_env,
|
2025-12-06 13:45:07 +00:00
|
|
|
delivered_status::DeliveredStatusManager,
|
2025-12-03 19:33:26 +00:00
|
|
|
error::ChatError,
|
2025-12-06 13:45:07 +00:00
|
|
|
event_bus::RabbitMQEventBus,
|
2025-12-06 12:18:12 +00:00
|
|
|
jwt_manager::{AccessTokenClaims, JwtManager},
|
2025-12-06 13:45:07 +00:00
|
|
|
models::message::Message,
|
|
|
|
|
read_receipts::ReadReceiptManager,
|
|
|
|
|
repository::MessageRepository,
|
|
|
|
|
security::permission::PermissionService,
|
|
|
|
|
services::MessageEditService,
|
|
|
|
|
typing_indicator::TypingIndicatorManager,
|
|
|
|
|
monitoring::ChatMetrics,
|
2025-12-03 19:33:26 +00:00
|
|
|
websocket::{
|
|
|
|
|
handler::{websocket_handler, WebSocketState},
|
|
|
|
|
IncomingMessage, OutgoingMessage, WebSocketManager,
|
|
|
|
|
},
|
|
|
|
|
};
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
use futures_util::{FutureExt, SinkExt, StreamExt};
|
2025-12-03 19:33:26 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use sqlx::PgPool;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::sync::Arc;
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
use std::time::Duration;
|
2025-12-03 19:33:26 +00:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
|
use tracing::{error, info, warn};
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
/// État global de l'application
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
struct AppState {
|
2025-12-06 13:45:07 +00:00
|
|
|
message_repo: Arc<MessageRepository>,
|
2025-12-03 19:33:26 +00:00
|
|
|
_ws_manager: Arc<WebSocketManager>,
|
|
|
|
|
database_pool: Option<sqlx::PgPool>,
|
2025-12-06 13:45:07 +00:00
|
|
|
event_bus: Option<Arc<RabbitMQEventBus>>,
|
|
|
|
|
config: chat_server::config::Config,
|
|
|
|
|
jwt_manager: Arc<JwtManager>,
|
|
|
|
|
metrics: Arc<ChatMetrics>,
|
|
|
|
|
permission_service: Arc<PermissionService>,
|
2025-12-03 19:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Requête d'envoi de message
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct SendMessageRequest {
|
2025-12-06 13:45:07 +00:00
|
|
|
conversation_id: Uuid,
|
2025-12-03 19:33:26 +00:00
|
|
|
content: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Paramètres de récupération de messages
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct GetMessagesQuery {
|
2025-12-06 13:45:07 +00:00
|
|
|
conversation_id: Uuid,
|
|
|
|
|
limit: Option<i64>,
|
2025-12-03 19:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Réponse API standard
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
|
struct ApiResponse<T> {
|
|
|
|
|
success: bool,
|
|
|
|
|
data: T,
|
|
|
|
|
message: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> ApiResponse<T> {
|
|
|
|
|
fn success(data: T) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
success: true,
|
|
|
|
|
data,
|
|
|
|
|
message: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use metrics_exporter_prometheus::PrometheusBuilder;
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() -> Result<(), ChatError> {
|
|
|
|
|
// Configuration du logging avec tracing
|
|
|
|
|
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
|
|
|
|
|
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
|
|
|
|
|
|
|
|
|
|
let is_prod = std::env::var("APP_ENV").unwrap_or_default() == "production";
|
|
|
|
|
|
|
|
|
|
if is_prod {
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
|
.with_env_filter(env_filter)
|
|
|
|
|
.json()
|
|
|
|
|
.init();
|
|
|
|
|
} else {
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
|
.with_env_filter(env_filter)
|
|
|
|
|
.with_target(true)
|
|
|
|
|
.with_file(true)
|
|
|
|
|
.with_line_number(true)
|
|
|
|
|
.init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialisation des métriques Prometheus
|
|
|
|
|
let builder = PrometheusBuilder::new();
|
|
|
|
|
let prometheus_handle = builder
|
|
|
|
|
.install_recorder()
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
.map_err(|e| ChatError::configuration_error(&format!("Failed to install Prometheus recorder: {}", e)))?;
|
2025-12-03 19:33:26 +00:00
|
|
|
|
|
|
|
|
info!("🚀 Démarrage du serveur de chat Veza...");
|
|
|
|
|
|
|
|
|
|
let app_config =
|
|
|
|
|
chat_server::config::Config::from_env().map_err(|e| ChatError::Configuration {
|
|
|
|
|
message: e.to_string(),
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
// Initialisation du pool de connexions à la base de données
|
|
|
|
|
let database_pool = match create_pool_from_env(Some(&app_config.database_url)).await {
|
|
|
|
|
Ok(pool) => {
|
|
|
|
|
info!("✅ Pool de connexions PostgreSQL initialisé avec succès");
|
|
|
|
|
Some(pool)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("⚠️ Échec d'initialisation du pool de connexions: {}. Le serveur continuera sans base de données.", e);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
// Database pool est requis pour les managers
|
|
|
|
|
let pool_ref = database_pool.as_ref().ok_or_else(|| {
|
|
|
|
|
ChatError::configuration_error("Database pool is required but not initialized")
|
|
|
|
|
})?;
|
2025-12-03 19:33:26 +00:00
|
|
|
let message_repo = Arc::new(MessageRepository::new(pool_ref.clone()));
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
let read_receipt_manager = Arc::new(ReadReceiptManager::new(pool_ref.clone()));
|
|
|
|
|
let delivered_status_manager = Arc::new(DeliveredStatusManager::new(pool_ref.clone()));
|
|
|
|
|
let typing_indicator_manager = Arc::new(TypingIndicatorManager::new());
|
|
|
|
|
let permission_service = Arc::new(PermissionService::new(pool_ref.clone()));
|
|
|
|
|
let message_edit_service = Arc::new(MessageEditService::new(pool_ref.clone()));
|
2025-12-06 13:45:07 +00:00
|
|
|
|
|
|
|
|
// Metrics
|
|
|
|
|
let metrics = Arc::new(ChatMetrics::new());
|
2025-12-03 19:33:26 +00:00
|
|
|
|
|
|
|
|
// Initialisation de l'Event Bus RabbitMQ
|
|
|
|
|
let event_bus = match RabbitMQEventBus::new_with_retry(app_config.rabbit_mq.clone()).await {
|
|
|
|
|
Ok(eb) => {
|
|
|
|
|
info!("✅ Event Bus RabbitMQ initialisé avec succès");
|
|
|
|
|
Some(eb)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("⚠️ Échec d'initialisation de l'Event Bus RabbitMQ: {}. Le serveur démarrera en mode dégradé (sans Event Bus).", e);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Initialisation du gestionnaire WebSocket
|
|
|
|
|
let ws_manager = Arc::new(WebSocketManager::new());
|
|
|
|
|
|
|
|
|
|
// Initialisation du gestionnaire JWT
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
let jwt_secret = chat_server::env::require_env_min_length("JWT_SECRET", 32);
|
2025-12-03 19:33:26 +00:00
|
|
|
|
|
|
|
|
let security_config = SecurityConfig {
|
|
|
|
|
jwt_secret,
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
jwt_access_duration: Duration::from_secs(900), // 15 min
|
|
|
|
|
jwt_refresh_duration: Duration::from_secs(86400 * 30), // 30 days
|
|
|
|
|
jwt_algorithm: "HS256".to_string(),
|
|
|
|
|
jwt_audience: "veza-chat".to_string(),
|
|
|
|
|
jwt_issuer: "veza-backend".to_string(),
|
|
|
|
|
enable_2fa: false,
|
|
|
|
|
totp_window: 1,
|
|
|
|
|
content_filtering: false,
|
|
|
|
|
password_min_length: 8,
|
|
|
|
|
bcrypt_cost: 12,
|
2025-12-03 19:33:26 +00:00
|
|
|
};
|
|
|
|
|
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
// Créer JwtManager avec pool DB si disponible
|
2025-12-03 19:33:26 +00:00
|
|
|
let jwt_manager = Arc::new(
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
if let Some(ref pool) = database_pool {
|
|
|
|
|
JwtManager::with_pool(security_config, pool.clone())
|
|
|
|
|
.map_err(|e| ChatError::configuration_error(&format!("JWT Manager error: {}", e)))?
|
|
|
|
|
} else {
|
|
|
|
|
JwtManager::new(security_config)
|
|
|
|
|
.map_err(|e| ChatError::configuration_error(&format!("JWT Manager error: {}", e)))?
|
|
|
|
|
}
|
2025-12-03 19:33:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Définir l'adresse d'écoute
|
|
|
|
|
let bind_addr = format!("{}:{}", app_config.host, app_config.port);
|
|
|
|
|
|
|
|
|
|
// État pour les routes HTTP (AppState reste pour compatibilité)
|
|
|
|
|
let state = AppState {
|
2025-12-06 13:45:07 +00:00
|
|
|
message_repo: message_repo.clone(),
|
2025-12-03 19:33:26 +00:00
|
|
|
_ws_manager: ws_manager.clone(),
|
|
|
|
|
database_pool: database_pool.clone(),
|
2025-12-06 13:45:07 +00:00
|
|
|
event_bus: event_bus.map(Arc::new),
|
|
|
|
|
config: app_config.clone(),
|
|
|
|
|
jwt_manager: jwt_manager.clone(),
|
|
|
|
|
metrics: metrics.clone(),
|
|
|
|
|
permission_service: permission_service.clone(),
|
2025-12-03 19:33:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// État pour le handler WebSocket
|
|
|
|
|
let ws_state = WebSocketState {
|
2025-12-06 13:45:07 +00:00
|
|
|
message_repo: message_repo.clone(),
|
|
|
|
|
read_receipt_manager: read_receipt_manager.clone(),
|
|
|
|
|
delivered_status_manager: delivered_status_manager.clone(),
|
|
|
|
|
typing_indicator_manager: typing_indicator_manager.clone(),
|
|
|
|
|
message_edit_service: message_edit_service.clone(),
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
ws_manager: ws_manager.clone(),
|
2025-12-06 13:45:07 +00:00
|
|
|
jwt_manager: jwt_manager.clone(),
|
|
|
|
|
permission_service: permission_service.clone(),
|
|
|
|
|
metrics: metrics.clone(),
|
2025-12-03 19:33:26 +00:00
|
|
|
};
|
|
|
|
|
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
// Démarrer le task de monitoring des typing indicators
|
|
|
|
|
let typing_manager_monitor = typing_indicator_manager.clone();
|
|
|
|
|
let ws_manager_monitor = ws_manager.clone();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(500));
|
|
|
|
|
loop {
|
|
|
|
|
interval.tick().await;
|
|
|
|
|
|
|
|
|
|
let expired_changes = typing_manager_monitor.monitor_timeouts().await;
|
|
|
|
|
|
|
|
|
|
for change in expired_changes {
|
|
|
|
|
let typing_message = OutgoingMessage::UserTyping {
|
|
|
|
|
conversation_id: change.conversation_id,
|
|
|
|
|
user_id: change.user_id,
|
|
|
|
|
is_typing: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Err(e) = ws_manager_monitor
|
|
|
|
|
.broadcast_to_conversation(change.conversation_id, typing_message)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
warn!(
|
|
|
|
|
conversation_id = %change.conversation_id,
|
|
|
|
|
user_id = %change.user_id,
|
|
|
|
|
error = %e,
|
|
|
|
|
"Erreur lors du broadcast de typing timeout"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
info!("✅ Task de monitoring des typing indicators démarré");
|
|
|
|
|
|
2025-12-06 13:45:07 +00:00
|
|
|
// Configuration des routes
|
2025-12-03 19:33:26 +00:00
|
|
|
let app = Router::new()
|
|
|
|
|
.route("/health", get(health_check))
|
2025-12-06 13:45:07 +00:00
|
|
|
.route("/healthz", get(health_check))
|
|
|
|
|
.route("/readyz", get(readiness_check))
|
2025-12-03 19:33:26 +00:00
|
|
|
.route(
|
|
|
|
|
"/metrics",
|
|
|
|
|
get(move || std::future::ready(prometheus_handle.render())),
|
2025-12-06 13:45:07 +00:00
|
|
|
)
|
2025-12-06 12:18:12 +00:00
|
|
|
.route("/api/messages/stats", get(get_stats));
|
|
|
|
|
|
|
|
|
|
let api_routes = Router::new()
|
|
|
|
|
.route("/api/messages/{conversation_id}", get(get_messages))
|
2025-12-03 19:33:26 +00:00
|
|
|
.route("/api/messages", post(send_message))
|
2025-12-06 12:18:12 +00:00
|
|
|
.route_layer(middleware::from_fn_with_state(state.clone(), auth_middleware));
|
|
|
|
|
|
|
|
|
|
let app = app.merge(api_routes)
|
2025-12-03 19:33:26 +00:00
|
|
|
.route(
|
|
|
|
|
"/ws",
|
|
|
|
|
get({
|
|
|
|
|
let ws_state_clone = ws_state.clone();
|
|
|
|
|
move |ws: WebSocketUpgrade, query: Query<HashMap<String, String>>| async move {
|
|
|
|
|
websocket_handler(ws, query, State(ws_state_clone)).await
|
|
|
|
|
}
|
|
|
|
|
}),
|
2025-12-06 13:45:07 +00:00
|
|
|
)
|
|
|
|
|
.with_state(state);
|
2025-12-03 19:33:26 +00:00
|
|
|
|
|
|
|
|
// Démarrage du serveur
|
|
|
|
|
let listener = TcpListener::bind(&bind_addr)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| ChatError::configuration_error(&format!("Bind error on {bind_addr}: {e}")))?;
|
|
|
|
|
|
|
|
|
|
info!("✅ Serveur démarré sur http://{}", bind_addr);
|
|
|
|
|
info!("📊 Endpoints disponibles:");
|
|
|
|
|
info!(" - GET /health - Vérification de santé");
|
2025-12-06 13:45:07 +00:00
|
|
|
info!(" - GET /api/messages/:conversation_id - Récupération des messages");
|
2025-12-03 19:33:26 +00:00
|
|
|
info!(" - POST /api/messages - Envoi de message");
|
|
|
|
|
info!(" - GET /api/messages/stats - Statistiques");
|
|
|
|
|
info!(" - GET /ws - WebSocket Chat (🆕)");
|
|
|
|
|
|
|
|
|
|
axum::serve(listener, app)
|
2025-12-06 11:02:46 +00:00
|
|
|
.with_graceful_shutdown(shutdown_signal())
|
2025-12-03 19:33:26 +00:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| ChatError::configuration_error(&format!("Server error: {e}")))?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Endpoint de readiness (DB check)
|
|
|
|
|
async fn readiness_check(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
) -> Result<Json<ApiResponse<HashMap<String, String>>>, StatusCode> {
|
|
|
|
|
let mut info = HashMap::new();
|
|
|
|
|
|
|
|
|
|
// Check Database
|
|
|
|
|
if let Some(pool) = &state.database_pool {
|
|
|
|
|
if let Err(e) = sqlx::query("SELECT 1").execute(pool).await {
|
|
|
|
|
warn!("Readiness check failed (DB): {}", e);
|
|
|
|
|
return Err(StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
warn!("Readiness check failed (No DB pool)");
|
|
|
|
|
return Err(StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check RabbitMQ Event Bus
|
|
|
|
|
if state.config.rabbit_mq.enable {
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
if let Some(ref event_bus) = state.event_bus {
|
|
|
|
|
if !event_bus.is_enabled {
|
2025-12-06 13:45:07 +00:00
|
|
|
warn!("Readiness check failed (RabbitMQ EventBus not enabled)");
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
return Err(StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-12-06 13:45:07 +00:00
|
|
|
warn!("Readiness check failed (RabbitMQ EventBus not initialized but enabled in config)");
|
2025-12-03 19:33:26 +00:00
|
|
|
return Err(StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info.insert("status".to_string(), "ready".to_string());
|
|
|
|
|
Ok(Json(ApiResponse::success(info)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Endpoint de vérification de santé
|
|
|
|
|
#[tracing::instrument(skip(state))]
|
|
|
|
|
async fn health_check(State(state): State<AppState>) -> Json<ApiResponse<HashMap<String, String>>> {
|
|
|
|
|
let mut info = HashMap::new();
|
|
|
|
|
info.insert("status".to_string(), "healthy".to_string());
|
|
|
|
|
info.insert("service".to_string(), "veza-chat-server".to_string());
|
|
|
|
|
info.insert("version".to_string(), "0.3.0".to_string());
|
|
|
|
|
info.insert("websocket".to_string(), "enabled".to_string());
|
|
|
|
|
|
|
|
|
|
if let Some(pool) = &state.database_pool {
|
|
|
|
|
match sqlx::query("SELECT 1").execute(pool).await {
|
2025-12-06 13:45:07 +00:00
|
|
|
Ok(_) => { info.insert("database".to_string(), "connected".to_string()); }
|
|
|
|
|
Err(e) => { info.insert("database".to_string(), format!("error: {}", e)); }
|
2025-12-03 19:33:26 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
info.insert("database".to_string(), "not_configured".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(event_bus) = &state.event_bus {
|
|
|
|
|
if event_bus.is_enabled {
|
|
|
|
|
info.insert("rabbitmq".to_string(), "connected".to_string());
|
|
|
|
|
} else {
|
|
|
|
|
info.insert("rabbitmq".to_string(), "disabled".to_string());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if state.config.rabbit_mq.enable {
|
|
|
|
|
info.insert("rabbitmq".to_string(), "disconnected".to_string());
|
|
|
|
|
} else {
|
|
|
|
|
info.insert("rabbitmq".to_string(), "not_configured".to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json(ApiResponse::success(info))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Récupération des messages
|
|
|
|
|
#[tracing::instrument(skip(state, params))]
|
|
|
|
|
async fn get_messages(
|
|
|
|
|
State(state): State<AppState>,
|
2025-12-06 12:18:12 +00:00
|
|
|
Extension(claims): Extension<AccessTokenClaims>,
|
2025-12-06 13:45:07 +00:00
|
|
|
axum::extract::Path(conversation_id): axum::extract::Path<Uuid>,
|
2025-12-03 19:33:26 +00:00
|
|
|
Query(params): Query<GetMessagesQuery>,
|
|
|
|
|
) -> Result<Json<ApiResponse<Vec<Message>>>, StatusCode> {
|
2025-12-06 12:18:12 +00:00
|
|
|
let user_uuid = Uuid::parse_str(&claims.user_id).map_err(|_| StatusCode::UNAUTHORIZED)?;
|
|
|
|
|
|
|
|
|
|
state.permission_service
|
|
|
|
|
.can_read_conversation(user_uuid, conversation_id)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|_| StatusCode::FORBIDDEN)?;
|
|
|
|
|
|
2025-12-03 19:33:26 +00:00
|
|
|
let limit = params.limit.unwrap_or(50).min(100);
|
|
|
|
|
|
|
|
|
|
let messages = state
|
|
|
|
|
.message_repo
|
2025-12-06 13:45:07 +00:00
|
|
|
.get_conversation_messages(conversation_id, limit)
|
2025-12-03 19:33:26 +00:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
warn!("Erreur récupération messages conversation: {}", e);
|
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
Ok(Json(ApiResponse::success(messages)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Envoi de message
|
|
|
|
|
#[tracing::instrument(skip(state, payload))]
|
|
|
|
|
async fn send_message(
|
|
|
|
|
State(state): State<AppState>,
|
2025-12-06 12:18:12 +00:00
|
|
|
Extension(claims): Extension<AccessTokenClaims>,
|
2025-12-03 19:33:26 +00:00
|
|
|
Json(payload): Json<SendMessageRequest>,
|
|
|
|
|
) -> Result<Json<ApiResponse<Uuid>>, StatusCode> {
|
2025-12-06 12:18:12 +00:00
|
|
|
let user_uuid = Uuid::parse_str(&claims.user_id).map_err(|_| StatusCode::UNAUTHORIZED)?;
|
|
|
|
|
|
|
|
|
|
state.permission_service
|
|
|
|
|
.can_send_message(user_uuid, payload.conversation_id)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|_| StatusCode::FORBIDDEN)?;
|
|
|
|
|
|
2025-12-03 19:33:26 +00:00
|
|
|
let message = state
|
|
|
|
|
.message_repo
|
2025-12-06 13:45:07 +00:00
|
|
|
.create(payload.conversation_id, user_uuid, &payload.content)
|
2025-12-03 19:33:26 +00:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
warn!("Erreur envoi message: {}", e);
|
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
|
|
|
})?;
|
|
|
|
|
|
2025-12-06 13:45:07 +00:00
|
|
|
info!("✅ Message envoyé - ID: {:?}, sender: {:?}", message.id, message.sender_id);
|
2025-12-03 19:33:26 +00:00
|
|
|
|
|
|
|
|
Ok(Json(ApiResponse::success(message.id)))
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-06 13:45:07 +00:00
|
|
|
/// Statistiques avec métriques réelles (Memory/CPU)
|
|
|
|
|
#[tracing::instrument(skip(state))]
|
|
|
|
|
async fn get_stats(State(state): State<AppState>) -> Json<ApiResponse<HashMap<String, serde_json::Value>>> {
|
2025-12-06 12:25:25 +00:00
|
|
|
let mut stats = HashMap::new();
|
2025-12-06 13:45:07 +00:00
|
|
|
|
|
|
|
|
// Récupérer les métriques système via metrics
|
|
|
|
|
let (memory_mb, cpu) = state.metrics.get_system_metrics().await;
|
|
|
|
|
|
|
|
|
|
stats.insert("active_users".to_string(), serde_json::json!(0)); // Placeholder for active users
|
|
|
|
|
stats.insert("server_memory_mb".to_string(), serde_json::json!(memory_mb));
|
|
|
|
|
stats.insert("server_cpu_percent".to_string(), serde_json::json!(cpu));
|
|
|
|
|
stats.insert("websocket_enabled".to_string(), serde_json::json!(true));
|
2025-12-06 12:25:25 +00:00
|
|
|
|
2025-12-03 19:33:26 +00:00
|
|
|
Json(ApiResponse::success(stats))
|
|
|
|
|
}
|
2025-12-06 11:02:46 +00:00
|
|
|
|
2025-12-06 12:18:12 +00:00
|
|
|
/// Middleware d'authentification
|
|
|
|
|
async fn auth_middleware(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
mut req: axum::extract::Request,
|
|
|
|
|
next: axum::middleware::Next,
|
|
|
|
|
) -> Result<axum::response::Response, StatusCode> {
|
|
|
|
|
let auth_header = req.headers()
|
|
|
|
|
.get(axum::http::header::AUTHORIZATION)
|
|
|
|
|
.and_then(|header| header.to_str().ok());
|
|
|
|
|
|
|
|
|
|
let auth_header = if let Some(auth_header) = auth_header {
|
|
|
|
|
auth_header
|
|
|
|
|
} else {
|
|
|
|
|
return Err(StatusCode::UNAUTHORIZED);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !auth_header.starts_with("Bearer ") {
|
|
|
|
|
return Err(StatusCode::UNAUTHORIZED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let token = &auth_header[7..];
|
|
|
|
|
|
|
|
|
|
match state.jwt_manager.validate_access_token(token).await {
|
|
|
|
|
Ok(claims) => {
|
|
|
|
|
req.extensions_mut().insert(claims);
|
|
|
|
|
Ok(next.run(req).await)
|
|
|
|
|
}
|
|
|
|
|
Err(_) => Err(StatusCode::UNAUTHORIZED),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-06 11:02:46 +00:00
|
|
|
async fn shutdown_signal() {
|
|
|
|
|
let ctrl_c = async {
|
|
|
|
|
tokio::signal::ctrl_c()
|
|
|
|
|
.await
|
|
|
|
|
.expect("failed to install Ctrl+C handler");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
let terminate = async {
|
|
|
|
|
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
|
|
|
|
.expect("failed to install signal handler")
|
|
|
|
|
.recv()
|
|
|
|
|
.await;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
|
let terminate = std::future::pending::<()>();
|
|
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
|
_ = ctrl_c => {},
|
|
|
|
|
_ = terminate => {},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info!("🛑 Signal d'arrêt reçu, fermeture gracieuse...");
|
|
|
|
|
}
|