1 use config::{Config, ConfigError, Environment, File};
2 use serde::Deserialize;
3 use std::{env, fs, io::Error, net::IpAddr, sync::RwLock};
5 static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
6 static CONFIG_FILE: &str = "config/config.hjson";
8 #[derive(Debug, Deserialize, Clone)]
10 pub setup: Option<Setup>,
11 pub database: Database,
15 pub jwt_secret: String,
16 pub front_end_dir: String,
17 pub rate_limit: RateLimitConfig,
18 pub email: Option<EmailConfig>,
19 pub federation: Federation,
20 pub captcha: CaptchaConfig,
23 #[derive(Debug, Deserialize, Clone)]
25 pub admin_username: String,
26 pub admin_password: String,
27 pub admin_email: Option<String>,
28 pub site_name: String,
31 #[derive(Debug, Deserialize, Clone)]
32 pub struct RateLimitConfig {
34 pub message_per_second: i32,
36 pub post_per_second: i32,
38 pub register_per_second: i32,
41 #[derive(Debug, Deserialize, Clone)]
42 pub struct EmailConfig {
43 pub smtp_server: String,
44 pub smtp_login: Option<String>,
45 pub smtp_password: Option<String>,
46 pub smtp_from_address: String,
50 #[derive(Debug, Deserialize, Clone)]
51 pub struct CaptchaConfig {
53 pub difficulty: String, // easy, medium, or hard
56 #[derive(Debug, Deserialize, Clone)]
66 #[derive(Debug, Deserialize, Clone)]
67 pub struct Federation {
69 pub tls_enabled: bool,
70 pub allowed_instances: String,
74 static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
76 Err(e) => panic!("{}", e),
81 /// Reads config from the files and environment.
82 /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
83 /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
84 /// added to the config.
86 /// Note: The env var `LEMMY_DATABASE_URL` is parsed in
87 /// `server/lemmy_db/src/lib.rs::get_database_url_from_env()`
88 fn init() -> Result<Self, ConfigError> {
89 let mut s = Config::new();
91 s.merge(File::with_name(&Self::get_config_defaults_location()))?;
93 s.merge(File::with_name(CONFIG_FILE).required(false))?;
95 // Add in settings from the environment (with a prefix of LEMMY)
96 // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
97 // Note: we need to use double underscore here, because otherwise variables containing
98 // underscore cant be set from environmnet.
99 // https://github.com/mehcode/config-rs/issues/73
100 s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
105 /// Returns the config as a struct.
106 pub fn get() -> Self {
107 SETTINGS.read().unwrap().to_owned()
110 pub fn get_database_url(&self) -> String {
112 "postgres://{}:{}@{}:{}/{}",
114 self.database.password,
117 self.database.database
121 pub fn api_endpoint(&self) -> String {
122 format!("{}/api/v1", self.hostname)
125 pub fn get_config_defaults_location() -> String {
126 env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE_DEFAULTS.to_string())
129 pub fn read_config_file() -> Result<String, Error> {
130 fs::read_to_string(CONFIG_FILE)
133 pub fn save_config_file(data: &str) -> Result<String, Error> {
134 fs::write(CONFIG_FILE, data)?;
136 // Reload the new settings
137 // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
138 let mut new_settings = SETTINGS.write().unwrap();
139 *new_settings = match Settings::init() {
141 Err(e) => panic!("{}", e),
144 Self::read_config_file()