2019-12-15 16:40:55 +00:00
|
|
|
use config::{Config, ConfigError, Environment, File};
|
|
|
|
use serde::Deserialize;
|
2020-07-14 13:17:25 +00:00
|
|
|
use std::{env, fs, io::Error, net::IpAddr, sync::RwLock};
|
2019-12-15 16:40:55 +00:00
|
|
|
|
|
|
|
static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
|
2019-12-17 21:35:48 +00:00
|
|
|
static CONFIG_FILE: &str = "config/config.hjson";
|
2019-12-15 16:40:55 +00:00
|
|
|
|
2020-04-11 18:06:04 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2019-12-15 16:40:55 +00:00
|
|
|
pub struct Settings {
|
2020-03-26 14:23:15 +00:00
|
|
|
pub setup: Option<Setup>,
|
2020-08-13 20:26:49 +00:00
|
|
|
pub database: DatabaseConfig,
|
2019-12-15 16:40:55 +00:00
|
|
|
pub hostname: String,
|
|
|
|
pub bind: IpAddr,
|
|
|
|
pub port: u16,
|
|
|
|
pub jwt_secret: String,
|
2020-08-05 16:00:00 +00:00
|
|
|
pub pictrs_url: String,
|
2019-12-15 16:40:55 +00:00
|
|
|
pub rate_limit: RateLimitConfig,
|
|
|
|
pub email: Option<EmailConfig>,
|
2020-08-13 20:26:49 +00:00
|
|
|
pub federation: FederationConfig,
|
2020-07-29 13:02:46 +00:00
|
|
|
pub captcha: CaptchaConfig,
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 18:06:04 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2020-03-26 14:23:15 +00:00
|
|
|
pub struct Setup {
|
|
|
|
pub admin_username: String,
|
|
|
|
pub admin_password: String,
|
|
|
|
pub admin_email: Option<String>,
|
|
|
|
pub site_name: String,
|
|
|
|
}
|
|
|
|
|
2020-04-11 18:06:04 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2019-12-15 16:40:55 +00:00
|
|
|
pub struct RateLimitConfig {
|
|
|
|
pub message: i32,
|
|
|
|
pub message_per_second: i32,
|
|
|
|
pub post: i32,
|
|
|
|
pub post_per_second: i32,
|
|
|
|
pub register: i32,
|
|
|
|
pub register_per_second: i32,
|
2020-08-05 16:00:00 +00:00
|
|
|
pub image: i32,
|
|
|
|
pub image_per_second: i32,
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 18:06:04 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2019-12-15 16:40:55 +00:00
|
|
|
pub struct EmailConfig {
|
|
|
|
pub smtp_server: String,
|
2020-01-31 11:03:26 +00:00
|
|
|
pub smtp_login: Option<String>,
|
|
|
|
pub smtp_password: Option<String>,
|
2019-12-15 16:40:55 +00:00
|
|
|
pub smtp_from_address: String,
|
2020-01-31 11:03:26 +00:00
|
|
|
pub use_tls: bool,
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 13:02:46 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct CaptchaConfig {
|
|
|
|
pub enabled: bool,
|
|
|
|
pub difficulty: String, // easy, medium, or hard
|
|
|
|
}
|
|
|
|
|
2020-04-11 18:06:04 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2020-08-13 20:26:49 +00:00
|
|
|
pub struct DatabaseConfig {
|
2019-12-15 16:40:55 +00:00
|
|
|
pub user: String,
|
|
|
|
pub password: String,
|
|
|
|
pub host: String,
|
|
|
|
pub port: i32,
|
|
|
|
pub database: String,
|
2019-12-16 12:25:47 +00:00
|
|
|
pub pool_size: u32,
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 23:18:13 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2020-08-13 20:26:49 +00:00
|
|
|
pub struct FederationConfig {
|
2020-03-18 21:09:00 +00:00
|
|
|
pub enabled: bool,
|
|
|
|
pub tls_enabled: bool,
|
2020-06-16 10:49:51 +00:00
|
|
|
pub allowed_instances: String,
|
2020-08-13 20:26:49 +00:00
|
|
|
pub blocked_instances: String,
|
2020-03-18 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 16:40:55 +00:00
|
|
|
lazy_static! {
|
2020-04-11 18:06:04 +00:00
|
|
|
static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
});
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
/// Reads config from the files and environment.
|
|
|
|
/// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
|
2019-12-17 21:35:48 +00:00
|
|
|
/// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
|
|
|
|
/// added to the config.
|
2020-07-10 18:15:41 +00:00
|
|
|
///
|
|
|
|
/// Note: The env var `LEMMY_DATABASE_URL` is parsed in
|
2020-09-15 19:26:47 +00:00
|
|
|
/// `lemmy_db/src/lib.rs::get_database_url_from_env()`
|
2019-12-15 16:40:55 +00:00
|
|
|
fn init() -> Result<Self, ConfigError> {
|
|
|
|
let mut s = Config::new();
|
|
|
|
|
2020-07-23 00:17:41 +00:00
|
|
|
s.merge(File::with_name(&Self::get_config_defaults_location()))?;
|
2019-12-15 16:40:55 +00:00
|
|
|
|
2020-07-23 00:17:41 +00:00
|
|
|
s.merge(File::with_name(CONFIG_FILE).required(false))?;
|
2019-12-15 16:40:55 +00:00
|
|
|
|
|
|
|
// Add in settings from the environment (with a prefix of LEMMY)
|
|
|
|
// Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
|
2019-12-28 11:11:06 +00:00
|
|
|
// Note: we need to use double underscore here, because otherwise variables containing
|
|
|
|
// underscore cant be set from environmnet.
|
|
|
|
// https://github.com/mehcode/config-rs/issues/73
|
|
|
|
s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
|
2019-12-15 16:40:55 +00:00
|
|
|
|
2020-01-02 11:30:00 +00:00
|
|
|
s.try_into()
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the config as a struct.
|
2020-04-11 18:06:04 +00:00
|
|
|
pub fn get() -> Self {
|
|
|
|
SETTINGS.read().unwrap().to_owned()
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_database_url(&self) -> String {
|
2020-07-10 18:15:41 +00:00
|
|
|
format!(
|
|
|
|
"postgres://{}:{}@{}:{}/{}",
|
|
|
|
self.database.user,
|
|
|
|
self.database.password,
|
|
|
|
self.database.host,
|
|
|
|
self.database.port,
|
|
|
|
self.database.database
|
|
|
|
)
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 00:17:41 +00:00
|
|
|
pub fn get_config_defaults_location() -> String {
|
|
|
|
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE_DEFAULTS.to_string())
|
2020-07-13 15:33:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
pub fn read_config_file() -> Result<String, Error> {
|
2020-07-23 00:17:41 +00:00
|
|
|
fs::read_to_string(CONFIG_FILE)
|
2020-04-10 20:55:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:58:32 +00:00
|
|
|
pub fn get_allowed_instances(&self) -> Vec<String> {
|
|
|
|
let mut allowed_instances: Vec<String> = self
|
|
|
|
.federation
|
|
|
|
.allowed_instances
|
|
|
|
.split(',')
|
|
|
|
.map(|d| d.to_string())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// The defaults.hjson config always returns a [""]
|
|
|
|
allowed_instances.retain(|d| !d.eq(""));
|
|
|
|
|
|
|
|
allowed_instances
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:26:49 +00:00
|
|
|
pub fn get_blocked_instances(&self) -> Vec<String> {
|
|
|
|
let mut blocked_instances: Vec<String> = self
|
|
|
|
.federation
|
|
|
|
.blocked_instances
|
|
|
|
.split(',')
|
|
|
|
.map(|d| d.to_string())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// The defaults.hjson config always returns a [""]
|
|
|
|
blocked_instances.retain(|d| !d.eq(""));
|
|
|
|
|
|
|
|
blocked_instances
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
pub fn save_config_file(data: &str) -> Result<String, Error> {
|
2020-07-23 00:17:41 +00:00
|
|
|
fs::write(CONFIG_FILE, data)?;
|
2020-04-11 18:06:04 +00:00
|
|
|
|
|
|
|
// Reload the new settings
|
|
|
|
// From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
|
|
|
|
let mut new_settings = SETTINGS.write().unwrap();
|
|
|
|
*new_settings = match Settings::init() {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
};
|
|
|
|
|
2020-04-10 20:55:57 +00:00
|
|
|
Self::read_config_file()
|
|
|
|
}
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|