Skip to main content

ares_http/
config.rs

1//! HTTP-facing auth and server configuration (moves to ares-http in Phase 7).
2
3use serde::{Deserialize, Serialize};
4
5// ============= Authentication Configuration =============
6
7/// Authentication configuration settings.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct AuthConfig {
10    /// Environment variable name containing the JWT secret.
11    pub jwt_secret_env: String,
12
13    /// JWT access token expiry time in seconds (default: 900 = 15 minutes).
14    #[serde(default = "default_jwt_access_expiry")]
15    pub jwt_access_expiry: i64,
16
17    /// JWT refresh token expiry time in seconds (default: 604800 = 7 days).
18    #[serde(default = "default_jwt_refresh_expiry")]
19    pub jwt_refresh_expiry: i64,
20
21    /// Environment variable name containing the API key.
22    pub api_key_env: String,
23}
24
25fn default_jwt_access_expiry() -> i64 {
26    900
27}
28
29fn default_jwt_refresh_expiry() -> i64 {
30    604800
31}
32
33impl Default for AuthConfig {
34    fn default() -> Self {
35        Self {
36            jwt_secret_env: "JWT_SECRET".to_string(),
37            jwt_access_expiry: default_jwt_access_expiry(),
38            jwt_refresh_expiry: default_jwt_refresh_expiry(),
39            api_key_env: "API_KEY".to_string(),
40        }
41    }
42}
43
44// ============= Server Configuration =============
45
46/// Server configuration settings.
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct ServerConfig {
49    /// Host address to bind to (default: "127.0.0.1").
50    #[serde(default = "default_host")]
51    pub host: String,
52
53    /// Port number to listen on (default: 3000).
54    #[serde(default = "default_port")]
55    pub port: u16,
56
57    /// Log level: "trace", "debug", "info", "warn", "error" (default: "info").
58    #[serde(default = "default_log_level")]
59    pub log_level: String,
60
61    /// Allowed CORS origins (default: ["*"] for development, set explicitly for production).
62    /// Use specific origins like `["https://yourdomain.com"]` in production.
63    #[serde(default = "default_cors_origins")]
64    pub cors_origins: Vec<String>,
65
66    /// Rate limiting: requests per second per IP (default: 100, 0 = disabled).
67    #[serde(default = "default_rate_limit")]
68    pub rate_limit_per_second: u32,
69
70    /// Rate limiting burst size (default: 10).
71    #[serde(default = "default_rate_limit_burst")]
72    pub rate_limit_burst: u32,
73}
74
75fn default_host() -> String {
76    "127.0.0.1".to_string()
77}
78
79fn default_port() -> u16 {
80    3000
81}
82
83fn default_log_level() -> String {
84    "info".to_string()
85}
86
87fn default_cors_origins() -> Vec<String> {
88    vec!["http://localhost:3000".to_string()]
89}
90
91fn default_rate_limit() -> u32 {
92    100
93}
94
95fn default_rate_limit_burst() -> u32 {
96    10
97}
98
99impl Default for ServerConfig {
100    fn default() -> Self {
101        Self {
102            host: default_host(),
103            port: default_port(),
104            log_level: default_log_level(),
105            cors_origins: default_cors_origins(),
106            rate_limit_per_second: default_rate_limit(),
107            rate_limit_burst: default_rate_limit_burst(),
108        }
109    }
110}