]> Untitled Git - lemmy.git/blobdiff - crates/utils/src/settings/structs.rs
Remove `actix_rt` & use standard tokio spawn (#3158)
[lemmy.git] / crates / utils / src / settings / structs.rs
index 3863f6e3c2ad3312dbdaffa551dd759895558f53..5d0e642f6a2a55787ebfe7c34e2b5a20c67f8207 100644 (file)
-use merge::Merge;
-use serde::Deserialize;
-use std::net::IpAddr;
+use doku::Document;
+use serde::{Deserialize, Serialize};
+use std::net::{IpAddr, Ipv4Addr};
+use url::Url;
 
-#[derive(Debug, Deserialize, Clone, Merge)]
+#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
+#[serde(default)]
 pub struct Settings {
-  pub(crate) database: Option<DatabaseConfig>,
-  pub(crate) rate_limit: Option<RateLimitConfig>,
-  pub(crate) federation: Option<FederationConfig>,
-  pub(crate) hostname: Option<String>,
-  pub(crate) bind: Option<IpAddr>,
-  pub(crate) port: Option<u16>,
-  pub(crate) tls_enabled: Option<bool>,
-  pub(crate) jwt_secret: Option<String>,
-  pub(crate) pictrs_url: Option<String>,
-  pub(crate) iframely_url: Option<String>,
-  pub(crate) captcha: Option<CaptchaConfig>,
-  pub(crate) email: Option<EmailConfig>,
-  pub(crate) setup: Option<SetupConfig>,
+  /// settings related to the postgresql database
+  #[default(Default::default())]
+  pub database: DatabaseConfig,
+  /// Settings related to activitypub federation
+  /// Pictrs image server configuration.
+  #[default(Some(Default::default()))]
+  pub(crate) pictrs: Option<PictrsConfig>,
+  /// Email sending configuration. All options except login/password are mandatory
+  #[default(None)]
+  #[doku(example = "Some(Default::default())")]
+  pub email: Option<EmailConfig>,
+  /// Parameters for automatic configuration of new instance (only used at first start)
+  #[default(None)]
+  #[doku(example = "Some(Default::default())")]
+  pub setup: Option<SetupConfig>,
+  /// the domain name of your instance (mandatory)
+  #[default("unset")]
+  #[doku(example = "example.com")]
+  pub hostname: String,
+  /// Address where lemmy should listen for incoming requests
+  #[default(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))]
+  #[doku(as = "String")]
+  pub bind: IpAddr,
+  /// Port where lemmy should listen for incoming requests
+  #[default(8536)]
+  pub port: u16,
+  /// Whether the site is available over TLS. Needs to be true for federation to work.
+  #[default(true)]
+  pub tls_enabled: bool,
+  /// Set the URL for opentelemetry exports. If you do not have an opentelemetry collector, do not set this option
+  #[default(None)]
+  #[doku(skip)]
+  pub opentelemetry_url: Option<Url>,
+  /// The number of activitypub federation workers that can be in-flight concurrently
+  #[default(0)]
+  pub worker_count: usize,
+  /// The number of activitypub federation retry workers that can be in-flight concurrently
+  #[default(0)]
+  pub retry_count: usize,
 }
 
-#[derive(Debug, Deserialize, Clone)]
-pub struct CaptchaConfig {
-  pub enabled: bool,
-  pub difficulty: String,
+#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
+#[serde(default, deny_unknown_fields)]
+pub struct PictrsConfig {
+  /// Address where pictrs is available (for image hosting)
+  #[default(Url::parse("http://localhost:8080").expect("parse pictrs url"))]
+  #[doku(example = "http://localhost:8080")]
+  pub url: Url,
+
+  /// Set a custom pictrs API key. ( Required for deleting images )
+  #[default(None)]
+  pub api_key: Option<String>,
 }
 
-#[derive(Debug, Deserialize, Clone)]
+#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
+#[serde(default)]
 pub struct DatabaseConfig {
-  pub(super) user: Option<String>,
-  pub password: String,
-  pub host: String,
-  pub(super) port: Option<i32>,
-  pub(super) database: Option<String>,
-  pub(super) pool_size: Option<u32>,
+  #[serde(flatten, default)]
+  pub connection: DatabaseConnection,
+
+  /// Maximum number of active sql connections
+  #[default(5)]
+  pub pool_size: usize,
 }
 
-impl DatabaseConfig {
-  pub fn user(&self) -> String {
-    self
-      .user
-      .to_owned()
-      .unwrap_or_else(|| DatabaseConfig::default().user.expect("missing user"))
-  }
-  pub fn port(&self) -> i32 {
-    self
-      .port
-      .unwrap_or_else(|| DatabaseConfig::default().port.expect("missing port"))
-  }
-  pub fn database(&self) -> String {
-    self.database.to_owned().unwrap_or_else(|| {
-      DatabaseConfig::default()
-        .database
-        .expect("missing database")
-    })
-  }
-  pub fn pool_size(&self) -> u32 {
-    self.pool_size.unwrap_or_else(|| {
-      DatabaseConfig::default()
-        .pool_size
-        .expect("missing pool_size")
-    })
-  }
+#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
+#[serde(untagged)]
+pub enum DatabaseConnection {
+  /// Configure the database by specifying a URI
+  ///
+  /// This is the preferred method to specify database connection details since
+  /// it is the most flexible.
+  Uri {
+    /// Connection URI pointing to a postgres instance
+    ///
+    /// This example uses peer authentication to obviate the need for creating,
+    /// configuring, and managing passwords.
+    ///
+    /// For an explanation of how to use connection URIs, see [here][0] in
+    /// PostgreSQL's documentation.
+    ///
+    /// [0]: https://www.postgresql.org/docs/current/libpq-connect.html#id-1.7.3.8.3.6
+    #[doku(example = "postgresql:///lemmy?user=lemmy&host=/var/run/postgresql")]
+    uri: String,
+  },
+
+  /// Configure the database by specifying parts of a URI
+  ///
+  /// Note that specifying the `uri` field should be preferred since it provides
+  /// greater control over how the connection is made. This merely exists for
+  /// backwards-compatibility.
+  #[default]
+  Parts(DatabaseConnectionParts),
 }
 
-#[derive(Debug, Deserialize, Clone)]
+#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
+#[serde(default)]
+pub struct DatabaseConnectionParts {
+  /// Username to connect to postgres
+  #[default("lemmy")]
+  pub(super) user: String,
+  /// Password to connect to postgres
+  #[default("password")]
+  pub password: String,
+  #[default("localhost")]
+  /// Host where postgres is running
+  pub host: String,
+  /// Port where postgres can be accessed
+  #[default(5432)]
+  pub(super) port: i32,
+  /// Name of the postgres database for lemmy
+  #[default("lemmy")]
+  pub(super) database: String,
+}
+
+#[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)]
+#[serde(deny_unknown_fields)]
 pub struct EmailConfig {
+  /// Hostname and port of the smtp server
+  #[doku(example = "localhost:25")]
   pub smtp_server: String,
+  /// Login name for smtp server
   pub smtp_login: Option<String>,
+  /// Password to login to the smtp server
   pub smtp_password: Option<String>,
+  #[doku(example = "noreply@example.com")]
+  /// Address to send emails from, eg "noreply@your-instance.com"
   pub smtp_from_address: String,
-  pub use_tls: bool,
-}
-
-#[derive(Debug, Deserialize, Clone)]
-pub struct FederationConfig {
-  pub enabled: bool,
-  pub allowed_instances: Option<Vec<String>>,
-  pub blocked_instances: Option<Vec<String>>,
+  /// Whether or not smtp connections should use tls. Can be none, tls, or starttls
+  #[default("none")]
+  #[doku(example = "none")]
+  pub tls_type: String,
 }
 
-#[derive(Debug, Deserialize, Clone)]
-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,
-  pub image: i32,
-  pub image_per_second: i32,
-}
-
-#[derive(Debug, Deserialize, Clone)]
+#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
+#[serde(deny_unknown_fields)]
 pub struct SetupConfig {
+  /// Username for the admin user
+  #[doku(example = "admin")]
   pub admin_username: String,
+  /// Password for the admin user. It must be at least 10 characters.
+  #[doku(example = "tf6HHDS4RolWfFhk4Rq9")]
   pub admin_password: String,
-  pub admin_email: Option<String>,
+  /// Name of the site (can be changed later)
+  #[doku(example = "My Lemmy Instance")]
   pub site_name: String,
+  /// Email for the admin user (optional, can be omitted and set later through the website)
+  #[doku(example = "user@example.com")]
+  #[default(None)]
+  pub admin_email: Option<String>,
 }