summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDessalines <tyhou13@gmx.com>2020-04-11 14:06:04 -0400
committerDessalines <tyhou13@gmx.com>2020-04-11 14:06:04 -0400
commit61815bce2e34e41d0c915c8fa5390d824f0828f7 (patch)
treee3eb40038840e20a4d045fd706e9cba0a2f9c2ce
parentbb287cbd076940bd09f6afb61b642370d020f91e (diff)
Adding live reloading of config.hjson changes through UI.
- https://stackoverflow.com/questions/61159698/update-re-initialize-a-var-defined-in-lazy-static/61161271#61161271 - https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
-rw-r--r--server/src/api/user.rs2
-rw-r--r--server/src/lib.rs4
-rw-r--r--server/src/main.rs1
-rw-r--r--server/src/routes/index.rs2
-rw-r--r--server/src/settings.rs35
5 files changed, 26 insertions, 18 deletions
diff --git a/server/src/api/user.rs b/server/src/api/user.rs
index 056a2a84..40e09969 100644
--- a/server/src/api/user.rs
+++ b/server/src/api/user.rs
@@ -253,7 +253,7 @@ impl Perform<LoginResponse> for Oper<Register> {
// Register the new user
let user_form = UserForm {
name: data.username.to_owned(),
- fedi_name: Settings::get().hostname.to_owned(),
+ fedi_name: Settings::get().hostname,
email: data.email.to_owned(),
matrix_user_id: None,
avatar: None,
diff --git a/server/src/lib.rs b/server/src/lib.rs
index 8257dab9..9bbfe251 100644
--- a/server/src/lib.rs
+++ b/server/src/lib.rs
@@ -112,7 +112,7 @@ pub fn send_email(
to_username: &str,
html: &str,
) -> Result<(), String> {
- let email_config = Settings::get().email.as_ref().ok_or("no_email_setup")?;
+ let email_config = Settings::get().email.ok_or("no_email_setup")?;
let email = Email::builder()
.to((to_email, to_username))
@@ -127,7 +127,7 @@ pub fn send_email(
} else {
SmtpClient::new(&email_config.smtp_server, ClientSecurity::None).unwrap()
}
- .hello_name(ClientId::Domain(Settings::get().hostname.to_owned()))
+ .hello_name(ClientId::Domain(Settings::get().hostname))
.smtp_utf8(true)
.authentication_mechanism(Mechanism::Plain)
.connection_reuse(ConnectionReuseParameters::ReuseUnlimited);
diff --git a/server/src/main.rs b/server/src/main.rs
index 601c2e0d..1c79c9ee 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -39,6 +39,7 @@ async fn main() -> io::Result<()> {
// Create Http server with websocket support
HttpServer::new(move || {
+ let settings = Settings::get();
App::new()
.wrap(middleware::Logger::default())
.data(pool.clone())
diff --git a/server/src/routes/index.rs b/server/src/routes/index.rs
index 45ce204e..2e192df4 100644
--- a/server/src/routes/index.rs
+++ b/server/src/routes/index.rs
@@ -45,6 +45,6 @@ pub fn config(cfg: &mut web::ServiceConfig) {
async fn index() -> Result<NamedFile, actix_web::error::Error> {
Ok(NamedFile::open(
- Settings::get().front_end_dir.to_owned() + "/index.html",
+ Settings::get().front_end_dir + "/index.html",
)?)
}
diff --git a/server/src/settings.rs b/server/src/settings.rs
index 216c057e..6e5667cb 100644
--- a/server/src/settings.rs
+++ b/server/src/settings.rs
@@ -4,11 +4,12 @@ use serde::Deserialize;
use std::env;
use std::fs;
use std::net::IpAddr;
+use std::sync::RwLock;
static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
static CONFIG_FILE: &str = "config/config.hjson";
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
pub struct Settings {
pub setup: Option<Setup>,
pub database: Database,
@@ -22,7 +23,7 @@ pub struct Settings {
pub federation_enabled: bool,
}
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
pub struct Setup {
pub admin_username: String,
pub admin_password: String,
@@ -30,7 +31,7 @@ pub struct Setup {
pub site_name: String,
}
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
pub struct RateLimitConfig {
pub message: i32,
pub message_per_second: i32,
@@ -40,7 +41,7 @@ pub struct RateLimitConfig {
pub register_per_second: i32,
}
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
pub struct EmailConfig {
pub smtp_server: String,
pub smtp_login: Option<String>,
@@ -49,7 +50,7 @@ pub struct EmailConfig {
pub use_tls: bool,
}
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
pub struct Database {
pub user: String,
pub password: String,
@@ -60,12 +61,10 @@ pub struct Database {
}
lazy_static! {
- static ref SETTINGS: Settings = {
- match Settings::init() {
- Ok(c) => c,
- Err(e) => panic!("{}", e),
- }
- };
+ static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
+ Ok(c) => c,
+ Err(e) => panic!("{}", e),
+ });
}
impl Settings {
@@ -91,8 +90,8 @@ impl Settings {
}
/// Returns the config as a struct.
- pub fn get() -> &'static Self {
- &SETTINGS
+ pub fn get() -> Self {
+ SETTINGS.read().unwrap().to_owned()
}
/// 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> {
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()
}
}