Skip to main content

max / multithreaded

2.2 KB · 62 lines History Blame Raw
1 //! Application configuration read from environment variables.
2
3 use std::sync::Arc;
4 use uuid::Uuid;
5
6 #[derive(Clone)]
7 pub struct Config {
8 pub mnw_base_url: Arc<str>,
9 pub oauth_client_id: String,
10 pub oauth_redirect_uri: String,
11 pub platform_admin_id: Option<Uuid>,
12 /// Whether to set the `Secure` flag on session cookies.
13 /// Defaults to `true`. Set `COOKIE_SECURE=false` for local HTTP development.
14 pub cookie_secure: bool,
15 /// S3 storage configuration. None if S3 env vars are missing.
16 pub s3: Option<S3Config>,
17 /// Shared secret for HMAC-signed internal API requests from MNW.
18 pub internal_shared_secret: Option<String>,
19 }
20
21 #[derive(Clone)]
22 pub struct S3Config {
23 pub endpoint: String,
24 pub bucket: String,
25 pub access_key: String,
26 pub secret_key: String,
27 pub region: String,
28 }
29
30 impl S3Config {
31 fn from_env() -> Option<Self> {
32 let endpoint = std::env::var("S3_ENDPOINT").ok()?;
33 let bucket = std::env::var("S3_BUCKET").ok()?;
34 let access_key = std::env::var("S3_ACCESS_KEY").ok()?;
35 let secret_key = std::env::var("S3_SECRET_KEY").ok()?;
36 let region = std::env::var("S3_REGION").unwrap_or_else(|_| "us-east-1".to_string());
37 Some(Self { endpoint, bucket, access_key, secret_key, region })
38 }
39 }
40
41 impl Config {
42 pub fn from_env() -> Self {
43 Self {
44 mnw_base_url: std::env::var("MNW_BASE_URL")
45 .unwrap_or_else(|_| "http://127.0.0.1:3000".to_string())
46 .into(),
47 oauth_client_id: std::env::var("OAUTH_CLIENT_ID")
48 .expect("OAUTH_CLIENT_ID must be set"),
49 oauth_redirect_uri: std::env::var("OAUTH_REDIRECT_URI")
50 .unwrap_or_else(|_| "http://127.0.0.1:3400/auth/callback".to_string()),
51 platform_admin_id: std::env::var("PLATFORM_ADMIN_ID")
52 .ok()
53 .and_then(|s| Uuid::parse_str(&s).ok()),
54 cookie_secure: std::env::var("COOKIE_SECURE")
55 .map(|v| v != "false")
56 .unwrap_or(true),
57 s3: S3Config::from_env(),
58 internal_shared_secret: std::env::var("INTERNAL_SHARED_SECRET").ok(),
59 }
60 }
61 }
62