Skip to main content

manabrew_server/
config.rs

1const DEFAULT_CAPTURE_MAX_GB: u64 = 20;
2const BYTES_PER_GB: u64 = 1024 * 1024 * 1024;
3
4pub struct ServerConfig {
5    pub host: String,
6    pub port: u16,
7    pub health_port: u16,
8    pub max_rooms: usize,
9    pub server_key: String,
10    pub official_key: Option<String>,
11    pub events_dir: Option<String>,
12    pub capture_dir: Option<String>,
13    pub capture_max_gb: u64,
14    pub deck_hub_enabled: bool,
15    pub hub_deck_plays_url: Option<String>,
16    pub hub_deck_plays_token: Option<String>,
17    pub hub_jwks_url: Option<String>,
18}
19
20impl ServerConfig {
21    pub fn from_env() -> Self {
22        ServerConfig {
23            host: std::env::var("FORGE_HOST").unwrap_or_else(|_| "0.0.0.0".into()),
24            port: std::env::var("FORGE_PORT")
25                .ok()
26                .and_then(|p| p.parse().ok())
27                .unwrap_or(9443),
28            health_port: std::env::var("FORGE_HEALTH_PORT")
29                .ok()
30                .and_then(|p| p.parse().ok())
31                .unwrap_or(9444),
32            max_rooms: std::env::var("FORGE_MAX_ROOMS")
33                .ok()
34                .and_then(|r| r.parse().ok())
35                .unwrap_or(100),
36            server_key: std::env::var("MANABREW_SERVER_KEY").unwrap_or_else(|_| "forge".into()),
37            official_key: std::env::var("SECRET_MANABREW_KEY")
38                .ok()
39                .filter(|key| !key.is_empty()),
40            events_dir: std::env::var("MANABREW_EVENTS_DIR")
41                .ok()
42                .filter(|dir| !dir.is_empty()),
43            capture_dir: std::env::var("MANABREW_GAME_CAPTURE_DIR")
44                .ok()
45                .filter(|dir| !dir.is_empty()),
46            capture_max_gb: std::env::var("MANABREW_GAME_CAPTURE_MAX_GB")
47                .ok()
48                .and_then(|gb| gb.parse().ok())
49                .unwrap_or(DEFAULT_CAPTURE_MAX_GB),
50            deck_hub_enabled: std::env::var("DECK_HUB").ok().is_some_and(|value| {
51                matches!(
52                    value.to_ascii_lowercase().as_str(),
53                    "1" | "true" | "yes" | "on"
54                )
55            }),
56            hub_deck_plays_url: std::env::var("MANABREW_HUB_DECK_PLAYS_URL")
57                .ok()
58                .filter(|url| !url.is_empty()),
59            hub_deck_plays_token: std::env::var("MANABREW_HUB_DECK_PLAYS_TOKEN")
60                .ok()
61                .filter(|token| !token.is_empty()),
62            hub_jwks_url: std::env::var("MANABREW_HUB_JWKS_URL")
63                .ok()
64                .filter(|url| !url.is_empty()),
65        }
66    }
67
68    pub fn capture_max_bytes(&self) -> u64 {
69        self.capture_max_gb.saturating_mul(BYTES_PER_GB)
70    }
71}