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    /// Create test configuration with custom database URL
47    #[cfg(test)]
48    pub fn for_test(database_url: String) -> Self {
49        Self {
50            database_url,
51            mcp_port: 3333,
52            log_level: "info".to_string(),
53        }
54    }
55}
56
57impl Default for Config {
58    fn default() -> Self {
59        // Default should not provide database URL - must come from environment
60        Self {
61            database_url: std::env::var("DATABASE_URL").unwrap_or_else(|_| String::new()), // Empty string if not set - will fail on use
62            mcp_port: 3333,
63            log_level: "info".to_string(),
64        }
65    }
66}