1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct ServerConfig {
6 pub host: String,
7 pub port: u16,
8 pub data_dir: PathBuf,
9 pub log_level: String,
10 #[serde(default)]
11 pub api_key: Option<String>,
12 pub max_connections: usize,
13 pub query_timeout_ms: u64,
14}
15
16impl Default for ServerConfig {
17 fn default() -> Self {
18 Self {
19 host: "127.0.0.1".to_string(),
20 port: 6333,
21 data_dir: PathBuf::from("./data"),
22 log_level: "info".to_string(),
23 api_key: None,
24 max_connections: 100,
25 query_timeout_ms: 5000,
26 }
27 }
28}
29
30impl ServerConfig {
31 pub fn from_env_and_file(path: Option<&str>) -> anyhow::Result<Self> {
32 let mut builder = ::config::Config::builder()
33 .set_default("host", "127.0.0.1")?
34 .set_default("port", 6333_i64)?
35 .set_default("data_dir", "./data")?
36 .set_default("log_level", "info")?
37 .set_default("max_connections", 100_i64)?
38 .set_default("query_timeout_ms", 5000_i64)?;
39
40 if let Some(p) = path {
41 builder = builder.add_source(::config::File::with_name(p).required(false));
42 }
43
44 builder = builder.add_source(::config::Environment::with_prefix("VECDB").separator("__"));
45
46 Ok(builder.build()?.try_deserialize()?)
47 }
48}