42 lines
1.4 KiB
Rust
42 lines
1.4 KiB
Rust
use anyhow::{bail, Result};
|
|
use log::info;
|
|
use std::env;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Config {
|
|
pub addr: String,
|
|
pub port: u16,
|
|
pub database: String,
|
|
pub threads: Option<u16>,
|
|
}
|
|
|
|
impl Config {
|
|
/// Loads the configuration from environment variables
|
|
pub fn from_env() -> Result<Self> {
|
|
Ok(Self {
|
|
addr: env::var("CR_ADDRESS").unwrap_or_else(|_| {
|
|
info!("Using default address 0.0.0.0");
|
|
"0.0.0.0".to_string()
|
|
}),
|
|
port: match env::var("CR_PORT") {
|
|
Ok(str) => match str.parse() {
|
|
Ok(port) => port,
|
|
Err(_) => bail!("Failed to parse environment variable CR_PORT: '{str}' is not a valid port number!"),
|
|
},
|
|
Err(_) => { info!("Using default port 42069"); 42069 }
|
|
},
|
|
database: match env::var("CR_DATABASE") {
|
|
Ok(db) => db,
|
|
Err(_) => bail!("The environment variable CR_DATABASE is required!"),
|
|
},
|
|
threads: match env::var("CR_THREADS") {
|
|
Ok(str) => match str.parse() {
|
|
Ok(threads) => Some(threads),
|
|
Err(_) => bail!("Failed to parse environment variable CR_THREAD: '{str}' is not a valid number!"),
|
|
},
|
|
Err(_) => None
|
|
},
|
|
})
|
|
}
|
|
}
|