Skip to main content

fraiseql_server/config/
cors.rs

1//! CORS configuration.
2
3use serde::Deserialize;
4
5/// Cross-Origin Resource Sharing (CORS) policy configuration.
6#[derive(Debug, Clone, Deserialize)]
7pub struct CorsConfig {
8    /// Whether CORS headers are added to responses.  Default: `true`.
9    #[serde(default = "default_enabled")]
10    pub enabled: bool,
11
12    /// Allowed origins (supports wildcards like "https://*.example.com")
13    #[serde(default = "default_origins")]
14    pub origins: Vec<String>,
15
16    /// Allowed HTTP methods
17    #[serde(default = "default_methods")]
18    pub methods: Vec<String>,
19
20    /// Allowed headers
21    #[serde(default = "default_headers")]
22    pub headers: Vec<String>,
23
24    /// Allow credentials
25    #[serde(default)]
26    pub credentials: bool,
27
28    /// Preflight cache duration in seconds
29    #[serde(default = "default_max_age")]
30    pub max_age: u64,
31
32    /// Exposed headers (returned to browser)
33    #[serde(default)]
34    pub expose_headers: Vec<String>,
35
36    /// Allow private network access (for localhost development)
37    #[serde(default)]
38    pub private_network: bool,
39}
40
41impl Default for CorsConfig {
42    fn default() -> Self {
43        Self {
44            enabled:         default_enabled(),
45            origins:         default_origins(),
46            methods:         default_methods(),
47            headers:         default_headers(),
48            credentials:     false,
49            max_age:         default_max_age(),
50            expose_headers:  Vec::new(),
51            private_network: false,
52        }
53    }
54}
55
56const fn default_enabled() -> bool {
57    true
58}
59fn default_origins() -> Vec<String> {
60    vec!["*".to_string()]
61}
62fn default_methods() -> Vec<String> {
63    vec!["GET".to_string(), "POST".to_string(), "OPTIONS".to_string()]
64}
65fn default_headers() -> Vec<String> {
66    vec![
67        "Authorization".to_string(),
68        "Content-Type".to_string(),
69        "X-Request-ID".to_string(),
70    ]
71}
72const fn default_max_age() -> u64 {
73    86400
74}