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        dotenvy::dotenv().ok(); // Ignore if .env doesn't exist
21
22        let database_url = std::env::var("DATABASE_URL")
23            .map_err(|_| Error::Config("DATABASE_URL not set".to_string()))?;
24
25        // Ensure DATABASE_URL is not empty
26        if database_url.is_empty() {
27            return Err(Error::Config("DATABASE_URL not set or empty".to_string()));
28        }
29
30        let mcp_port = std::env::var("MCP_PORT")
31            .unwrap_or_else(|_| "3333".to_string())
32            .parse::<u16>()
33            .map_err(|e| Error::Config(format!("Invalid MCP_PORT: {e}")))?;
34
35        let log_level = std::env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string());
36
37        Ok(Self {
38            database_url,
39            mcp_port,
40            log_level,
41        })
42    }
43}
44
45impl Default for Config {
46    fn default() -> Self {
47        // Default should not provide database URL - must come from environment
48        Self {
49            database_url: std::env::var("DATABASE_URL").unwrap_or_else(|_| String::new()), // Empty string if not set - will fail on use
50            mcp_port: 3333,
51            log_level: "info".to_string(),
52        }
53    }
54}