|
| 1 | +use crate::constants::{ |
| 2 | + self, DEFAULT_PORT, ENV_DATA_PATH, ENV_DIMENSION, ENV_INDEX_TYPE, ENV_LOGGING, ENV_PORT, |
| 3 | + ENV_ROOT_PASSWORD, ENV_STORAGE_TYPE, |
| 4 | +}; |
| 5 | +use crate::errors; |
| 6 | +use api; |
| 7 | +use dotenv::dotenv; |
| 8 | +use index::IndexType; |
| 9 | +use std::net::SocketAddr; |
| 10 | +use std::path::PathBuf; |
| 11 | +use std::{env, fs}; |
| 12 | +use storage; |
| 13 | +use tempfile::tempdir; |
| 14 | +use tracing::{Level, event}; |
| 15 | + |
| 16 | +pub struct GRPCServerConfig { |
| 17 | + pub addr: SocketAddr, |
| 18 | + pub root_password: String, |
| 19 | + pub db_config: api::DbConfig, |
| 20 | + pub logging: bool, |
| 21 | +} |
| 22 | + |
| 23 | +impl GRPCServerConfig { |
| 24 | + pub fn load_config() -> Result<GRPCServerConfig, Box<dyn std::error::Error>> { |
| 25 | + dotenv().ok(); |
| 26 | + |
| 27 | + // fetch server host; default to localhost if not defined |
| 28 | + let host = env::var(constants::ENV_HOST) |
| 29 | + .inspect_err(|_| { |
| 30 | + event!(Level::WARN, "Host not defined, defaulting to 'localhost'"); |
| 31 | + }) |
| 32 | + .unwrap_or("127.0.0.1".to_string()); |
| 33 | + |
| 34 | + // fetch server port; default to 8080 if not defined |
| 35 | + let port: u32 = env::var(ENV_PORT) |
| 36 | + .inspect_err(|_| { |
| 37 | + event!( |
| 38 | + Level::WARN, |
| 39 | + "Port not defined, defaulting to {}", |
| 40 | + DEFAULT_PORT |
| 41 | + ); |
| 42 | + }) |
| 43 | + .unwrap_or(DEFAULT_PORT.to_string()) |
| 44 | + .parse() |
| 45 | + .unwrap_or(DEFAULT_PORT.parse::<u32>().unwrap()); |
| 46 | + |
| 47 | + // fetch server root password; return err if not defined |
| 48 | + let root_password = env::var(ENV_ROOT_PASSWORD).map_err(|_| { |
| 49 | + errors::ConfigError::MissingRequiredEnvVar(ENV_ROOT_PASSWORD.to_string()) |
| 50 | + })?; |
| 51 | + |
| 52 | + // fetch server storage type |
| 53 | + let storage_type_str = env::var(ENV_STORAGE_TYPE) |
| 54 | + .inspect_err(|_| { |
| 55 | + event!( |
| 56 | + Level::WARN, |
| 57 | + "Storage Type not defined, defaulting to InMemory" |
| 58 | + ) |
| 59 | + }) |
| 60 | + .unwrap_or_default(); |
| 61 | + let storage_type = match storage_type_str.as_str() { |
| 62 | + "inmemory" => storage::StorageType::InMemory, |
| 63 | + "rocksdb" => storage::StorageType::RocksDb, |
| 64 | + _ => storage::StorageType::InMemory, // default to InMemory if not specified |
| 65 | + }; |
| 66 | + |
| 67 | + // fetch server index type |
| 68 | + let index_type_str = env::var(ENV_INDEX_TYPE) |
| 69 | + .inspect_err(|_| event!(Level::WARN, "Index Type not defined, defaulting to flat")) |
| 70 | + .unwrap_or("flat".to_string()) |
| 71 | + .to_lowercase(); |
| 72 | + let index_type = match index_type_str.as_str() { |
| 73 | + "flat" => IndexType::Flat, |
| 74 | + "kdtree" => IndexType::KDTree, |
| 75 | + "hnsw" => IndexType::HNSW, |
| 76 | + _ => IndexType::Flat, // default to Flat if not specified |
| 77 | + }; |
| 78 | + |
| 79 | + // fetch dimension size |
| 80 | + let dimension: usize = env::var(ENV_DIMENSION) |
| 81 | + .map_err(|_| errors::ConfigError::MissingRequiredEnvVar(ENV_DIMENSION.to_string()))? |
| 82 | + .parse() |
| 83 | + .map_err(|_| errors::ConfigError::InvalidDimension)?; |
| 84 | + |
| 85 | + // fetch data path; create tempdir if not specified |
| 86 | + let data_path: PathBuf; |
| 87 | + if let Ok(data_path_str) = env::var(ENV_DATA_PATH) { |
| 88 | + data_path = PathBuf::from(data_path_str); |
| 89 | + fs::create_dir_all(&data_path).map_err(|_| errors::ConfigError::InvalidDataPath)?; |
| 90 | + } else { |
| 91 | + let tempbuf = tempdir().unwrap().path().to_path_buf().join("vectordb"); |
| 92 | + fs::create_dir_all(&tempbuf)?; |
| 93 | + event!( |
| 94 | + Level::WARN, |
| 95 | + "Data Path not specified, using temporary directory: {:?}", |
| 96 | + tempbuf.clone() |
| 97 | + ); |
| 98 | + data_path = tempbuf; |
| 99 | + } |
| 100 | + |
| 101 | + // create db config for api |
| 102 | + let db_config = api::DbConfig { |
| 103 | + storage_type, |
| 104 | + index_type, |
| 105 | + data_path, |
| 106 | + dimension, |
| 107 | + }; |
| 108 | + |
| 109 | + // create socket address for grpc server |
| 110 | + let addr: SocketAddr = format!("{}:{}", host, port).parse()?; |
| 111 | + |
| 112 | + // check if logging is enabled |
| 113 | + let mut logging: bool = true; // default to logging enabled |
| 114 | + if let Ok(logging_str) = env::var(ENV_LOGGING) { |
| 115 | + logging = logging_str.parse().unwrap_or(true); |
| 116 | + } |
| 117 | + |
| 118 | + Ok(GRPCServerConfig { |
| 119 | + addr, |
| 120 | + root_password, |
| 121 | + db_config, |
| 122 | + logging, |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments