phantom_frame/
config.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::path::Path;
4
5#[derive(Debug, Clone, Deserialize, Serialize)]
6pub struct Config {
7    pub server: ServerConfig,
8}
9
10#[derive(Debug, Clone, Deserialize, Serialize)]
11pub struct ServerConfig {
12    #[serde(default = "default_control_port")]
13    pub control_port: u16,
14
15    #[serde(default = "default_proxy_port")]
16    pub proxy_port: u16,
17
18    /// The URL of the backend to proxy to
19    #[serde(default = "default_proxy_url")]
20    pub proxy_url: String,
21
22    /// Paths to include in caching (empty means include all)
23    /// Supports wildcards: ["/api/*", "/*/users"]
24    #[serde(default)]
25    pub include_paths: Vec<String>,
26
27    /// Paths to exclude from caching (empty means exclude none)
28    /// Supports wildcards: ["/admin/*", "/*/private"]
29    /// Exclude overrides include
30    #[serde(default)]
31    pub exclude_paths: Vec<String>,
32
33    /// Enable WebSocket and protocol upgrade support (default: true)
34    /// When enabled, requests with Connection: Upgrade headers will bypass
35    /// the cache and establish a direct bidirectional TCP tunnel
36    #[serde(default = "default_enable_websocket")]
37    pub enable_websocket: bool,
38
39    pub control_auth: Option<String>,
40}
41
42fn default_enable_websocket() -> bool {
43    true
44}
45
46fn default_control_port() -> u16 {
47    17809
48}
49
50fn default_proxy_port() -> u16 {
51    3000
52}
53
54fn default_proxy_url() -> String {
55    "http://localhost:8080".to_string()
56}
57
58impl Config {
59    pub fn from_file(path: impl AsRef<Path>) -> Result<Self> {
60        let content = std::fs::read_to_string(path)?;
61        let config: Config = toml::from_str(&content)?;
62        Ok(config)
63    }
64}
65
66impl Default for ServerConfig {
67    fn default() -> Self {
68        Self {
69            control_port: default_control_port(),
70            proxy_port: default_proxy_port(),
71            proxy_url: default_proxy_url(),
72            include_paths: vec![],
73            exclude_paths: vec![],
74            enable_websocket: default_enable_websocket(),
75            control_auth: None,
76        }
77    }
78}