add application configuration to replace Rocket.toml
This commit is contained in:
@ -1,54 +1,53 @@
|
|||||||
// Kind thanks to Alex Wennerberg (https://alexwennerberg.com/) for writing
|
//! Define the configuration parameters for the web application.
|
||||||
// this code and offered it under the 0BSD (BDS 0-Clause) license.
|
//!
|
||||||
|
//! Sets default values and updates them if the corresponding environment
|
||||||
|
//! variables have been set.
|
||||||
|
|
||||||
use std::fs::File;
|
use std::env;
|
||||||
use std::io::{self, BufRead};
|
|
||||||
use std::path::Path;
|
// environment variable keys to check for
|
||||||
|
const ENV_VARS: [&str; 4] = ["STANDALONE_MODE", "DISABLE_AUTH", "ADDR", "PORT"];
|
||||||
|
|
||||||
// Ini-like key=value configuration with global config only (no subsections).
|
|
||||||
#[derive(Debug, PartialEq)]
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub disable_auth: bool,
|
|
||||||
pub standalone_mode: bool,
|
pub standalone_mode: bool,
|
||||||
|
pub disable_auth: bool,
|
||||||
|
pub addr: String,
|
||||||
|
pub port: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
disable_auth: false,
|
|
||||||
standalone_mode: true,
|
standalone_mode: true,
|
||||||
|
disable_auth: false,
|
||||||
|
addr: "127.0.0.1".to_string(),
|
||||||
|
port: "8000".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn match_kv(&mut self, key: &str, value: &str) {
|
pub fn new() -> Config {
|
||||||
match key {
|
// define default config values
|
||||||
"disable_auth" => self.disable_auth = value.parse().unwrap(),
|
let mut config = Config::default();
|
||||||
"standalone_mode" => self.standalone_mode = value.parse().unwrap(),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
// check for the environment variables in our config
|
||||||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config, std::io::Error> {
|
for key in ENV_VARS {
|
||||||
let file = File::open(path)?;
|
// if a variable (key) has been set, check the value
|
||||||
let mut conf = Config::default();
|
if let Ok(val) = env::var(key) {
|
||||||
|
// if the value is of the correct type, update the config value
|
||||||
for l in io::BufReader::new(file).lines() {
|
match key {
|
||||||
let line = l?;
|
"STANDALONE_MODE" if val.as_str() == "true" => config.standalone_mode = true,
|
||||||
if line.is_empty() {
|
"STANDALONE_MODE" if val.as_str() == "false" => config.standalone_mode = false,
|
||||||
continue;
|
"DISABLE_AUTH" if val.as_str() == "true" => config.disable_auth = true,
|
||||||
}
|
"DISABLE_AUTH" if val.as_str() == "false" => config.disable_auth = false,
|
||||||
if let Some(i) = line.find('=') {
|
"ADDR" => config.addr = val,
|
||||||
let key = &line[..i];
|
"PORT" => config.port = val,
|
||||||
let value = &line[i + 1..];
|
_ => (),
|
||||||
conf.match_kv(key, value);
|
}
|
||||||
} else {
|
|
||||||
// panic!("Invalid config")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(conf)
|
|
||||||
|
config
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user