Skip to content

Commit 9d7326a

Browse files
authored
Merge pull request #46 from G8XSU/config-file
Add file-based config support.
2 parents 59e0d58 + 0be84e7 commit 9d7326a

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

rust/server/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ hyper-util = { version = "0.1", default-features = false, features = ["server-gr
1313
tokio = { version = "1.38.0", default-features = false, features = ["time", "signal", "rt-multi-thread", "macros"] }
1414
prost = { version = "0.11.6", default-features = false, features = ["std"] }
1515
bytes = "1.4.0"
16+
serde = { version = "1.0.203", default-features = false, features = ["derive"] }
17+
toml = { version = "0.8.9", default-features = false, features = ["parse"] }

rust/server/src/main.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,32 @@ use api::kv_store::KvStore;
2323
use impls::postgres_store::PostgresBackendImpl;
2424
use std::sync::Arc;
2525

26+
pub(crate) mod util;
2627
pub(crate) mod vss_service;
2728

2829
fn main() {
29-
// Define the address to bind the server to
30-
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
30+
let args: Vec<String> = std::env::args().collect();
31+
if args.len() != 2 {
32+
eprintln!("Usage: {} <config-file-path>", args[0]);
33+
std::process::exit(1);
34+
}
35+
36+
let config = match util::config::load_config(&args[1]) {
37+
Ok(cfg) => cfg,
38+
Err(e) => {
39+
eprintln!("Failed to load configuration: {}", e);
40+
std::process::exit(1);
41+
},
42+
};
43+
44+
let addr: SocketAddr =
45+
match format!("{}:{}", config.server_config.host, config.server_config.port).parse() {
46+
Ok(addr) => addr,
47+
Err(e) => {
48+
eprintln!("Invalid host/port configuration: {}", e);
49+
std::process::exit(1);
50+
},
51+
};
3152

3253
let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() {
3354
Ok(runtime) => Arc::new(runtime),
@@ -47,7 +68,7 @@ fn main() {
4768
};
4869
let authorizer = Arc::new(NoopAuthorizer {});
4970
let store = Arc::new(
50-
PostgresBackendImpl::new("postgresql://postgres:postgres@localhost:5432/postgres")
71+
PostgresBackendImpl::new(&config.postgresql_config.expect("PostgreSQLConfig must be defined in config file.").to_connection_string())
5172
.await
5273
.unwrap(),
5374
);

rust/server/src/util/config.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use serde::Deserialize;
2+
3+
#[derive(Deserialize)]
4+
pub(crate) struct Config {
5+
pub(crate) server_config: ServerConfig,
6+
pub(crate) postgresql_config: Option<PostgreSQLConfig>,
7+
}
8+
9+
#[derive(Deserialize)]
10+
pub(crate) struct ServerConfig {
11+
pub(crate) host: String,
12+
pub(crate) port: u16,
13+
}
14+
15+
#[derive(Deserialize)]
16+
pub(crate) struct PostgreSQLConfig {
17+
pub(crate) username: Option<String>, // Optional in TOML, can be overridden by env
18+
pub(crate) password: Option<String>, // Optional in TOML, can be overridden by env
19+
pub(crate) host: String,
20+
pub(crate) port: u16,
21+
pub(crate) database: String,
22+
}
23+
24+
impl PostgreSQLConfig {
25+
pub(crate) fn to_connection_string(&self) -> String {
26+
let username_env = std::env::var("VSS_POSTGRESQL_USERNAME");
27+
let username = username_env.as_ref()
28+
.ok()
29+
.or_else(|| self.username.as_ref())
30+
.expect("PostgreSQL database username must be provided in config or env var VSS_POSTGRESQL_USERNAME must be set.");
31+
let password_env = std::env::var("VSS_POSTGRESQL_PASSWORD");
32+
let password = password_env.as_ref()
33+
.ok()
34+
.or_else(|| self.password.as_ref())
35+
.expect("PostgreSQL database password must be provided in config or env var VSS_POSTGRESQL_PASSWORD must be set.");
36+
37+
format!(
38+
"postgresql://{}:{}@{}:{}/{}",
39+
username, password, self.host, self.port, self.database
40+
)
41+
}
42+
}
43+
44+
pub(crate) fn load_config(config_path: &str) -> Result<Config, Box<dyn std::error::Error>> {
45+
let config_str = std::fs::read_to_string(config_path)?;
46+
let config: Config = toml::from_str(&config_str)?;
47+
Ok(config)
48+
}

rust/server/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod config;

rust/server/vss-server-config.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[server_config]
2+
host = "127.0.0.1"
3+
port = 8080
4+
5+
[postgresql_config]
6+
username = "postgres" # Optional in TOML, can be overridden by env var `VSS_POSTGRESQL_USERNAME`
7+
password = "postgres" # Optional in TOML, can be overridden by env var `VSS_POSTGRESQL_PASSWORD`
8+
host = "localhost"
9+
port = 5432
10+
database = "postgres"

0 commit comments

Comments
 (0)