Skip to content

Commit 5f38982

Browse files
committed
Optionally configure bind address via env var
This allows users to configure VSS-Server entirely through environment variables, without a configuration file. We hence remove the requirement that a path to a configuration file always be passed as an argument.
1 parent 5a7b6b1 commit 5f38982

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

rust/server/src/main.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ mod vss_service;
2828

2929
fn main() {
3030
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-
}
3531

36-
let config = util::config::load_configuration(&args[1]).unwrap_or_else(|e| {
37-
eprintln!("Failed to load configuration: {}", e);
38-
std::process::exit(-1);
39-
});
32+
let config =
33+
util::config::load_configuration(args.get(1).map(|s| s.as_str())).unwrap_or_else(|e| {
34+
eprintln!("Failed to load configuration: {}", e);
35+
std::process::exit(-1);
36+
});
4037

4138
let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() {
4239
Ok(runtime) => Arc::new(runtime),

rust/server/src/util/config.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
use serde::Deserialize;
22
use std::net::SocketAddr;
33

4-
#[derive(Deserialize)]
4+
#[derive(Deserialize, Default)]
55
struct Config {
6-
server_config: ServerConfig,
6+
server_config: Option<ServerConfig>,
77
jwt_auth_config: Option<JwtAuthConfig>,
88
postgresql_config: Option<PostgreSQLConfig>,
99
}
1010

1111
#[derive(Deserialize)]
1212
struct ServerConfig {
13-
bind_address: SocketAddr,
13+
bind_address: Option<SocketAddr>,
1414
}
1515

1616
#[derive(Deserialize)]
1717
struct JwtAuthConfig {
1818
rsa_pem: Option<String>,
1919
}
2020

21+
const BIND_ADDR_VAR: &str = "VSS_BIND_ADDRESS";
2122
const JWT_RSA_PEM_VAR: &str = "VSS_JWT_RSA_PEM";
2223
const PSQL_USER_VAR: &str = "VSS_PSQL_USERNAME";
2324
const PSQL_PASS_VAR: &str = "VSS_PSQL_PASSWORD";
@@ -51,12 +52,19 @@ pub(crate) struct Configuration {
5152
pub(crate) tls_config: Option<Option<String>>,
5253
}
5354

54-
pub(crate) fn load_configuration(config_file_path: &str) -> Result<Configuration, String> {
55-
let config_file = std::fs::read_to_string(config_file_path)
56-
.map_err(|e| format!("Failed to read configuration file: {}", e))?;
57-
let Config { server_config: ServerConfig { bind_address }, jwt_auth_config, postgresql_config } =
55+
pub(crate) fn load_configuration(config_file_path: Option<&str>) -> Result<Configuration, String> {
56+
let Config { server_config, jwt_auth_config, postgresql_config } = if config_file_path.is_some()
57+
&& std::fs::exists(config_file_path.expect("config file path is some"))
58+
.map_err(|e| format!("Failed to check presence of configuration file: {}", e))?
59+
{
60+
let config_file =
61+
std::fs::read_to_string(config_file_path.expect("config file path is some"))
62+
.map_err(|e| format!("Failed to read configuration file: {}", e))?;
5863
toml::from_str(&config_file)
59-
.map_err(|e| format!("Failed to parse configuration file: {}", e))?;
64+
.map_err(|e| format!("Failed to parse configuration file: {}", e))?
65+
} else {
66+
Config::default() // All fields are set to `None`
67+
};
6068

6169
macro_rules! read_env {
6270
($env_var:expr) => {
@@ -82,6 +90,20 @@ pub(crate) fn load_configuration(config_file_path: &str) -> Result<Configuration
8290
};
8391
}
8492

93+
let bind_address_env = read_env!(BIND_ADDR_VAR)
94+
.map(|addr| {
95+
addr.parse().map_err(|e| {
96+
format!("Unable to parse the bind address environment variable: {}", e)
97+
})
98+
})
99+
.transpose()?;
100+
let bind_address = read_config!(
101+
bind_address_env,
102+
server_config.and_then(|c| c.bind_address),
103+
"VSS server bind address",
104+
BIND_ADDR_VAR
105+
);
106+
85107
let rsa_pem_env = read_env!(JWT_RSA_PEM_VAR);
86108
let rsa_pem = rsa_pem_env.or(jwt_auth_config.and_then(|config| config.rsa_pem));
87109

0 commit comments

Comments
 (0)