34 lines
1017 B
Rust
34 lines
1017 B
Rust
|
use anyhow::{bail, Result};
|
||
|
use log::info;
|
||
|
use std::env;
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct Config {
|
||
|
addr: String,
|
||
|
port: u16,
|
||
|
database: String,
|
||
|
}
|
||
|
|
||
|
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!"),
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|