Skip to content

Commit

Permalink
fix: config for mercury and token server
Browse files Browse the repository at this point in the history
  • Loading branch information
DhananjayPurohit committed May 24, 2024
1 parent 1a4dc80 commit 2d93785
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Rocket.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[global]
address = "0.0.0.0"
port = 8000
port = 8000
2 changes: 1 addition & 1 deletion server/Settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 38 additions & 3 deletions server/src/server_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use config::Config;
use config::{Config as ConfigRs, Environment, File};
use serde::{Serialize, Deserialize};
use std::env;

Expand All @@ -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<ConfigRs> for ServerConfig {
fn from(config: ConfigRs) -> Self {
ServerConfig {
lockbox: config.get::<Option<String>>("lockbox").unwrap_or(None),
network: config.get::<String>("network").unwrap_or_else(|_| String::new()),
lockheight_init: config.get::<u32>("lockheight_init").unwrap_or(0),
lh_decrement: config.get::<u32>("lh_decrement").unwrap_or(0),
connection_string: config.get::<String>("connection_string").unwrap_or_else(|_| String::new()),
batch_timeout: config.get::<u32>("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();

Expand Down
2 changes: 1 addition & 1 deletion token-server/Settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
39 changes: 36 additions & 3 deletions token-server/src/server_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use config::Config;
use config::{Config as ConfigRs, Environment, File};
use serde::{Serialize, Deserialize};
use std::env;

Expand All @@ -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<ConfigRs> for ServerConfig {
fn from(config: ConfigRs) -> Self {
ServerConfig {
processor_url: config.get::<String>("processor_url").unwrap_or_else(|_| String::new()),
api_key: config.get::<String>("api_key").unwrap_or_else(|_| String::new()),
fee: config.get::<String>("fee").unwrap_or_else(|_| String::new()),
delay: config.get::<u64>("delay").unwrap_or(0),
connection_string: config.get::<String>("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();

Expand Down

0 comments on commit 2d93785

Please sign in to comment.