2025-12-03 19:36:56 +00:00
|
|
|
//! Stream Server pour Veza
|
|
|
|
|
//!
|
|
|
|
|
//! Serveur de streaming audio temps réel avec WebRTC
|
|
|
|
|
|
|
|
|
|
pub mod analytics;
|
|
|
|
|
pub mod audio;
|
|
|
|
|
pub mod auth;
|
|
|
|
|
pub mod cache;
|
|
|
|
|
pub mod codecs;
|
|
|
|
|
pub mod config;
|
|
|
|
|
pub mod core;
|
|
|
|
|
pub mod database;
|
|
|
|
|
pub mod error;
|
|
|
|
|
pub mod event_bus;
|
|
|
|
|
pub mod health;
|
|
|
|
|
pub mod middleware;
|
|
|
|
|
pub mod monitoring;
|
|
|
|
|
pub mod notifications;
|
2025-12-08 18:57:54 +00:00
|
|
|
pub mod routes;
|
2025-12-03 19:36:56 +00:00
|
|
|
pub mod streaming;
|
|
|
|
|
pub mod structured_logging;
|
|
|
|
|
pub mod transcoding; // NEW: Phase 3 Transcoding Engine
|
|
|
|
|
pub mod utils; // ORIGIN Architecture: Event-driven via RabbitMQ
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::sync::Arc;
|
2025-12-08 18:57:54 +00:00
|
|
|
use sqlx::PgPool;
|
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
use parking_lot::RwLock as PlRwLock;
|
|
|
|
|
use crate::core::sync::{SyncEngine, TimeServer, DriftCompensator, SyncConfig as CoreSyncConfig};
|
|
|
|
|
use crate::core::stream::{StreamManager, StreamConfig as CoreStreamConfig};
|
|
|
|
|
use crate::streaming::websocket_transport::WebSocketSyncTransport;
|
2025-12-03 19:36:56 +00:00
|
|
|
|
|
|
|
|
/// État global de l'application
|
|
|
|
|
/// Cette structure contient tous les services et composants nécessaires au serveur
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct AppState {
|
|
|
|
|
pub config: Arc<config::Config>,
|
2025-12-08 18:57:54 +00:00
|
|
|
pub pool: PgPool,
|
2025-12-03 19:36:56 +00:00
|
|
|
// Ces champs sont initialisés dans main.rs via create_app_state()
|
|
|
|
|
// Utilisation de pub pour permettre l'accès depuis main.rs et les routes
|
|
|
|
|
pub cache: Arc<cache::FileCache>,
|
|
|
|
|
pub metrics: Arc<utils::metrics::Metrics>,
|
|
|
|
|
pub analytics: Arc<analytics::AnalyticsEngine>,
|
|
|
|
|
pub audio_processor: Arc<audio::processing::AudioProcessor>,
|
|
|
|
|
pub adaptive_streaming: Arc<streaming::adaptive::AdaptiveStreamingManager>,
|
|
|
|
|
pub health_monitor: Arc<health::HealthMonitor>,
|
|
|
|
|
pub auth_manager: Arc<auth::AuthManager>,
|
|
|
|
|
pub compression_engine: Arc<audio::compression::CompressionEngine>,
|
|
|
|
|
pub notification_service: Arc<notifications::NotificationService>,
|
|
|
|
|
pub websocket_manager: Arc<streaming::websocket::WebSocketManager>,
|
|
|
|
|
pub event_bus: Option<Arc<event_bus::RabbitMQEventBus>>, // Add RabbitMQEventBus, wrapped in Arc for Clone trait
|
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
|
|
|
pub transcoding_engine: Arc<transcoding::TranscodingEngine>, // Transcoding engine for audio encoding
|
2025-12-08 18:57:54 +00:00
|
|
|
pub sync_engine: Arc<SyncEngine>,
|
|
|
|
|
pub stream_manager: Arc<StreamManager>,
|
2025-12-03 19:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppState {
|
|
|
|
|
pub async fn new(config: config::Config) -> Result<Self, Box<dyn std::error::Error>> {
|
|
|
|
|
let config_arc = Arc::new(config.clone());
|
|
|
|
|
|
2025-12-08 18:57:54 +00:00
|
|
|
// Create database pool
|
|
|
|
|
let pool = database::create_pool_from_config(&config.database).await?;
|
|
|
|
|
|
|
|
|
|
// Run migrations if enabled
|
|
|
|
|
if config.database.migrate_on_start {
|
|
|
|
|
tracing::info!("🔄 Exécution des migrations de base de données...");
|
|
|
|
|
sqlx::migrate!("./migrations")
|
|
|
|
|
.run(&pool)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| format!("Migration failed: {}", e))?;
|
|
|
|
|
tracing::info!("✅ Migrations terminées avec succès");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 19:36:56 +00:00
|
|
|
// Cache needs max_size_mb as usize
|
|
|
|
|
let cache = Arc::new(cache::FileCache::new(config.cache.max_size_mb as usize));
|
|
|
|
|
|
|
|
|
|
// Metrics needs config
|
|
|
|
|
let metrics = Arc::new(utils::metrics::Metrics::new(config_arc.clone()));
|
|
|
|
|
|
2025-12-08 18:57:54 +00:00
|
|
|
// AnalyticsEngine uses the shared pool
|
2025-12-03 19:36:56 +00:00
|
|
|
let analytics = Arc::new(
|
2025-12-08 18:57:54 +00:00
|
|
|
analytics::AnalyticsEngine::new(pool.clone(), config_arc.clone()).await?,
|
2025-12-03 19:36:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let audio_processor = Arc::new(audio::processing::AudioProcessor::new(config_arc.clone()));
|
|
|
|
|
|
|
|
|
|
let compression_engine = Arc::new(audio::compression::CompressionEngine::new(
|
|
|
|
|
config_arc.clone(),
|
|
|
|
|
));
|
|
|
|
|
|
2025-12-08 18:57:54 +00:00
|
|
|
// HealthMonitor needs config and analytics for db check
|
|
|
|
|
let health_monitor = Arc::new(health::HealthMonitor::new(config_arc.clone(), analytics.clone()));
|
2025-12-03 19:36:56 +00:00
|
|
|
|
|
|
|
|
// AuthManager::new returns Result
|
|
|
|
|
let auth_manager = Arc::new(auth::AuthManager::new(config_arc.clone())?);
|
|
|
|
|
|
|
|
|
|
let notification_service =
|
|
|
|
|
Arc::new(notifications::NotificationService::new(config_arc.clone()));
|
|
|
|
|
|
|
|
|
|
// WebSocketManager::new takes no arguments
|
|
|
|
|
let websocket_manager = Arc::new(streaming::websocket::WebSocketManager::new());
|
|
|
|
|
|
|
|
|
|
// Adaptive streaming manager
|
|
|
|
|
let adaptive_streaming = Arc::new(streaming::adaptive::AdaptiveStreamingManager::new(
|
|
|
|
|
config_arc.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
|
|
|
// Transcoding engine
|
|
|
|
|
let worker_count = num_cpus::get();
|
|
|
|
|
let transcoding_engine = Arc::new(transcoding::TranscodingEngine::new(worker_count));
|
|
|
|
|
// Démarrer les workers
|
|
|
|
|
transcoding_engine.start();
|
|
|
|
|
|
2025-12-08 18:57:54 +00:00
|
|
|
// SyncEngine initialization
|
|
|
|
|
let time_server = Arc::new(TimeServer::new(vec![]).await.map_err(|e| format!("Failed to init TimeServer: {}", e))?);
|
|
|
|
|
let drift_compensator = Arc::new(DriftCompensator::new());
|
|
|
|
|
let sync_config = Arc::new(PlRwLock::new(CoreSyncConfig::default()));
|
|
|
|
|
let sync_transport = Arc::new(WebSocketSyncTransport::new(websocket_manager.clone()));
|
|
|
|
|
|
|
|
|
|
let sync_engine = Arc::new(SyncEngine::new(
|
|
|
|
|
time_server,
|
|
|
|
|
drift_compensator,
|
|
|
|
|
sync_config,
|
|
|
|
|
Some(sync_transport), // Now using real transport!
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// StreamManager initialization
|
|
|
|
|
let stream_manager = Arc::new(StreamManager::new(
|
|
|
|
|
CoreStreamConfig::default(),
|
|
|
|
|
pool.clone(),
|
|
|
|
|
sync_engine.clone(),
|
|
|
|
|
).await?);
|
|
|
|
|
|
|
|
|
|
// Start the sync loop
|
|
|
|
|
stream_manager.start_sync_loop().await;
|
|
|
|
|
|
2025-12-03 19:36:56 +00:00
|
|
|
Ok(Self {
|
|
|
|
|
config: config_arc,
|
2025-12-08 18:57:54 +00:00
|
|
|
pool,
|
2025-12-03 19:36:56 +00:00
|
|
|
cache,
|
|
|
|
|
metrics,
|
|
|
|
|
analytics,
|
|
|
|
|
audio_processor,
|
|
|
|
|
adaptive_streaming,
|
|
|
|
|
health_monitor,
|
|
|
|
|
auth_manager,
|
|
|
|
|
compression_engine,
|
|
|
|
|
notification_service,
|
|
|
|
|
websocket_manager,
|
|
|
|
|
event_bus: None, // Initialisé à None, sera assigné dans main.rs
|
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
|
|
|
transcoding_engine,
|
2025-12-08 18:57:54 +00:00
|
|
|
sync_engine,
|
|
|
|
|
stream_manager,
|
2025-12-03 19:36:56 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_app_state_structure() {
|
|
|
|
|
// Just verify struct exists
|
|
|
|
|
assert!(true);
|
|
|
|
|
}
|
|
|
|
|
}
|