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    /// Only allow GET requests, reject all others (default: false)
40    /// When true, only GET requests are processed; POST, PUT, DELETE, etc. return 405 Method Not Allowed
41    /// Useful for static site prerendering where mutations shouldn't be allowed
42    #[serde(default = "default_forward_get_only")]
43    pub forward_get_only: bool,
44
45    pub control_auth: Option<String>,
46}
47
48fn default_enable_websocket() -> bool {
49    true
50}
51
52fn default_forward_get_only() -> bool {
53    false
54}
55
56fn default_control_port() -> u16 {
57    17809
58}
59
60fn default_proxy_port() -> u16 {
61    3000
62}
63
64fn default_proxy_url() -> String {
65    "http://localhost:8080".to_string()
66}
67
68impl Config {
69    pub fn from_file(path: impl AsRef<Path>) -> Result<Self> {
70        let content = std::fs::read_to_string(path)?;
71        let config: Config = toml::from_str(&content)?;
72        Ok(config)
73    }
74}
75
76impl Default for ServerConfig {
77    fn default() -> Self {
78        Self {
79            control_port: default_control_port(),
80            proxy_port: default_proxy_port(),
81            proxy_url: default_proxy_url(),
82            include_paths: vec![],
83            exclude_paths: vec![],
84            enable_websocket: default_enable_websocket(),
85            forward_get_only: default_forward_get_only(),
86            control_auth: None,
87        }
88    }
89}