11use serde:: Deserialize ;
22use std:: net:: SocketAddr ;
33
4- #[ derive( Deserialize ) ]
4+ #[ derive( Deserialize , Default ) ]
55struct 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 ) ]
1212struct ServerConfig {
13- bind_address : SocketAddr ,
13+ bind_address : Option < SocketAddr > ,
1414}
1515
1616#[ derive( Deserialize ) ]
1717struct JwtAuthConfig {
1818 rsa_pem : Option < String > ,
1919}
2020
21+ const BIND_ADDR_VAR : & str = "VSS_BIND_ADDRESS" ;
2122const JWT_RSA_PEM_VAR : & str = "VSS_JWT_RSA_PEM" ;
2223const PSQL_USER_VAR : & str = "VSS_PSQL_USERNAME" ;
2324const 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