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 pictrs_url: String,
18 pub rate_limit: RateLimitConfig,
19 pub email: Option<EmailConfig>,
20 pub federation: Federation,
21 pub captcha: CaptchaConfig,
24 #[derive(Debug, Deserialize, Clone)]
26 pub admin_username: String,
27 pub admin_password: String,
28 pub admin_email: Option<String>,
29 pub site_name: String,
32 #[derive(Debug, Deserialize, Clone)]
33 pub struct RateLimitConfig {
35 pub message_per_second: i32,
37 pub post_per_second: i32,
39 pub register_per_second: i32,
41 pub image_per_second: i32,
44 #[derive(Debug, Deserialize, Clone)]
45 pub struct EmailConfig {
46 pub smtp_server: String,
47 pub smtp_login: Option<String>,
48 pub smtp_password: Option<String>,
49 pub smtp_from_address: String,
53 #[derive(Debug, Deserialize, Clone)]
54 pub struct CaptchaConfig {
56 pub difficulty: String, // easy, medium, or hard
59 #[derive(Debug, Deserialize, Clone)]
69 #[derive(Debug, Deserialize, Clone)]
70 pub struct Federation {
72 pub tls_enabled: bool,
73 pub allowed_instances: String,
77 static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
79 Err(e) => panic!("{}", e),
84 /// Reads config from the files and environment.
85 /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
86 /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
87 /// added to the config.
89 /// Note: The env var `LEMMY_DATABASE_URL` is parsed in
90 /// `server/lemmy_db/src/lib.rs::get_database_url_from_env()`
91 fn init() -> Result<Self, ConfigError> {
92 let mut s = Config::new();
94 s.merge(File::with_name(&Self::get_config_defaults_location()))?;
96 s.merge(File::with_name(CONFIG_FILE).required(false))?;
98 // Add in settings from the environment (with a prefix of LEMMY)
99 // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
100 // Note: we need to use double underscore here, because otherwise variables containing
101 // underscore cant be set from environmnet.
102 // https://github.com/mehcode/config-rs/issues/73
103 s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
108 /// Returns the config as a struct.
109 pub fn get() -> Self {
110 SETTINGS.read().unwrap().to_owned()
113 pub fn get_database_url(&self) -> String {
115 "postgres://{}:{}@{}:{}/{}",
117 self.database.password,
120 self.database.database
124 pub fn api_endpoint(&self) -> String {
125 format!("{}/api/v1", self.hostname)
128 pub fn get_config_defaults_location() -> String {
129 env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE_DEFAULTS.to_string())
132 pub fn read_config_file() -> Result<String, Error> {
133 fs::read_to_string(CONFIG_FILE)
136 pub fn save_config_file(data: &str) -> Result<String, Error> {
137 fs::write(CONFIG_FILE, data)?;
139 // Reload the new settings
140 // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
141 let mut new_settings = SETTINGS.write().unwrap();
142 *new_settings = match Settings::init() {
144 Err(e) => panic!("{}", e),
147 Self::read_config_file()