Skip to main content

mesofact_core/proxy/
config.rs

1//! CLI configuration for the mesofact-proxy binary.
2
3use clap::Parser;
4use std::path::PathBuf;
5
6#[derive(Debug, Parser)]
7#[command(name = "mesofact-proxy", about = "mesofact tri-mode web proxy (axum)")]
8pub struct Config {
9    /// Local path to manifest.json (required).
10    #[arg(long, env = "MESOFACT_MANIFEST")]
11    pub manifest: PathBuf,
12
13    /// Bind address for the HTTP listener.
14    #[arg(long, default_value = "0.0.0.0:3000", env = "MESOFACT_BIND")]
15    pub bind: String,
16
17    /// Number of Bun workers to spawn (default = num_cpus / 2, min 1).
18    #[arg(long, env = "MESOFACT_WORKERS")]
19    pub workers: Option<usize>,
20
21    /// CDN base URL for Mode 1 redirect dispatch (e.g. https://cdn.yah.dev).
22    /// When set, Mode 1 routes 302-redirect to `{cdn_base_url}{path}`.
23    #[arg(long, env = "MESOFACT_CDN_BASE_URL")]
24    pub cdn_base_url: Option<String>,
25
26    /// Local dist/ directory for Mode 1 fallback (when CDN is not configured).
27    #[arg(long, env = "MESOFACT_FALLBACK_DIR")]
28    pub fallback_dir: Option<PathBuf>,
29
30    /// Path to the mesofact-worker entry script (bun entrypoint).
31    #[arg(
32        long,
33        env = "MESOFACT_WORKER_ENTRY",
34        default_value = "packages/mesofact-worker/src/worker.ts"
35    )]
36    pub worker_entry: PathBuf,
37
38    /// Path to `mesofact.config.toml`. Read for source generation tokens
39    /// (cache-key input 6). Missing file → generations resolve to a placeholder.
40    #[arg(long, env = "MESOFACT_SOURCES_CONFIG")]
41    pub sources_config: Option<PathBuf>,
42
43    /// Env var holding the HMAC key for `CookieSessionResolver`. When set,
44    /// Mode 2 sessions resolve from the session cookie; when unset, sessions
45    /// are disabled (`requires: ["user"]` routes always redirect/401).
46    #[arg(long, env = "MESOFACT_SESSION_SECRET_ENV")]
47    pub session_secret_env: Option<String>,
48
49    /// Session cookie name (default `mesofact_session`).
50    #[arg(long, env = "MESOFACT_SESSION_COOKIE", default_value = "mesofact_session")]
51    pub session_cookie: String,
52
53    /// Login URL for `requires: ["user"]` routes with no session. The proxy
54    /// 302s here with `?next=<original-url>`. Unset → 401 instead.
55    #[arg(long, env = "MESOFACT_LOGIN_URL")]
56    pub login_url: Option<String>,
57
58    /// Mode 2 LRU response-cache capacity (entries).
59    #[arg(long, env = "MESOFACT_CACHE_CAPACITY", default_value_t = 4096)]
60    pub cache_capacity: usize,
61}
62
63impl Config {
64    pub fn worker_count(&self) -> usize {
65        self.workers
66            .unwrap_or_else(|| (num_cpus::get() / 2).max(1))
67    }
68}