use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub server: ServerConfig,
pub database: DatabaseConfig,
pub ai: AiConfig,
pub registry: RegistryConfig,
pub logging: LoggingConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
pub host: String,
pub port: u16,
pub workers: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfig {
pub url: String,
pub max_connections: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AiConfig {
pub openai_api_key: Option<String>,
pub anthropic_api_key: Option<String>,
pub default_model: String,
pub max_tokens: usize,
pub temperature: f32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistryConfig {
pub url: String,
pub cache_ttl: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
pub level: String,
pub format: String,
}
impl Config {
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
let content = std::fs::read_to_string(path)?;
let config: Self = toml::from_str(&content)?;
Ok(config)
}
pub fn default() -> Self {
Self {
server: ServerConfig {
host: "127.0.0.1".to_string(),
port: 8080,
workers: num_cpus::get(),
},
database: DatabaseConfig {
url: "sqlite:opencrates.db".to_string(),
max_connections: 10,
},
ai: AiConfig {
openai_api_key: std::env::var("OPENAI_API_KEY").ok(),
anthropic_api_key: std::env::var("ANTHROPIC_API_KEY").ok(),
default_model: "gpt-4".to_string(),
max_tokens: 4096,
temperature: 0.7,
},
registry: RegistryConfig {
url: "https://crates.io".to_string(),
cache_ttl: 3600,
},
logging: LoggingConfig {
level: "info".to_string(),
format: "json".to_string(),
},
}
}
}