summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-12 13:30:19 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-12 13:30:19 +0000
commitaf3f5430a0b0f1834228b28fd89848959512e718 (patch)
tree27e843c147b92bf09893012f323ddaf54f5eb0a5 /src/config.rs
parent385102a5763f667d53a9d8ed2052ccb8c791bc91 (diff)
Use configuration for environment variables
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs38
1 files changed, 33 insertions, 5 deletions
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::<i64>()
+// .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<Self, ConfigError> {
+ 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");
+}