From 0be84e7c52d6cd31c00f728df3fdd84e474519f6 Mon Sep 17 00:00:00 2001 From: G8XSU <3442979+G8XSU@users.noreply.github.com> Date: Fri, 14 Mar 2025 09:56:59 -0700 Subject: [PATCH] Add file-based config support. --- rust/server/Cargo.toml | 2 ++ rust/server/src/main.rs | 27 +++++++++++++++-- rust/server/src/util/config.rs | 48 ++++++++++++++++++++++++++++++ rust/server/src/util/mod.rs | 1 + rust/server/vss-server-config.toml | 10 +++++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 rust/server/src/util/config.rs create mode 100644 rust/server/src/util/mod.rs create mode 100644 rust/server/vss-server-config.toml diff --git a/rust/server/Cargo.toml b/rust/server/Cargo.toml index 8674aa8..5eec5a6 100644 --- a/rust/server/Cargo.toml +++ b/rust/server/Cargo.toml @@ -13,3 +13,5 @@ hyper-util = { version = "0.1", default-features = false, features = ["server-gr tokio = { version = "1.38.0", default-features = false, features = ["time", "signal", "rt-multi-thread", "macros"] } prost = { version = "0.11.6", default-features = false, features = ["std"] } bytes = "1.4.0" +serde = { version = "1.0.203", default-features = false, features = ["derive"] } +toml = { version = "0.8.9", default-features = false, features = ["parse"] } diff --git a/rust/server/src/main.rs b/rust/server/src/main.rs index 2b13a19..849dcee 100644 --- a/rust/server/src/main.rs +++ b/rust/server/src/main.rs @@ -23,11 +23,32 @@ use api::kv_store::KvStore; use impls::postgres_store::PostgresBackendImpl; use std::sync::Arc; +pub(crate) mod util; pub(crate) mod vss_service; fn main() { - // Define the address to bind the server to - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let args: Vec = std::env::args().collect(); + if args.len() != 2 { + eprintln!("Usage: {} ", args[0]); + std::process::exit(1); + } + + let config = match util::config::load_config(&args[1]) { + Ok(cfg) => cfg, + Err(e) => { + eprintln!("Failed to load configuration: {}", e); + std::process::exit(1); + }, + }; + + let addr: SocketAddr = + match format!("{}:{}", config.server_config.host, config.server_config.port).parse() { + Ok(addr) => addr, + Err(e) => { + eprintln!("Invalid host/port configuration: {}", e); + std::process::exit(1); + }, + }; let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() { Ok(runtime) => Arc::new(runtime), @@ -47,7 +68,7 @@ fn main() { }; let authorizer = Arc::new(NoopAuthorizer {}); let store = Arc::new( - PostgresBackendImpl::new("postgresql://postgres:postgres@localhost:5432/postgres") + PostgresBackendImpl::new(&config.postgresql_config.expect("PostgreSQLConfig must be defined in config file.").to_connection_string()) .await .unwrap(), ); diff --git a/rust/server/src/util/config.rs b/rust/server/src/util/config.rs new file mode 100644 index 0000000..e75f972 --- /dev/null +++ b/rust/server/src/util/config.rs @@ -0,0 +1,48 @@ +use serde::Deserialize; + +#[derive(Deserialize)] +pub(crate) struct Config { + pub(crate) server_config: ServerConfig, + pub(crate) postgresql_config: Option, +} + +#[derive(Deserialize)] +pub(crate) struct ServerConfig { + pub(crate) host: String, + pub(crate) port: u16, +} + +#[derive(Deserialize)] +pub(crate) struct PostgreSQLConfig { + pub(crate) username: Option, // Optional in TOML, can be overridden by env + pub(crate) password: Option, // Optional in TOML, can be overridden by env + pub(crate) host: String, + pub(crate) port: u16, + pub(crate) database: String, +} + +impl PostgreSQLConfig { + pub(crate) fn to_connection_string(&self) -> String { + let username_env = std::env::var("VSS_POSTGRESQL_USERNAME"); + let username = username_env.as_ref() + .ok() + .or_else(|| self.username.as_ref()) + .expect("PostgreSQL database username must be provided in config or env var VSS_POSTGRESQL_USERNAME must be set."); + let password_env = std::env::var("VSS_POSTGRESQL_PASSWORD"); + let password = password_env.as_ref() + .ok() + .or_else(|| self.password.as_ref()) + .expect("PostgreSQL database password must be provided in config or env var VSS_POSTGRESQL_PASSWORD must be set."); + + format!( + "postgresql://{}:{}@{}:{}/{}", + username, password, self.host, self.port, self.database + ) + } +} + +pub(crate) fn load_config(config_path: &str) -> Result> { + let config_str = std::fs::read_to_string(config_path)?; + let config: Config = toml::from_str(&config_str)?; + Ok(config) +} diff --git a/rust/server/src/util/mod.rs b/rust/server/src/util/mod.rs new file mode 100644 index 0000000..30e489d --- /dev/null +++ b/rust/server/src/util/mod.rs @@ -0,0 +1 @@ +pub(crate) mod config; diff --git a/rust/server/vss-server-config.toml b/rust/server/vss-server-config.toml new file mode 100644 index 0000000..8a013b5 --- /dev/null +++ b/rust/server/vss-server-config.toml @@ -0,0 +1,10 @@ +[server_config] +host = "127.0.0.1" +port = 8080 + +[postgresql_config] +username = "postgres" # Optional in TOML, can be overridden by env var `VSS_POSTGRESQL_USERNAME` +password = "postgres" # Optional in TOML, can be overridden by env var `VSS_POSTGRESQL_PASSWORD` +host = "localhost" +port = 5432 +database = "postgres"