From af3f5430a0b0f1834228b28fd89848959512e718 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 12 Sep 2022 15:30:19 +0200 Subject: Use configuration for environment variables --- src/config.rs | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 02ae63d..7e67214 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,34 @@ -// TODO: Everything here must be a std::env +pub use config::ConfigError; +use lazy_static::lazy_static; +use serde::Deserialize; -pub static PAGE_LIMIT: i64 = 20; -pub const MAX_UPLOAD_FILE_SIZE: u64 = 1024 * 1024; // 1 MB -pub const SAVE_FILE_BASE_PATH: &str = "./uploads"; -pub const UPLOADS_ENDPOINT: &str = "/uploads"; +// pub static PAGE_LIMIT: i64 = std::env::var("PAGE_LIMIT") +// .unwrap_or_else(|_| "20".to_string()) +// .parse::() +// .unwrap(); +// pub const MAX_UPLOAD_FILE_SIZE: u64 = 1024 * 1024; // 1 MB +// pub const SAVE_FILE_BASE_PATH: &str = &std::env::var("SAVE_FILE_BASE_PATH").unwrap(); +// pub const UPLOADS_ENDPOINT: &str = &std::env::var("UPLOADS_ENDPOINT").unwrap(); + +#[derive(Deserialize)] +pub struct Configuration { + pub page_limit: i64, + pub save_file_base_path: String, + pub uploads_endpoint: String, + pub rust_log: String, + pub database_url: String, + pub jwt_secret: String, + pub allowed_host: String, +} + +impl Configuration { + pub fn new() -> Result { + let mut cfg = config::Config::new(); + cfg.merge(config::Environment::default())?; + cfg.try_into() + } +} + +lazy_static! { + pub static ref CONFIG: Configuration = Configuration::new().expect("Config can be loaded"); +} -- cgit v1.2.3-71-g8e6c