diff --git a/Rocket.toml b/Rocket.toml index 313e173a..dea7ede3 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,3 +1,3 @@ [global] address = "0.0.0.0" -port = 8000 +port = 8000 \ No newline at end of file diff --git a/server/Settings.toml b/server/Settings.toml index 1e0e8d34..29142e42 100644 --- a/server/Settings.toml +++ b/server/Settings.toml @@ -3,4 +3,4 @@ network = "testnet" lockheight_init = 1000 lh_decrement = 10 connection_string = "postgresql://postgres:postgres@localhost/mercury" -batch_timeout = 120 # seconds +batch_timeout = 120 # seconds \ No newline at end of file diff --git a/server/src/server_config.rs b/server/src/server_config.rs index 8548a954..e0ca67f1 100644 --- a/server/src/server_config.rs +++ b/server/src/server_config.rs @@ -1,4 +1,4 @@ -use config::Config; +use config::{Config as ConfigRs, Environment, File}; use serde::{Serialize, Deserialize}; use std::env; @@ -19,10 +19,45 @@ pub struct ServerConfig { pub batch_timeout: u32, } +impl Default for ServerConfig { + fn default() -> ServerConfig { + ServerConfig { + lockbox: None, + network: String::from("regtest"), + lockheight_init: 10000, + lh_decrement: 100, + connection_string: String::from("postgresql://postgres:postgres@localhost/mercury"), + batch_timeout: 120, + } + } +} + +impl From for ServerConfig { + fn from(config: ConfigRs) -> Self { + ServerConfig { + lockbox: config.get::>("lockbox").unwrap_or(None), + network: config.get::("network").unwrap_or_else(|_| String::new()), + lockheight_init: config.get::("lockheight_init").unwrap_or(0), + lh_decrement: config.get::("lh_decrement").unwrap_or(0), + connection_string: config.get::("connection_string").unwrap_or_else(|_| String::new()), + batch_timeout: config.get::("batch_timeout").unwrap_or(0), + } + } +} + impl ServerConfig { pub fn load() -> Self { - let settings = Config::builder() - .add_source(config::File::with_name("Settings")) + let mut conf_rs = ConfigRs::default(); + let _ = conf_rs + // First merge struct default config + .merge(ConfigRs::try_from(&ServerConfig::default()).unwrap()); + // Override with settings in file Settings.toml if exists + conf_rs.merge(File::with_name("Settings").required(false)); + // Override with settings in file Rocket.toml if exists + conf_rs.merge(File::with_name("Rocket").required(false)); + + let settings = ConfigRs::builder() + .add_source(File::with_name("Settings")) .build() .unwrap(); diff --git a/token-server/Settings.toml b/token-server/Settings.toml index ac878d0c..48d793de 100644 --- a/token-server/Settings.toml +++ b/token-server/Settings.toml @@ -2,4 +2,4 @@ processor_url = "http://0.0.0.0:18080" api_key = "aaaaa" fee = 10000 delay = 3600 -connection_string = "postgresql://postgres:postgres@localhost/mercury" +connection_string = "postgresql://postgres:postgres@localhost/mercury" \ No newline at end of file diff --git a/token-server/src/server_config.rs b/token-server/src/server_config.rs index 224701c9..65cb54f8 100644 --- a/token-server/src/server_config.rs +++ b/token-server/src/server_config.rs @@ -1,4 +1,4 @@ -use config::Config; +use config::{Config as ConfigRs, Environment, File}; use serde::{Serialize, Deserialize}; use std::env; @@ -17,10 +17,43 @@ pub struct ServerConfig { pub connection_string: String, } +impl Default for ServerConfig { + fn default() -> ServerConfig { + ServerConfig { + processor_url: String::from("http://0.0.0.0:18080"), + api_key: String::from("aaaaa"), + fee: String::from("10000"), + delay: 3600, + connection_string: String::from("postgresql://postgres:postgres@localhost/mercury"), + } + } +} + +impl From for ServerConfig { + fn from(config: ConfigRs) -> Self { + ServerConfig { + processor_url: config.get::("processor_url").unwrap_or_else(|_| String::new()), + api_key: config.get::("api_key").unwrap_or_else(|_| String::new()), + fee: config.get::("fee").unwrap_or_else(|_| String::new()), + delay: config.get::("delay").unwrap_or(0), + connection_string: config.get::("connection_string").unwrap_or_else(|_| String::new()), + } + } +} + impl ServerConfig { pub fn load() -> Self { - let settings = Config::builder() - .add_source(config::File::with_name("Settings")) + let mut conf_rs = ConfigRs::default(); + let _ = conf_rs + // First merge struct default config + .merge(ConfigRs::try_from(&ServerConfig::default()).unwrap()); + // Override with settings in file Settings.toml if exists + conf_rs.merge(File::with_name("Settings").required(false)); + // Override with settings in file Rocket.toml if exists + conf_rs.merge(File::with_name("Rocket").required(false)); + + let settings = ConfigRs::builder() + .add_source(File::with_name("Settings")) .build() .unwrap();