29 lines
812 B
Rust
29 lines
812 B
Rust
use {std::env, thiserror::Error};
|
|
|
|
const ENV_VAR_PREFIX: &str = "LOCALITY_";
|
|
|
|
#[derive(Error, Debug)]
|
|
pub enum Error {
|
|
#[error("The environment variable \"{0}\" must be set.")]
|
|
MissingEnvironmentVariable(String),
|
|
}
|
|
|
|
pub struct Config {
|
|
pub database_url: String,
|
|
pub static_file_path: String,
|
|
pub hmac_secret: Vec<u8>,
|
|
}
|
|
|
|
fn get_config_string(variable: &str) -> Result<String, Error> {
|
|
let full_var = format!("{ENV_VAR_PREFIX}{variable}");
|
|
env::var(&full_var).map_err(|_| Error::MissingEnvironmentVariable(full_var))
|
|
}
|
|
|
|
pub fn get_config() -> Result<Config, Error> {
|
|
Ok(Config {
|
|
database_url: get_config_string("DATABASE_URL")?,
|
|
static_file_path: get_config_string("STATIC_FILE_PATH")?,
|
|
hmac_secret: get_config_string("HMAC_SECRET")?.into_bytes(),
|
|
})
|
|
}
|