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};
16
17use getset::Getters;
18use microsandbox_utils::{env, NAMESPACES_SUBDIR};
19use serde::Deserialize;
20
21use crate::{port::LOCALHOST_IP, 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    /// Address to listen on
49    addr: SocketAddr,
50}
51
52//--------------------------------------------------------------------------------------------------
53// Implementations
54//--------------------------------------------------------------------------------------------------
55
56impl Config {
57    /// Create a new configuration
58    pub fn new(
59        key: Option<String>,
60        port: u16,
61        namespace_dir: Option<PathBuf>,
62        dev_mode: bool,
63    ) -> MicrosandboxServerResult<Self> {
64        // Check key requirement based on dev mode
65        let key = match key {
66            Some(k) => Some(k),
67            None if dev_mode => None,
68            None => {
69                return Err(MicrosandboxServerError::ConfigError(
70                    "No key provided. A key is required when not in dev mode".to_string(),
71                ));
72            }
73        };
74
75        let addr = SocketAddr::new(LOCALHOST_IP, port);
76        let namespace_dir = namespace_dir
77            .unwrap_or_else(|| env::get_microsandbox_home_path().join(NAMESPACES_SUBDIR));
78
79        Ok(Self {
80            key,
81            namespace_dir,
82            dev_mode,
83            addr,
84        })
85    }
86}