Skip to main content

max / makenotwork

1.7 KB · 56 lines History Blame Raw
1 //! Configuration loaded from environment variables.
2
3 use std::path::PathBuf;
4
5 /// Application configuration.
6 pub struct Config {
7 /// Port to listen on for SSH connections.
8 pub port: u16,
9 /// MNW API base URL (e.g., "http://localhost:3000").
10 pub api_url: String,
11 /// Service token for authenticating with MNW internal API.
12 pub service_token: String,
13 /// Path to the ed25519 host key file.
14 pub host_key_path: PathBuf,
15 /// Base directory for staging uploaded files.
16 pub staging_dir: PathBuf,
17 /// System user that owns git repositories (for `sudo -u`).
18 pub git_user: String,
19 }
20
21 impl Config {
22 /// Load configuration from environment variables.
23 pub fn from_env() -> anyhow::Result<Self> {
24 let port: u16 = std::env::var("SSH_PORT")
25 .unwrap_or_else(|_| "2222".to_string())
26 .parse()
27 .map_err(|_| anyhow::anyhow!("Invalid SSH_PORT"))?;
28
29 let api_url = std::env::var("MNW_API_URL")
30 .unwrap_or_else(|_| "http://localhost:3000".to_string());
31
32 let service_token = std::env::var("MNW_SERVICE_TOKEN")
33 .map_err(|_| anyhow::anyhow!("MNW_SERVICE_TOKEN is required"))?;
34
35 let host_key_path = std::env::var("SSH_HOST_KEY")
36 .unwrap_or_else(|_| "host_ed25519".to_string())
37 .into();
38
39 let staging_dir = std::env::var("STAGING_DIR")
40 .unwrap_or_else(|_| "/var/lib/mnw-cli/staging".to_string())
41 .into();
42
43 let git_user = std::env::var("GIT_SUDO_USER")
44 .unwrap_or_else(|_| "git".to_string());
45
46 Ok(Config {
47 port,
48 api_url,
49 service_token,
50 host_key_path,
51 staging_dir,
52 git_user,
53 })
54 }
55 }
56