1 use config::{Config, ConfigError, Environment, File};
3 use serde::Deserialize;
9 static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
10 static CONFIG_FILE: &str = "config/config.hjson";
12 #[derive(Debug, Deserialize, Clone)]
14 pub setup: Option<Setup>,
15 pub database: Database,
19 pub jwt_secret: String,
20 pub front_end_dir: String,
21 pub rate_limit: RateLimitConfig,
22 pub email: Option<EmailConfig>,
23 pub federation_enabled: bool,
26 #[derive(Debug, Deserialize, Clone)]
28 pub admin_username: String,
29 pub admin_password: String,
30 pub admin_email: Option<String>,
31 pub site_name: String,
34 #[derive(Debug, Deserialize, Clone)]
35 pub struct RateLimitConfig {
37 pub message_per_second: i32,
39 pub post_per_second: i32,
41 pub register_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)]
64 static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
66 Err(e) => panic!("{}", e),
71 /// Reads config from the files and environment.
72 /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
73 /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
74 /// added to the config.
75 fn init() -> Result<Self, ConfigError> {
76 let mut s = Config::new();
78 s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
80 s.merge(File::with_name(CONFIG_FILE).required(false))?;
82 // Add in settings from the environment (with a prefix of LEMMY)
83 // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
84 // Note: we need to use double underscore here, because otherwise variables containing
85 // underscore cant be set from environmnet.
86 // https://github.com/mehcode/config-rs/issues/73
87 s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
92 /// Returns the config as a struct.
93 pub fn get() -> Self {
94 SETTINGS.read().unwrap().to_owned()
97 /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
98 /// otherwise the connection url is generated from the config.
99 pub fn get_database_url(&self) -> String {
100 match env::var("LEMMY_DATABASE_URL") {
103 "postgres://{}:{}@{}:{}/{}",
105 self.database.password,
108 self.database.database
113 pub fn api_endpoint(&self) -> String {
114 format!("{}/api/v1", self.hostname)
117 pub fn read_config_file() -> Result<String, Error> {
118 Ok(fs::read_to_string(CONFIG_FILE)?)
121 pub fn save_config_file(data: &str) -> Result<String, Error> {
122 fs::write(CONFIG_FILE, data)?;
124 // Reload the new settings
125 // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
126 let mut new_settings = SETTINGS.write().unwrap();
127 *new_settings = match Settings::init() {
129 Err(e) => panic!("{}", e),
132 Self::read_config_file()