Skip to content

Commit f8700aa

Browse files
committed
feat: allow to inject configuration from env var
You can use an env var to pass the configuration instead of using the configuration file in the root folder `config.toml` ``` TORRUST_TRACKER_CONFIG=$(cat config.toml) TORRUST_TRACKER_CONFIG=`cat config.toml` cargo run ``` This allow the applciation to be executed in dockerized environments whithout needing to mount a file or volume for the configuration.
1 parent 3098ed2 commit f8700aa

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/config.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::collections::HashMap;
2-
use std::fs;
32
use std::net::IpAddr;
43
use std::path::Path;
54
use std::str::FromStr;
5+
use std::{env, fs};
66

7-
use config::{Config, ConfigError, File};
7+
use config::{Config, ConfigError, File, FileFormat};
88
use serde::{Deserialize, Serialize};
99
use serde_with::{serde_as, NoneAsEmptyString};
1010
use {std, toml};
@@ -164,7 +164,7 @@ impl Configuration {
164164
let config = Configuration::default();
165165
config.save_to_file(path)?;
166166
return Err(Error::Message(
167-
"Please edit the config.TOML in the root folder and restart the tracker.".to_string(),
167+
"Please edit the config.TOML and restart the tracker.".to_string(),
168168
));
169169
}
170170

@@ -173,6 +173,26 @@ impl Configuration {
173173
Ok(torrust_config)
174174
}
175175

176+
/// # Errors
177+
///
178+
/// Will return `Err` if the environment variable does not exist or has a bad configuration.
179+
pub fn load_from_env_var(config_env_var_name: &str) -> Result<Configuration, Error> {
180+
match env::var(config_env_var_name) {
181+
Ok(config_toml) => {
182+
let config_builder = Config::builder()
183+
.add_source(File::from_str(&config_toml, FileFormat::Toml))
184+
.build()
185+
.map_err(Error::ConfigError)?;
186+
let config = config_builder.try_deserialize().map_err(Error::ConfigError)?;
187+
Ok(config)
188+
}
189+
Err(_) => Err(Error::Message(format!(
190+
"No environment variable for configuration found: {}",
191+
&config_env_var_name
192+
))),
193+
}
194+
}
195+
176196
/// # Errors
177197
///
178198
/// Will return `Err` if `filename` does not exist or the user does not have

src/main.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::env;
12
use std::sync::Arc;
23

34
use log::info;
@@ -7,7 +8,8 @@ use torrust_tracker::{ephemeral_instance_keys, logging, setup, static_time, trac
78

89
#[tokio::main]
910
async fn main() {
10-
const CONFIG_PATH: &str = "config.toml";
11+
const CONFIG_PATH: &str = "./config.toml";
12+
const CONFIG_ENV_VAR_NAME: &str = "TORRUST_TRACKER_CONFIG";
1113

1214
// Set the time of Torrust app starting
1315
lazy_static::initialize(&static_time::TIME_AT_APP_START);
@@ -16,11 +18,12 @@ async fn main() {
1618
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
1719

1820
// Initialize Torrust config
19-
let config = match Configuration::load_from_file(CONFIG_PATH) {
20-
Ok(config) => Arc::new(config),
21-
Err(error) => {
22-
panic!("{}", error)
23-
}
21+
let config = if env::var(CONFIG_ENV_VAR_NAME).is_ok() {
22+
println!("Loading configuration from env var {CONFIG_ENV_VAR_NAME}");
23+
Arc::new(Configuration::load_from_env_var(CONFIG_ENV_VAR_NAME).unwrap())
24+
} else {
25+
println!("Loading configuration from config file {CONFIG_PATH}");
26+
Arc::new(Configuration::load_from_file(CONFIG_PATH).unwrap())
2427
};
2528

2629
// Initialize statistics

0 commit comments

Comments
 (0)