Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rust/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
27 changes: 24 additions & 3 deletions rust/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = std::env::args().collect();
if args.len() != 2 {
eprintln!("Usage: {} <config-file-path>", 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),
Expand All @@ -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(),
);
Expand Down
48 changes: 48 additions & 0 deletions rust/server/src/util/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use serde::Deserialize;

#[derive(Deserialize)]
pub(crate) struct Config {
pub(crate) server_config: ServerConfig,
pub(crate) postgresql_config: Option<PostgreSQLConfig>,
}

#[derive(Deserialize)]
pub(crate) struct ServerConfig {
pub(crate) host: String,
pub(crate) port: u16,
}

#[derive(Deserialize)]
pub(crate) struct PostgreSQLConfig {
pub(crate) username: Option<String>, // Optional in TOML, can be overridden by env
pub(crate) password: Option<String>, // 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<Config, Box<dyn std::error::Error>> {
let config_str = std::fs::read_to_string(config_path)?;
let config: Config = toml::from_str(&config_str)?;
Ok(config)
}
1 change: 1 addition & 0 deletions rust/server/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod config;
10 changes: 10 additions & 0 deletions rust/server/vss-server-config.toml
Original file line number Diff line number Diff line change
@@ -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"