2021-03-01 17:24:11 +00:00
|
|
|
use crate::{
|
|
|
|
location_info,
|
2021-04-08 11:19:58 +00:00
|
|
|
settings::structs::{
|
|
|
|
CaptchaConfig,
|
|
|
|
DatabaseConfig,
|
|
|
|
EmailConfig,
|
|
|
|
FederationConfig,
|
|
|
|
RateLimitConfig,
|
|
|
|
Settings,
|
|
|
|
SetupConfig,
|
2021-03-01 17:24:11 +00:00
|
|
|
},
|
|
|
|
LemmyError,
|
|
|
|
};
|
|
|
|
use anyhow::{anyhow, Context};
|
|
|
|
use deser_hjson::from_str;
|
|
|
|
use merge::Merge;
|
|
|
|
use std::{env, fs, io::Error, net::IpAddr, sync::RwLock};
|
|
|
|
|
2021-04-06 12:57:37 +00:00
|
|
|
pub mod defaults;
|
2021-03-01 17:24:11 +00:00
|
|
|
pub mod structs;
|
|
|
|
|
|
|
|
static CONFIG_FILE: &str = "config/config.hjson";
|
|
|
|
|
|
|
|
lazy_static! {
|
2021-04-05 14:23:32 +00:00
|
|
|
static ref SETTINGS: RwLock<Settings> =
|
|
|
|
RwLock::new(Settings::init().expect("Failed to load settings file"));
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
2021-05-26 13:39:38 +00:00
|
|
|
/// Reads config from configuration file.
|
|
|
|
/// Then values from the environment (with prefix LEMMY) are added to the config.
|
|
|
|
/// And then default values are merged into config.
|
|
|
|
/// Defaults are controlled by Default trait implemntation for Settings structs.
|
2021-03-01 17:24:11 +00:00
|
|
|
///
|
|
|
|
/// Note: The env var `LEMMY_DATABASE_URL` is parsed in
|
|
|
|
/// `lemmy_db_queries/src/lib.rs::get_database_url_from_env()`
|
|
|
|
fn init() -> Result<Self, LemmyError> {
|
|
|
|
// Read the config file
|
|
|
|
let mut custom_config = from_str::<Settings>(&Self::read_config_file()?)?;
|
|
|
|
|
|
|
|
// Merge with env vars
|
|
|
|
custom_config.merge(envy::prefixed("LEMMY_").from_env::<Settings>()?);
|
|
|
|
|
|
|
|
// Merge with default
|
|
|
|
custom_config.merge(Settings::default());
|
|
|
|
|
|
|
|
if custom_config.hostname == Settings::default().hostname {
|
|
|
|
return Err(anyhow!("Hostname variable is not set!").into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(custom_config)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the config as a struct.
|
|
|
|
pub fn get() -> Self {
|
2021-03-01 12:56:07 +00:00
|
|
|
SETTINGS.read().expect("read config").to_owned()
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_database_url(&self) -> String {
|
|
|
|
let conf = self.database();
|
|
|
|
format!(
|
|
|
|
"postgres://{}:{}@{}:{}/{}",
|
2021-04-08 11:19:58 +00:00
|
|
|
conf.user(),
|
2021-04-06 12:57:37 +00:00
|
|
|
conf.password,
|
|
|
|
conf.host,
|
2021-04-08 11:19:58 +00:00
|
|
|
conf.port(),
|
|
|
|
conf.database(),
|
2021-03-01 17:24:11 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_config_location() -> String {
|
|
|
|
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_config_file() -> Result<String, Error> {
|
|
|
|
fs::read_to_string(Self::get_config_location())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_allowed_instances(&self) -> Option<Vec<String>> {
|
|
|
|
self.federation().allowed_instances
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_blocked_instances(&self) -> Option<Vec<String>> {
|
|
|
|
self.federation().blocked_instances
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns either "http" or "https", depending on tls_enabled setting
|
|
|
|
pub fn get_protocol_string(&self) -> &'static str {
|
|
|
|
if let Some(tls_enabled) = self.tls_enabled {
|
|
|
|
if tls_enabled {
|
|
|
|
"https"
|
|
|
|
} else {
|
|
|
|
"http"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
"http"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns something like `http://localhost` or `https://lemmy.ml`,
|
|
|
|
/// with the correct protocol and hostname.
|
|
|
|
pub fn get_protocol_and_hostname(&self) -> String {
|
|
|
|
format!("{}://{}", self.get_protocol_string(), self.hostname())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname`
|
|
|
|
/// variable will be like `lemmy-alpha:8541`. This method removes the port and returns
|
|
|
|
/// `lemmy-alpha` instead. It has no effect in production.
|
|
|
|
pub fn get_hostname_without_port(&self) -> Result<String, anyhow::Error> {
|
|
|
|
Ok(
|
|
|
|
self
|
|
|
|
.hostname()
|
|
|
|
.split(':')
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.first()
|
|
|
|
.context(location_info!())?
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:56:07 +00:00
|
|
|
pub fn save_config_file(data: &str) -> Result<String, LemmyError> {
|
2021-03-01 17:24:11 +00:00
|
|
|
fs::write(CONFIG_FILE, data)?;
|
|
|
|
|
|
|
|
// Reload the new settings
|
|
|
|
// From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
|
2021-03-01 12:56:07 +00:00
|
|
|
let mut new_settings = SETTINGS.write().expect("write config");
|
2021-03-01 17:24:11 +00:00
|
|
|
*new_settings = match Settings::init() {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
};
|
|
|
|
|
2021-03-01 12:56:07 +00:00
|
|
|
Ok(Self::read_config_file()?)
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hostname(&self) -> String {
|
2021-07-23 01:53:44 +00:00
|
|
|
self.hostname.to_owned().expect("No hostname given")
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
pub fn bind(&self) -> IpAddr {
|
2021-03-01 12:56:07 +00:00
|
|
|
self.bind.expect("return bind address")
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
pub fn port(&self) -> u16 {
|
2021-07-23 01:53:44 +00:00
|
|
|
self
|
|
|
|
.port
|
|
|
|
.unwrap_or_else(|| Settings::default().port.expect("missing port"))
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
pub fn tls_enabled(&self) -> bool {
|
2021-07-23 01:53:44 +00:00
|
|
|
self.tls_enabled.unwrap_or_else(|| {
|
|
|
|
Settings::default()
|
|
|
|
.tls_enabled
|
|
|
|
.expect("missing tls_enabled")
|
|
|
|
})
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
pub fn jwt_secret(&self) -> String {
|
2021-07-23 01:53:44 +00:00
|
|
|
self
|
|
|
|
.jwt_secret
|
|
|
|
.to_owned()
|
|
|
|
.unwrap_or_else(|| Settings::default().jwt_secret.expect("missing jwt_secret"))
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
pub fn pictrs_url(&self) -> String {
|
2021-07-23 01:53:44 +00:00
|
|
|
self
|
|
|
|
.pictrs_url
|
|
|
|
.to_owned()
|
|
|
|
.unwrap_or_else(|| Settings::default().pictrs_url.expect("missing pictrs_url"))
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
pub fn iframely_url(&self) -> String {
|
2021-07-23 01:53:44 +00:00
|
|
|
self.iframely_url.to_owned().unwrap_or_else(|| {
|
|
|
|
Settings::default()
|
|
|
|
.iframely_url
|
|
|
|
.expect("missing iframely_url")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn actor_name_max_length(&self) -> usize {
|
|
|
|
self.actor_name_max_length.unwrap_or_else(|| {
|
|
|
|
Settings::default()
|
|
|
|
.actor_name_max_length
|
|
|
|
.expect("missing actor name length")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn database(&self) -> DatabaseConfig {
|
|
|
|
self.database.to_owned().unwrap_or_default()
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
pub fn rate_limit(&self) -> RateLimitConfig {
|
|
|
|
self.rate_limit.to_owned().unwrap_or_default()
|
|
|
|
}
|
|
|
|
pub fn federation(&self) -> FederationConfig {
|
|
|
|
self.federation.to_owned().unwrap_or_default()
|
|
|
|
}
|
|
|
|
pub fn captcha(&self) -> CaptchaConfig {
|
|
|
|
self.captcha.to_owned().unwrap_or_default()
|
|
|
|
}
|
|
|
|
pub fn email(&self) -> Option<EmailConfig> {
|
|
|
|
self.email.to_owned()
|
|
|
|
}
|
|
|
|
pub fn setup(&self) -> Option<SetupConfig> {
|
|
|
|
self.setup.to_owned()
|
|
|
|
}
|
|
|
|
}
|