codex_memory/
config.rs

1use crate::error::{Error, Result};
2use serde::{Deserialize, Serialize};
3
4/// Minimal configuration for the storage service
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Config {
7    /// PostgreSQL database connection URL
8    pub database_url: String,
9
10    /// MCP server port
11    pub mcp_port: u16,
12
13    /// Log level (debug, info, warn, error)
14    pub log_level: String,
15}
16
17impl Config {
18    /// Load configuration from environment variables
19    pub fn from_env() -> Result<Self> {
20        // Only load .env file if not in test mode
21        #[cfg(not(test))]
22        dotenvy::dotenv().ok(); // Ignore if .env doesn't exist
23
24        let database_url = std::env::var("DATABASE_URL")
25            .map_err(|_| Error::Config("DATABASE_URL not set".to_string()))?;
26
27        // Ensure DATABASE_URL is not empty
28        if database_url.is_empty() {
29            return Err(Error::Config("DATABASE_URL not set or empty".to_string()));
30        }
31
32        let mcp_port = std::env::var("MCP_PORT")
33            .unwrap_or_else(|_| "3333".to_string())
34            .parse::<u16>()
35            .map_err(|e| Error::Config(format!("Invalid MCP_PORT: {e}")))?;
36
37        let log_level = std::env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string());
38
39        Ok(Self {
40            database_url,
41            mcp_port,
42            log_level,
43        })
44    }
45}
46
47impl Default for Config {
48    fn default() -> Self {
49        // Default should not provide database URL - must come from environment
50        Self {
51            database_url: std::env::var("DATABASE_URL").unwrap_or_else(|_| String::new()), // Empty string if not set - will fail on use
52            mcp_port: 3333,
53            log_level: "info".to_string(),
54        }
55    }
56}