|
|
@ -4,11 +4,12 @@ use serde::Deserialize;
|
|
|
|
use std::env;
|
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
|
|
|
use std::fs;
|
|
|
|
use std::net::IpAddr;
|
|
|
|
use std::net::IpAddr;
|
|
|
|
|
|
|
|
use std::sync::RwLock;
|
|
|
|
|
|
|
|
|
|
|
|
static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
|
|
|
|
static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
|
|
|
|
static CONFIG_FILE: &str = "config/config.hjson";
|
|
|
|
static CONFIG_FILE: &str = "config/config.hjson";
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct Settings {
|
|
|
|
pub struct Settings {
|
|
|
|
pub setup: Option<Setup>,
|
|
|
|
pub setup: Option<Setup>,
|
|
|
|
pub database: Database,
|
|
|
|
pub database: Database,
|
|
|
@ -22,7 +23,7 @@ pub struct Settings {
|
|
|
|
pub federation_enabled: bool,
|
|
|
|
pub federation_enabled: bool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct Setup {
|
|
|
|
pub struct Setup {
|
|
|
|
pub admin_username: String,
|
|
|
|
pub admin_username: String,
|
|
|
|
pub admin_password: String,
|
|
|
|
pub admin_password: String,
|
|
|
@ -30,7 +31,7 @@ pub struct Setup {
|
|
|
|
pub site_name: String,
|
|
|
|
pub site_name: String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct RateLimitConfig {
|
|
|
|
pub struct RateLimitConfig {
|
|
|
|
pub message: i32,
|
|
|
|
pub message: i32,
|
|
|
|
pub message_per_second: i32,
|
|
|
|
pub message_per_second: i32,
|
|
|
@ -40,7 +41,7 @@ pub struct RateLimitConfig {
|
|
|
|
pub register_per_second: i32,
|
|
|
|
pub register_per_second: i32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct EmailConfig {
|
|
|
|
pub struct EmailConfig {
|
|
|
|
pub smtp_server: String,
|
|
|
|
pub smtp_server: String,
|
|
|
|
pub smtp_login: Option<String>,
|
|
|
|
pub smtp_login: Option<String>,
|
|
|
@ -49,7 +50,7 @@ pub struct EmailConfig {
|
|
|
|
pub use_tls: bool,
|
|
|
|
pub use_tls: bool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct Database {
|
|
|
|
pub struct Database {
|
|
|
|
pub user: String,
|
|
|
|
pub user: String,
|
|
|
|
pub password: String,
|
|
|
|
pub password: String,
|
|
|
@ -60,12 +61,10 @@ pub struct Database {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
lazy_static! {
|
|
|
|
static ref SETTINGS: Settings = {
|
|
|
|
static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
|
|
|
|
match Settings::init() {
|
|
|
|
Ok(c) => c,
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
impl Settings {
|
|
|
@ -91,8 +90,8 @@ impl Settings {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the config as a struct.
|
|
|
|
/// Returns the config as a struct.
|
|
|
|
pub fn get() -> &'static Self {
|
|
|
|
pub fn get() -> Self {
|
|
|
|
&SETTINGS
|
|
|
|
SETTINGS.read().unwrap().to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
|
|
|
|
/// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
|
|
|
@ -121,7 +120,15 @@ impl Settings {
|
|
|
|
|
|
|
|
|
|
|
|
pub fn save_config_file(data: &str) -> Result<String, Error> {
|
|
|
|
pub fn save_config_file(data: &str) -> Result<String, Error> {
|
|
|
|
fs::write(CONFIG_FILE, data)?;
|
|
|
|
fs::write(CONFIG_FILE, data)?;
|
|
|
|
Self::init()?;
|
|
|
|
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Self::read_config_file()
|
|
|
|
Self::read_config_file()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|