api/ws/modules/mod.rs
1//! WebSocket topic routes under `/api/ws/modules/...`.
2//!
3//! Contains real-time module-related routes such as announcements,
4//! and nests assignment-specific WebSocket topics as well.
5
6use axum::{middleware::from_fn_with_state, routing::get, Router};
7use util::state::AppState;
8use util::ws::default_websocket_handler;
9
10use crate::ws::modules::assignments::ws_assignment_routes;
11use crate::auth::guards::require_assigned_to_module;
12
13pub mod assignments;
14
15/// Builds the `/api/ws/modules` WebSocket router.
16///
17/// # Routes
18/// - `/api/ws/modules/{module_id}/announcements` (guarded by `require_assigned_to_module`)
19/// - `/api/ws/modules/assignments/...`
20///
21/// # Guards
22/// - `require_assigned_to_module` ensures only users assigned to the module can subscribe to its announcement topic
23pub fn ws_module_routes(app_state: AppState) -> Router<AppState> {
24 Router::new()
25 .route("/{module_id}/announcements", get(default_websocket_handler).route_layer(from_fn_with_state(app_state.clone(), require_assigned_to_module)))
26 .nest("/{module_id}/assignments", ws_assignment_routes())
27}