microsandbox_server/
config.rs

1//! Configuration module for the microsandbox server.
2//!
3//! This module handles server configuration including:
4//! - Server settings and environment variables
5//! - JWT token configuration
6//! - Namespace management
7//! - Development and production mode settings
8//!
9//! The module provides:
10//! - Configuration structure for server settings
11//! - Default values for server configuration
12//! - Environment-based configuration loading
13//! - Namespace directory management
14
15use std::{net::SocketAddr, path::PathBuf, sync::LazyLock};
16
17use base64::{prelude::BASE64_STANDARD, Engine};
18use getset::Getters;
19use microsandbox_utils::{env, NAMESPACES_SUBDIR};
20use serde::Deserialize;
21
22use crate::{port::LOCALHOST_IP, MicrosandboxServerError, MicrosandboxServerResult};
23
24//--------------------------------------------------------------------------------------------------
25// Constants
26//--------------------------------------------------------------------------------------------------
27
28/// Default JWT header for HS256 algorithm in base64
29pub const DEFAULT_JWT_HEADER: LazyLock<String> =
30    LazyLock::new(|| BASE64_STANDARD.encode("{\"typ\":\"JWT\",\"alg\":\"HS256\"}"));
31
32/// The header name for the proxy authorization
33pub const PROXY_AUTH_HEADER: &str = "Proxy-Authorization";
34
35//--------------------------------------------------------------------------------------------------
36// Types
37//--------------------------------------------------------------------------------------------------
38
39/// Configuration structure that holds all the application settings
40/// loaded from environment variables
41#[derive(Debug, Deserialize, Getters)]
42#[getset(get = "pub with_prefix")]
43pub struct Config {
44    /// Secret key used for JWT token generation and validation
45    key: Option<String>,
46
47    /// Directory for storing namespaces
48    namespace_dir: PathBuf,
49
50    /// Whether to run the server in development mode
51    dev_mode: bool,
52
53    /// Address to listen on
54    addr: SocketAddr,
55}
56
57//--------------------------------------------------------------------------------------------------
58// Implementations
59//--------------------------------------------------------------------------------------------------
60
61impl Config {
62    /// Create a new configuration
63    pub fn new(
64        key: Option<String>,
65        port: u16,
66        namespace_dir: Option<PathBuf>,
67        dev_mode: bool,
68    ) -> MicrosandboxServerResult<Self> {
69        // Check key requirement based on dev mode
70        let key = match key {
71            Some(k) => Some(k),
72            None if dev_mode => None,
73            None => {
74                return Err(MicrosandboxServerError::ConfigError(
75                    "No key provided. A key is required when not in dev mode".to_string(),
76                ));
77            }
78        };
79
80        let addr = SocketAddr::new(LOCALHOST_IP, port);
81        let namespace_dir = namespace_dir
82            .unwrap_or_else(|| env::get_microsandbox_home_path().join(NAMESPACES_SUBDIR));
83
84        Ok(Self {
85            key,
86            namespace_dir,
87            dev_mode,
88            addr,
89        })
90    }
91}