diff --git a/Cargo.lock b/Cargo.lock index f6421fad8..47ee01e2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1103,15 +1103,6 @@ dependencies = [ "termcolor", ] -[[package]] -name = "envy" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f47e0157f2cb54f5ae1bd371b30a2ae4311e1c028f575cd4e81de7353215965" -dependencies = [ - "serde", -] - [[package]] name = "event-listener" version = "2.5.1" @@ -1964,7 +1955,6 @@ dependencies = [ "comrak", "deser-hjson", "diesel", - "envy", "futures", "http", "itertools", diff --git a/crates/utils/Cargo.toml b/crates/utils/Cargo.toml index 44218060d..50a41fb7b 100644 --- a/crates/utils/Cargo.toml +++ b/crates/utils/Cargo.toml @@ -34,4 +34,3 @@ diesel = "1.4.5" http = "0.2.3" jsonwebtoken = "7.2.0" deser-hjson = "0.1.12" -envy = "0.4" diff --git a/crates/utils/src/settings/environment.rs b/crates/utils/src/settings/environment.rs new file mode 100644 index 000000000..89ae2866f --- /dev/null +++ b/crates/utils/src/settings/environment.rs @@ -0,0 +1,73 @@ +use crate::settings::structs_opt::{ + CaptchaConfigOpt, + DatabaseConfigOpt, + EmailConfigOpt, + FederationConfigOpt, + RateLimitConfigOpt, + SettingsOpt, + SetupOpt, +}; +use std::{env, str::FromStr}; + +pub(in crate::settings) fn parse_from_env() -> SettingsOpt { + SettingsOpt { + hostname: env_var("HOSTNAME"), + bind: env_var("BIND"), + port: env_var("PORT"), + tls_enabled: env_var("TLS_ENABLED"), + jwt_secret: env_var("JWT_SECRET"), + pictrs_url: env_var("PICTRS_URL"), + iframely_url: env_var("IFRAMELY_URL"), + rate_limit: Some(RateLimitConfigOpt { + message: env_var("RATE_LIMIT__MESSAGE"), + message_per_second: env_var("RATE_LIMIT__MESSAGE_PER_SECOND"), + post: env_var("RATE_LIMIT__POST"), + post_per_second: env_var("RATE_LIMIT__POST_PER_SECOND"), + register: env_var("RATE_LIMIT__REGISTER"), + register_per_second: env_var("RATE_LIMIT__REGISTER_PER_SECOND"), + image: env_var("RATE_LIMIT__IMAGE"), + image_per_second: env_var("RATE_LIMIT__IMAGE_PER_SECOND"), + }), + email: Some(EmailConfigOpt { + smtp_server: env_var("EMAIL__SMTP_SERVER"), + smtp_login: Some(env_var("EMAIL__SMTP_LOGIN")), + smtp_password: Some(env_var("EMAIL__SMTP_PASSWORD")), + smtp_from_address: env_var("EMAIL__SMTP_FROM_ADDRESS"), + use_tls: env_var("EMAIL__USE_TLS"), + }), + federation: Some(FederationConfigOpt { + enabled: env_var("FEDERATION__ENABLED"), + allowed_instances: env_var("FEDERATION__ALLOWED_INSTANCES"), + blocked_instances: env_var("FEDERATION__BLOCKED_INSTANCES"), + }), + captcha: Some(CaptchaConfigOpt { + enabled: env_var("CAPTCHA__ENABLED"), + difficulty: env_var("CAPTCHA__DIFFICULTY"), + }), + setup: Some(SetupOpt { + admin_username: env_var("SETUP__ADMIN_USERNAME"), + admin_password: env_var("SETUP__ADMIN_PASSWORD"), + admin_email: Some(env_var("SETUP__ADMIN_EMAIL")), + site_name: env_var("SETUP__ADMIN_SITE_NAME"), + }), + database: Some(DatabaseConfigOpt { + user: env_var("DATABASE__USER"), + password: env_var("DATABASE__PASSWORD"), + host: env_var("DATABASE__HOST"), + port: env_var("DATABASE__PORT"), + database: env_var("DATABASE__DATABASE"), + pool_size: env_var("DATABASE__POOL_SIZE"), + }), + } +} + +fn env_var(name: &str) -> Option +where + T: FromStr, + ::Err: std::fmt::Debug, +{ + // TODO: probably remove the unwrap + env::var(format!("LEMMY_{}", name)) + .ok() + .map(|v| T::from_str(&v).unwrap()) +} diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index 4345d4f16..2111213ae 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -1,6 +1,11 @@ use crate::{ location_info, - settings::{merge::Merge, structs::Settings, structs_opt::SettingsOpt}, + settings::{ + environment::parse_from_env, + merge::Merge, + structs::Settings, + structs_opt::SettingsOpt, + }, LemmyError, }; use anyhow::{anyhow, Context}; @@ -8,6 +13,7 @@ use deser_hjson::from_str; use std::{env, fs, io::Error, sync::RwLock}; pub mod defaults; +mod environment; mod merge; pub mod structs; mod structs_opt; @@ -36,7 +42,7 @@ impl Settings { config.merge(from_str::(&Self::read_config_file()?)?); // Read env vars - config.merge(envy::prefixed("LEMMY_").from_env::()?); + config.merge(parse_from_env()); if config.hostname == Settings::default().hostname { return Err(anyhow!("Hostname variable is not set!").into());