1#[derive(Debug, Clone)]
2pub struct Settings {
3 pub server: ServerSettings,
4 pub browser: BrowserSettings,
5 pub engine: EngineSettings,
6 pub crawler: CrawlerSettings,
7 pub retry: RetrySettings,
8}
9
10#[derive(Debug, Clone)]
11pub struct ServerSettings {
12 pub port: u16,
13 pub host: String,
14 pub max_request_size_mb: usize,
15 pub log_level: String,
16}
17
18#[derive(Debug, Clone)]
19pub struct BrowserSettings {
20 pub headless: bool,
21 pub pool_size: usize,
22 pub timeout_ms: u64,
23}
24
25#[derive(Debug, Clone)]
26pub struct EngineSettings {
27 pub waterfall_enabled: bool,
28 pub waterfall_delay_ms: u64,
29 pub auto_fallback_on_block: bool,
30}
31
32#[derive(Debug, Clone)]
33pub struct CrawlerSettings {
34 pub max_concurrent_requests: usize,
35 pub max_duration_sec: u64,
36 pub rate_limit_per_sec: usize,
37}
38
39#[derive(Debug, Clone)]
40pub struct RetrySettings {
41 pub max_attempts: usize,
42 pub initial_delay_ms: u64,
43 pub max_delay_ms: u64,
44}
45
46fn env_or<T: std::str::FromStr>(key: &str, default: T) -> T {
47 std::env::var(key)
48 .ok()
49 .and_then(|v| v.parse().ok())
50 .unwrap_or(default)
51}
52
53impl Settings {
54 pub fn new() -> Result<Self, String> {
55 Ok(Self {
56 server: ServerSettings {
57 port: env_or("PORT", 8080),
58 host: std::env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string()),
59 max_request_size_mb: env_or("MAX_REQUEST_SIZE_MB", 1),
60 log_level: std::env::var("RUST_LOG")
61 .unwrap_or_else(|_| "essence=info".to_string()),
62 },
63 browser: BrowserSettings {
64 headless: env_or("BROWSER_HEADLESS", true),
65 pool_size: env_or("BROWSER_POOL_SIZE", 5),
66 timeout_ms: env_or("BROWSER_TIMEOUT_MS", 30000),
67 },
68 engine: EngineSettings {
69 waterfall_enabled: env_or("ENGINE_WATERFALL_ENABLED", true),
70 waterfall_delay_ms: env_or("ENGINE_WATERFALL_DELAY_MS", 5000),
71 auto_fallback_on_block: env_or("ESSENCE_ENGINE_AUTO_FALLBACK_ON_BLOCK", true),
72 },
73 crawler: CrawlerSettings {
74 max_concurrent_requests: env_or("MAX_CONCURRENT_REQUESTS", 10),
75 max_duration_sec: env_or("CRAWL_MAX_DURATION_SEC", 300),
76 rate_limit_per_sec: env_or("CRAWL_RATE_LIMIT_PER_SEC", 2),
77 },
78 retry: RetrySettings {
79 max_attempts: env_or("RETRY_MAX_ATTEMPTS", 3),
80 initial_delay_ms: env_or("RETRY_INITIAL_DELAY_MS", 500),
81 max_delay_ms: env_or("RETRY_MAX_DELAY_MS", 30000),
82 },
83 })
84 }
85}