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}
18
19impl ServerConfig {
20    pub fn from_env() -> Self {
21        ServerConfig {
22            host: std::env::var("FORGE_HOST").unwrap_or_else(|_| "0.0.0.0".into()),
23            port: std::env::var("FORGE_PORT")
24                .ok()
25                .and_then(|p| p.parse().ok())
26                .unwrap_or(9443),
27            health_port: std::env::var("FORGE_HEALTH_PORT")
28                .ok()
29                .and_then(|p| p.parse().ok())
30                .unwrap_or(9444),
31            max_rooms: std::env::var("FORGE_MAX_ROOMS")
32                .ok()
33                .and_then(|r| r.parse().ok())
34                .unwrap_or(100),
35            server_key: std::env::var("MANABREW_SERVER_KEY").unwrap_or_else(|_| "forge".into()),
36            official_key: std::env::var("SECRET_MANABREW_KEY")
37                .ok()
38                .filter(|key| !key.is_empty()),
39            events_dir: std::env::var("MANABREW_EVENTS_DIR")
40                .ok()
41                .filter(|dir| !dir.is_empty()),
42            capture_dir: std::env::var("MANABREW_GAME_CAPTURE_DIR")
43                .ok()
44                .filter(|dir| !dir.is_empty()),
45            capture_max_gb: std::env::var("MANABREW_GAME_CAPTURE_MAX_GB")
46                .ok()
47                .and_then(|gb| gb.parse().ok())
48                .unwrap_or(DEFAULT_CAPTURE_MAX_GB),
49            deck_hub_enabled: std::env::var("DECK_HUB").ok().is_some_and(|value| {
50                matches!(
51                    value.to_ascii_lowercase().as_str(),
52                    "1" | "true" | "yes" | "on"
53                )
54            }),
55            hub_deck_plays_url: std::env::var("MANABREW_HUB_DECK_PLAYS_URL")
56                .ok()
57                .filter(|url| !url.is_empty()),
58            hub_deck_plays_token: std::env::var("MANABREW_HUB_DECK_PLAYS_TOKEN")
59                .ok()
60                .filter(|token| !token.is_empty()),
61        }
62    }
63
64    pub fn capture_max_bytes(&self) -> u64 {
65        self.capture_max_gb.saturating_mul(BYTES_PER_GB)
66    }
67}