Add config extension module

Better way to parse environment variables and pass around config vars.
This commit is contained in:
2023-06-01 23:01:11 -04:00
parent a67ffbbbed
commit effccfdbbc
3 changed files with 39 additions and 7 deletions

25
src/config.rs Normal file
View File

@@ -0,0 +1,25 @@
use anyhow::{Context, Result};
#[derive(Clone, Debug)]
pub struct Config {
pub database_url: String,
pub database_max_connections: u32,
pub host: String,
pub port: u16,
}
impl Config {
pub fn new() -> Result<Config> {
let database_url = std::env::var("DATABASE_URL").context("DATABASE_URL not set")?;
let database_max_connections = std::env::var("DATABASE_MAX_CONNECTIONS").context("DATABASE_MAX_CONNECTIONS not set")?.parse()?;
let host = std::env::var("HOST").context("HOST not set")?;
let port = std::env::var("PORT").context("PORT not set")?.parse()?;
Ok(Config {
database_url,
database_max_connections,
host,
port,
})
}
}