]> Untitled Git - lemmy.git/commitdiff
Automatic instance setup based on config variables (fixes #404)
authorFelix <me@nutomic.com>
Thu, 26 Mar 2020 14:23:15 +0000 (15:23 +0100)
committerFelix <me@nutomic.com>
Sat, 28 Mar 2020 23:13:13 +0000 (00:13 +0100)
docker/lemmy.hjson
server/config/defaults.hjson
server/src/api/site.rs
server/src/api/user.rs
server/src/settings.rs

index 5a6d1ff11c88cd01d414a177a582de8092939350..b61ea82618621e09605466554cfb86e9b2043ef4 100644 (file)
     # interval length for registration limit
     register_per_second: 3600
   }
-#  # email sending configuration
+#  # optional: parameters for automatic configuration of new instance (only used at first start)
+#  setup: {
+#    # username for the admin user
+#    admin_username: "lemmy"
+#    # password for the admin user
+#    admin_password: "lemmy"
+#    # name of the site (can be changed later)
+#    site_name: "Lemmy Test"
+#  }
+#  # optional: email sending configuration
 #  email: {
 #    # hostname of the smtp server
 #    smtp_server: ""
index 2b37f3bb3562b138d1f6d43e7c00abf7c394884a..97b9429c16812ba43f607a6daf720b5a16b1e721 100644 (file)
@@ -1,4 +1,15 @@
 {
+#  # optional: parameters for automatic configuration of new instance (only used at first start)
+#  setup: {
+#    # username for the admin user
+#    admin_username: ""
+#    # password for the admin user
+#    admin_password: ""
+#    # optional: email for the admin user (can be omitted and set later through the website)
+#    admin_email: ""
+#    # name of the site (can be changed later)
+#    site_name: ""
+#  }
   # settings related to the postgresql database
   database: {
     # username to connect to postgres
index ef1a28287617575a82c23c46faf9e4e986be7314..6bd90149b9df79f551bac6ad7e7bc243bf200125 100644 (file)
@@ -1,5 +1,9 @@
 use super::*;
+use crate::api::user::Register;
+use crate::api::{Oper, Perform};
+use crate::settings::Settings;
 use diesel::PgConnection;
+use log::info;
 use std::str::FromStr;
 
 #[derive(Serialize, Deserialize)]
@@ -53,12 +57,12 @@ pub struct GetModlogResponse {
 
 #[derive(Serialize, Deserialize)]
 pub struct CreateSite {
-  name: String,
-  description: Option<String>,
-  enable_downvotes: bool,
-  open_registration: bool,
-  enable_nsfw: bool,
-  auth: String,
+  pub name: String,
+  pub description: Option<String>,
+  pub enable_downvotes: bool,
+  pub open_registration: bool,
+  pub enable_nsfw: bool,
+  pub auth: String,
 }
 
 #[derive(Serialize, Deserialize)]
@@ -277,10 +281,34 @@ impl Perform<GetSiteResponse> for Oper<GetSite> {
   fn perform(&self, conn: &PgConnection) -> Result<GetSiteResponse, Error> {
     let _data: &GetSite = &self.data;
 
-    // It can return a null site in order to redirect
-    let site_view = match Site::read(&conn, 1) {
-      Ok(_site) => Some(SiteView::read(&conn)?),
-      Err(_e) => None,
+    let site = Site::read(&conn, 1);
+    let site_view = if site.is_ok() {
+      Some(SiteView::read(&conn)?)
+    } else if let Some(setup) = Settings::get().setup.as_ref() {
+      let register = Register {
+        username: setup.admin_username.to_owned(),
+        email: setup.admin_email.to_owned(),
+        password: setup.admin_password.to_owned(),
+        password_verify: setup.admin_password.to_owned(),
+        admin: true,
+        show_nsfw: true,
+      };
+      let login_response = Oper::new(register).perform(&conn)?;
+      info!("Admin {} created", setup.admin_username);
+
+      let create_site = CreateSite {
+        name: setup.site_name.to_owned(),
+        description: None,
+        enable_downvotes: false,
+        open_registration: false,
+        enable_nsfw: false,
+        auth: login_response.jwt,
+      };
+      Oper::new(create_site).perform(&conn)?;
+      info!("Site {} created", setup.site_name);
+      Some(SiteView::read(&conn)?)
+    } else {
+      None
     };
 
     let mut admins = UserView::admins(&conn)?;
index 333fd9494d067c11bee959901b5c5447bb80525d..056a2a84626a0ae986b5d96218e1316347cc9bf4 100644 (file)
@@ -14,12 +14,12 @@ pub struct Login {
 
 #[derive(Serialize, Deserialize)]
 pub struct Register {
-  username: String,
-  email: Option<String>,
-  password: String,
-  password_verify: String,
-  admin: bool,
-  show_nsfw: bool,
+  pub username: String,
+  pub email: Option<String>,
+  pub password: String,
+  pub password_verify: String,
+  pub admin: bool,
+  pub show_nsfw: bool,
 }
 
 #[derive(Serialize, Deserialize)]
@@ -42,7 +42,7 @@ pub struct SaveUserSettings {
 
 #[derive(Serialize, Deserialize)]
 pub struct LoginResponse {
-  jwt: String,
+  pub jwt: String,
 }
 
 #[derive(Serialize, Deserialize)]
index ee3a3c0720036595362a98e93a2bc0122a474802..a2d61edbe5d8833ecf272368fdd38cee5ace14c7 100644 (file)
@@ -9,6 +9,7 @@ static CONFIG_FILE: &str = "config/config.hjson";
 
 #[derive(Debug, Deserialize)]
 pub struct Settings {
+  pub setup: Option<Setup>,
   pub database: Database,
   pub hostname: String,
   pub bind: IpAddr,
@@ -20,6 +21,14 @@ pub struct Settings {
   pub federation_enabled: bool,
 }
 
+#[derive(Debug, Deserialize)]
+pub struct Setup {
+  pub admin_username: String,
+  pub admin_password: String,
+  pub admin_email: Option<String>,
+  pub site_name: String,
+}
+
 #[derive(Debug, Deserialize)]
 pub struct RateLimitConfig {
   pub message: i32,