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::{IpAddr, SocketAddr}, path::PathBuf};
16
17use getset::Getters;
18use microsandbox_utils::{env, NAMESPACES_SUBDIR};
19use serde::Deserialize;
20
21use crate::{MicrosandboxServerError, MicrosandboxServerResult};
22
23//--------------------------------------------------------------------------------------------------
24// Constants
25//--------------------------------------------------------------------------------------------------
26
27/// The header name for the proxy authorization
28pub const PROXY_AUTH_HEADER: &str = "Proxy-Authorization";
29
30//--------------------------------------------------------------------------------------------------
31// Types
32//--------------------------------------------------------------------------------------------------
33
34/// Configuration structure that holds all the application settings
35/// loaded from environment variables
36#[derive(Debug, Deserialize, Getters)]
37#[getset(get = "pub with_prefix")]
38pub struct Config {
39    /// Secret key used for JWT token generation and validation
40    key: Option<String>,
41
42    /// Directory for storing namespaces
43    namespace_dir: PathBuf,
44
45    /// Whether to run the server in development mode
46    dev_mode: bool,
47
48    /// Host address to listen on
49    host: IpAddr,
50
51    /// Port number to listen on
52    port: u16,
53
54    /// Address to listen on
55    addr: SocketAddr,
56}
57
58//--------------------------------------------------------------------------------------------------
59// Implementations
60//--------------------------------------------------------------------------------------------------
61
62impl Config {
63    /// Create a new configuration
64    pub fn new(
65        key: Option<String>,
66        host: String,
67        port: u16,
68        namespace_dir: Option<PathBuf>,
69        dev_mode: bool,
70    ) -> MicrosandboxServerResult<Self> {
71        // Check key requirement based on dev mode
72        let key = match key {
73            Some(k) => Some(k),
74            None if dev_mode => None,
75            None => {
76                return Err(MicrosandboxServerError::ConfigError(
77                    "No key provided. A key is required when not in dev mode".to_string(),
78                ));
79            }
80        };
81
82        // Parse host string to IpAddr
83        let host_ip: IpAddr = host.parse().map_err(|_| {
84            MicrosandboxServerError::ConfigError(format!("Invalid host address: {}", host))
85        })?;
86
87        let addr = SocketAddr::new(host_ip, port);
88        let namespace_dir = namespace_dir
89            .unwrap_or_else(|| env::get_microsandbox_home_path().join(NAMESPACES_SUBDIR));
90
91        Ok(Self {
92            key,
93            namespace_dir,
94            dev_mode,
95            host: host_ip,
96            port,
97            addr,
98        })
99    }
100}