Skip to main content

rustant_core/gateway/
mod.rs

1//! # WebSocket Gateway
2//!
3//! Provides a WebSocket-based server for real-time communication between
4//! external clients and the Rustant agent. Supports authentication,
5//! connection management, session lifecycle, and a structured event protocol.
6
7mod auth;
8pub mod channel_bridge;
9mod connection;
10mod events;
11pub mod node_bridge;
12mod server;
13mod session;
14
15pub use auth::GatewayAuth;
16pub use channel_bridge::ChannelBridge;
17pub use connection::ConnectionManager;
18pub use events::{ClientMessage, GatewayEvent, ServerMessage};
19pub use node_bridge::NodeBridge;
20pub use server::{
21    GatewayServer, PendingApproval, SharedGateway, StatusProvider, router as gateway_router,
22    run as run_gateway,
23};
24pub use session::{GatewaySession, SessionManager, SessionState};
25
26use serde::{Deserialize, Serialize};
27
28/// Configuration for the WebSocket gateway.
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct GatewayConfig {
31    /// Whether the gateway is enabled.
32    pub enabled: bool,
33    /// Host to bind to.
34    pub host: String,
35    /// Port to listen on.
36    pub port: u16,
37    /// Valid authentication tokens.
38    pub auth_tokens: Vec<String>,
39    /// Maximum concurrent WebSocket connections.
40    pub max_connections: usize,
41    /// Session timeout in seconds (0 = no timeout).
42    pub session_timeout_secs: u64,
43    /// Broadcast channel capacity for event distribution to WebSocket connections.
44    #[serde(default = "default_broadcast_capacity")]
45    pub broadcast_capacity: usize,
46}
47
48fn default_broadcast_capacity() -> usize {
49    256
50}
51
52impl Default for GatewayConfig {
53    fn default() -> Self {
54        Self {
55            enabled: false,
56            host: "127.0.0.1".to_string(),
57            port: 8080,
58            auth_tokens: Vec::new(),
59            max_connections: 10,
60            session_timeout_secs: 3600,
61            broadcast_capacity: 256,
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_gateway_config_default() {
72        let config = GatewayConfig::default();
73        assert!(!config.enabled);
74        assert_eq!(config.host, "127.0.0.1");
75        assert_eq!(config.port, 8080);
76        assert!(config.auth_tokens.is_empty());
77        assert_eq!(config.max_connections, 10);
78        assert_eq!(config.session_timeout_secs, 3600);
79    }
80
81    #[test]
82    fn test_gateway_config_serialization() {
83        let config = GatewayConfig {
84            enabled: true,
85            host: "0.0.0.0".into(),
86            port: 9090,
87            auth_tokens: vec!["token1".into()],
88            max_connections: 50,
89            session_timeout_secs: 7200,
90            broadcast_capacity: 256,
91        };
92        let json = serde_json::to_string(&config).unwrap();
93        let restored: GatewayConfig = serde_json::from_str(&json).unwrap();
94        assert!(restored.enabled);
95        assert_eq!(restored.port, 9090);
96        assert_eq!(restored.auth_tokens.len(), 1);
97    }
98}