Skip to main content

ograf_core/
config.rs

1use std::{env, time::Duration};
2
3/// No `renderer_token` here — access control is delegated to the
4/// `AccessControl` implementation, not handled by Core's env-derived config.
5pub struct Config {
6    pub host: String,
7    pub port: u16,
8    pub graphics_storage: String,
9    pub log_level: String,
10    pub action_timeout_ms: u64,
11    pub graphics_cache_ttl_secs: u64,
12    pub renderer_max_pending: usize,
13}
14
15impl Config {
16    pub fn from_env() -> Self {
17        Self {
18            host: env::var("OGRAF_HOST").unwrap_or_else(|_| "0.0.0.0".into()),
19            port: env::var("OGRAF_PORT")
20                .unwrap_or_else(|_| "8080".into())
21                .parse()
22                .expect("OGRAF_PORT must be a valid port number"),
23            graphics_storage: env::var("OGRAF_STORAGE").unwrap_or_else(|_| "./graphics".into()),
24            log_level: env::var("RUST_LOG").unwrap_or_else(|_| "info".into()),
25            action_timeout_ms: env::var("OGRAF_ACTION_TIMEOUT_MS")
26                .unwrap_or_else(|_| "5000".into())
27                .parse()
28                .expect("OGRAF_ACTION_TIMEOUT_MS must be a valid number"),
29            graphics_cache_ttl_secs: env::var("OGRAF_GRAPHICS_CACHE_TTL_SECS")
30                .unwrap_or_else(|_| "30".into())
31                .parse()
32                .expect("OGRAF_GRAPHICS_CACHE_TTL_SECS must be a valid number"),
33            renderer_max_pending: env::var("OGRAF_RENDERER_MAX_PENDING")
34                .unwrap_or_else(|_| "100".into())
35                .parse()
36                .expect("OGRAF_RENDERER_MAX_PENDING must be a valid number"),
37        }
38    }
39
40    /// How long the server waits for a renderer to confirm a load/action
41    /// command before treating it as failed (504).
42    pub fn action_timeout(&self) -> Duration {
43        Duration::from_millis(self.action_timeout_ms)
44    }
45
46    /// How long to cache the graphics list before re-scanning disk.
47    /// Set to 0 to disable caching (always fetch fresh from disk).
48    pub fn graphics_cache_ttl(&self) -> Duration {
49        Duration::from_secs(self.graphics_cache_ttl_secs)
50    }
51}