diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 779da57f4..d934d1acc 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -19,7 +19,13 @@ use lemmy_db_views_actor::{ community_view::CommunityView, }; use lemmy_structs::{blocking, comment::*, community::*, post::*, site::*, user::*, websocket::*}; -use lemmy_utils::{claims::Claims, settings::Settings, APIError, ConnectionId, LemmyError}; +use lemmy_utils::{ + claims::Claims, + settings::structs::Settings, + APIError, + ConnectionId, + LemmyError, +}; use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation}; use serde::Deserialize; use std::process::Command; diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index 1bdce91ab..401082647 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -48,7 +48,7 @@ use lemmy_db_views_moderator::{ use lemmy_structs::{blocking, site::*, user::Register}; use lemmy_utils::{ location_info, - settings::Settings, + settings::structs::Settings, utils::{check_slurs, check_slurs_opt}, version, APIError, diff --git a/crates/api/src/user.rs b/crates/api/src/user.rs index 0d11065cf..5994c241f 100644 --- a/crates/api/src/user.rs +++ b/crates/api/src/user.rs @@ -71,7 +71,7 @@ use lemmy_utils::{ claims::Claims, email::send_email, location_info, - settings::Settings, + settings::structs::Settings, utils::{ check_slurs, generate_random_string, diff --git a/crates/apub/src/activities/send/comment.rs b/crates/apub/src/activities/send/comment.rs index f007cda42..be96b6c27 100644 --- a/crates/apub/src/activities/send/comment.rs +++ b/crates/apub/src/activities/send/comment.rs @@ -31,7 +31,7 @@ use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post use lemmy_structs::{blocking, WebFingerResponse}; use lemmy_utils::{ request::{retry, RecvError}, - settings::Settings, + settings::structs::Settings, utils::{scrape_text_for_mentions, MentionData}, LemmyError, }; diff --git a/crates/apub/src/activities/send/mod.rs b/crates/apub/src/activities/send/mod.rs index 166855e20..2da0b48c2 100644 --- a/crates/apub/src/activities/send/mod.rs +++ b/crates/apub/src/activities/send/mod.rs @@ -1,4 +1,4 @@ -use lemmy_utils::settings::Settings; +use lemmy_utils::settings::structs::Settings; use url::{ParseError, Url}; use uuid::Uuid; diff --git a/crates/apub/src/activity_queue.rs b/crates/apub/src/activity_queue.rs index b4ccac754..19f43308b 100644 --- a/crates/apub/src/activity_queue.rs +++ b/crates/apub/src/activity_queue.rs @@ -22,7 +22,7 @@ use background_jobs::{ use itertools::Itertools; use lemmy_db_queries::DbPool; use lemmy_db_schema::source::{community::Community, user::User_}; -use lemmy_utils::{location_info, settings::Settings, LemmyError}; +use lemmy_utils::{location_info, settings::structs::Settings, LemmyError}; use lemmy_websocket::LemmyContext; use log::{debug, warn}; use reqwest::Client; diff --git a/crates/apub/src/fetcher/search.rs b/crates/apub/src/fetcher/search.rs index 9f465f768..2a6a9ef5c 100644 --- a/crates/apub/src/fetcher/search.rs +++ b/crates/apub/src/fetcher/search.rs @@ -35,7 +35,7 @@ use lemmy_db_schema::source::{ use lemmy_db_views::{comment_view::CommentView, post_view::PostView}; use lemmy_db_views_actor::{community_view::CommunityView, user_view::UserViewSafe}; use lemmy_structs::{blocking, site::SearchResponse}; -use lemmy_utils::{settings::Settings, LemmyError}; +use lemmy_utils::{settings::structs::Settings, LemmyError}; use lemmy_websocket::LemmyContext; use log::debug; use url::Url; diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index f0ffbcb1e..f2c8a2083 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -4,7 +4,7 @@ use http::StatusCode; use lemmy_db_queries::source::activity::Activity_; use lemmy_db_schema::source::activity::Activity; use lemmy_structs::blocking; -use lemmy_utils::{settings::Settings, LemmyError}; +use lemmy_utils::{settings::structs::Settings, LemmyError}; use lemmy_websocket::LemmyContext; use serde::{Deserialize, Serialize}; diff --git a/crates/apub/src/inbox/mod.rs b/crates/apub/src/inbox/mod.rs index 751992966..db7225a81 100644 --- a/crates/apub/src/inbox/mod.rs +++ b/crates/apub/src/inbox/mod.rs @@ -19,7 +19,7 @@ use lemmy_db_queries::{ }; use lemmy_db_schema::source::{activity::Activity, community::Community, user::User_}; use lemmy_structs::blocking; -use lemmy_utils::{location_info, settings::Settings, LemmyError}; +use lemmy_utils::{location_info, settings::structs::Settings, LemmyError}; use lemmy_websocket::LemmyContext; use serde::Serialize; use std::fmt::Debug; diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 5d6744e33..36ee9a7bb 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -34,7 +34,7 @@ use lemmy_db_schema::source::{ user::User_, }; use lemmy_structs::blocking; -use lemmy_utils::{location_info, settings::Settings, LemmyError}; +use lemmy_utils::{location_info, settings::structs::Settings, LemmyError}; use lemmy_websocket::LemmyContext; use serde::Serialize; use std::net::IpAddr; diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index f6910cce0..3000db516 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -15,7 +15,12 @@ use diesel::result::Error::NotFound; use lemmy_db_queries::{ApubObject, Crud, DbPool}; use lemmy_db_schema::source::community::Community; use lemmy_structs::blocking; -use lemmy_utils::{location_info, settings::Settings, utils::convert_datetime, LemmyError}; +use lemmy_utils::{ + location_info, + settings::structs::Settings, + utils::convert_datetime, + LemmyError, +}; use lemmy_websocket::LemmyContext; use url::Url; diff --git a/crates/apub/src/objects/user.rs b/crates/apub/src/objects/user.rs index c23f4c509..c979170f5 100644 --- a/crates/apub/src/objects/user.rs +++ b/crates/apub/src/objects/user.rs @@ -26,7 +26,7 @@ use lemmy_db_schema::{ use lemmy_structs::blocking; use lemmy_utils::{ location_info, - settings::Settings, + settings::structs::Settings, utils::{check_slurs, check_slurs_opt, convert_datetime}, LemmyError, }; diff --git a/crates/apub/src/routes.rs b/crates/apub/src/routes.rs index ac946831a..b040bb757 100644 --- a/crates/apub/src/routes.rs +++ b/crates/apub/src/routes.rs @@ -16,7 +16,7 @@ use crate::{ }; use actix_web::*; use http_signature_normalization_actix::digest::middleware::VerifyDigest; -use lemmy_utils::settings::Settings; +use lemmy_utils::settings::structs::Settings; use sha2::{Digest, Sha256}; static APUB_JSON_CONTENT_TYPE_LONG: &str = diff --git a/crates/db_queries/src/source/user.rs b/crates/db_queries/src/source/user.rs index 5f3fa6cbb..5f175e1dc 100644 --- a/crates/db_queries/src/source/user.rs +++ b/crates/db_queries/src/source/user.rs @@ -7,7 +7,7 @@ use lemmy_db_schema::{ source::user::{UserForm, UserSafeSettings, User_}, Url, }; -use lemmy_utils::settings::Settings; +use lemmy_utils::settings::structs::Settings; mod safe_type { use crate::ToSafe; diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 6e0ae8e1f..57a122640 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -15,7 +15,12 @@ use lemmy_db_views::{ }; use lemmy_db_views_actor::user_mention_view::{UserMentionQueryBuilder, UserMentionView}; use lemmy_structs::blocking; -use lemmy_utils::{claims::Claims, settings::Settings, utils::markdown_to_html, LemmyError}; +use lemmy_utils::{ + claims::Claims, + settings::structs::Settings, + utils::markdown_to_html, + LemmyError, +}; use lemmy_websocket::LemmyContext; use rss::{ extension::dublincore::DublinCoreExtensionBuilder, diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index e58f59e31..67220aec6 100644 --- a/crates/routes/src/images.rs +++ b/crates/routes/src/images.rs @@ -1,7 +1,7 @@ use actix::clock::Duration; use actix_web::{body::BodyStream, http::StatusCode, *}; use awc::Client; -use lemmy_utils::{claims::Claims, rate_limit::RateLimit, settings::Settings}; +use lemmy_utils::{claims::Claims, rate_limit::RateLimit, settings::structs::Settings}; use serde::{Deserialize, Serialize}; pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { diff --git a/crates/routes/src/nodeinfo.rs b/crates/routes/src/nodeinfo.rs index b9768d9a0..e302b2966 100644 --- a/crates/routes/src/nodeinfo.rs +++ b/crates/routes/src/nodeinfo.rs @@ -2,7 +2,7 @@ use actix_web::{body::Body, error::ErrorBadRequest, *}; use anyhow::anyhow; use lemmy_db_views::site_view::SiteView; use lemmy_structs::blocking; -use lemmy_utils::{settings::Settings, version, LemmyError}; +use lemmy_utils::{settings::structs::Settings, version, LemmyError}; use lemmy_websocket::LemmyContext; use serde::{Deserialize, Serialize}; use url::Url; diff --git a/crates/routes/src/webfinger.rs b/crates/routes/src/webfinger.rs index 1609d863d..adc726421 100644 --- a/crates/routes/src/webfinger.rs +++ b/crates/routes/src/webfinger.rs @@ -4,7 +4,7 @@ use lemmy_db_queries::source::{community::Community_, user::User}; use lemmy_db_schema::source::{community::Community, user::User_}; use lemmy_structs::{blocking, WebFingerLink, WebFingerResponse}; use lemmy_utils::{ - settings::Settings, + settings::structs::Settings, LemmyError, WEBFINGER_COMMUNITY_REGEX, WEBFINGER_USER_REGEX, diff --git a/crates/structs/src/lib.rs b/crates/structs/src/lib.rs index 54c9586af..8c92403f4 100644 --- a/crates/structs/src/lib.rs +++ b/crates/structs/src/lib.rs @@ -13,7 +13,7 @@ use lemmy_db_schema::source::{ user::User_, user_mention::{UserMention, UserMentionForm}, }; -use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError}; +use lemmy_utils::{email::send_email, settings::structs::Settings, utils::MentionData, LemmyError}; use log::error; use serde::{Deserialize, Serialize}; use url::Url; diff --git a/crates/utils/src/claims.rs b/crates/utils/src/claims.rs index 1f9404742..eac148333 100644 --- a/crates/utils/src/claims.rs +++ b/crates/utils/src/claims.rs @@ -1,4 +1,4 @@ -use crate::settings::Settings; +use crate::settings::structs::Settings; use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation}; use serde::{Deserialize, Serialize}; diff --git a/crates/utils/src/email.rs b/crates/utils/src/email.rs index 31ee4133d..208d69f6a 100644 --- a/crates/utils/src/email.rs +++ b/crates/utils/src/email.rs @@ -1,4 +1,4 @@ -use crate::settings::Settings; +use crate::settings::structs::Settings; use lettre::{ message::{header, Mailbox, MultiPart, SinglePart}, transport::smtp::{ diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 9fb56d4b9..8ec2b26ce 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -14,7 +14,7 @@ mod test; pub mod utils; pub mod version; -use crate::settings::Settings; +use crate::settings::structs::Settings; use http::StatusCode; use regex::Regex; use thiserror::Error; diff --git a/crates/utils/src/rate_limit/mod.rs b/crates/utils/src/rate_limit/mod.rs index f1c6e360d..fa45c3a4e 100644 --- a/crates/utils/src/rate_limit/mod.rs +++ b/crates/utils/src/rate_limit/mod.rs @@ -1,5 +1,5 @@ use crate::{ - settings::{RateLimitConfig, Settings}, + settings::structs::{RateLimitConfig, Settings}, utils::get_ip, LemmyError, }; diff --git a/crates/utils/src/request.rs b/crates/utils/src/request.rs index 805f1b130..522b9cd9a 100644 --- a/crates/utils/src/request.rs +++ b/crates/utils/src/request.rs @@ -1,4 +1,4 @@ -use crate::{settings::Settings, LemmyError}; +use crate::{settings::structs::Settings, LemmyError}; use anyhow::anyhow; use log::error; use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; diff --git a/crates/utils/src/settings/defaults.rs b/crates/utils/src/settings/defaults.rs new file mode 100644 index 000000000..e46b52351 --- /dev/null +++ b/crates/utils/src/settings/defaults.rs @@ -0,0 +1,69 @@ +use crate::settings::structs::*; +use std::net::{IpAddr, Ipv4Addr}; + +impl Default for Settings { + fn default() -> Self { + Self { + database: Some(DatabaseConfig::default()), + rate_limit: Some(RateLimitConfig::default()), + federation: Some(FederationConfig::default()), + captcha: Some(CaptchaConfig::default()), + email: None, + setup: None, + hostname: None, + bind: Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))), + port: Some(8536), + tls_enabled: Some(true), + jwt_secret: Some("changeme".into()), + pictrs_url: Some("http://pictrs:8080".into()), + iframely_url: Some("http://iframely".into()), + } + } +} + +impl Default for RateLimitConfig { + fn default() -> Self { + Self { + message: 180, + message_per_second: 60, + post: 6, + post_per_second: 600, + register: 3, + register_per_second: 3600, + image: 6, + image_per_second: 3600, + } + } +} + +impl Default for CaptchaConfig { + fn default() -> Self { + Self { + enabled: true, + difficulty: "medium".into(), + } + } +} + +impl Default for DatabaseConfig { + fn default() -> Self { + Self { + user: "lemmy".into(), + password: "password".into(), + host: "localhost".into(), + port: 5432, + database: "lemmy".into(), + pool_size: 5, + } + } +} + +impl Default for FederationConfig { + fn default() -> Self { + Self { + enabled: false, + allowed_instances: Some("".into()), + blocked_instances: Some("".into()), + } + } +} diff --git a/crates/utils/src/settings.rs b/crates/utils/src/settings/mod.rs similarity index 54% rename from crates/utils/src/settings.rs rename to crates/utils/src/settings/mod.rs index 0a6859f9b..0af0ecd6c 100644 --- a/crates/utils/src/settings.rs +++ b/crates/utils/src/settings/mod.rs @@ -1,154 +1,14 @@ -use crate::{location_info, LemmyError}; +use crate::{location_info, settings::structs::Settings, LemmyError}; use anyhow::Context; use deser_hjson::from_str; use merge::Merge; -use serde::Deserialize; -use std::{ - env, - fs, - io::Error, - net::{IpAddr, Ipv4Addr}, - sync::RwLock, -}; +use std::{env, fs, io::Error, sync::RwLock}; + +pub mod defaults; +pub mod structs; static CONFIG_FILE: &str = "config/config.hjson"; -#[derive(Debug, Deserialize, Clone, Merge)] -pub struct Settings { - pub setup: Option, - pub database: Option, - pub hostname: Option, - pub bind: Option, - pub port: Option, - pub tls_enabled: Option, - pub jwt_secret: Option, - pub pictrs_url: Option, - pub iframely_url: Option, - pub rate_limit: Option, - pub email: Option, - pub federation: Option, - pub captcha: Option, -} - -impl Default for Settings { - fn default() -> Self { - Self { - database: Some(DatabaseConfig::default()), - rate_limit: Some(RateLimitConfig::default()), - federation: Some(FederationConfig::default()), - captcha: Some(CaptchaConfig::default()), - email: None, - setup: None, - hostname: None, - bind: Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))), - port: Some(8536), - tls_enabled: Some(true), - jwt_secret: Some("changeme".into()), - pictrs_url: Some("http://pictrs:8080".into()), - iframely_url: Some("http://iframely".into()), - } - } -} - -#[derive(Debug, Deserialize, Clone)] -pub struct Setup { - pub admin_username: String, - pub admin_password: String, - pub admin_email: Option, - pub site_name: 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, -} - -impl Default for RateLimitConfig { - fn default() -> Self { - Self { - message: 180, - message_per_second: 60, - post: 6, - post_per_second: 600, - register: 3, - register_per_second: 3600, - image: 6, - image_per_second: 3600, - } - } -} - -#[derive(Debug, Deserialize, Clone)] -pub struct EmailConfig { - pub smtp_server: String, - pub smtp_login: Option, - pub smtp_password: Option, - pub smtp_from_address: String, - pub use_tls: bool, -} - -#[derive(Debug, Deserialize, Clone)] -pub struct CaptchaConfig { - pub enabled: bool, - pub difficulty: String, -} - -impl Default for CaptchaConfig { - fn default() -> Self { - Self { - enabled: true, - difficulty: "medium".into(), - } - } -} - -#[derive(Debug, Deserialize, Clone)] -pub struct DatabaseConfig { - pub user: String, - pub password: String, - pub host: String, - pub port: i32, - pub database: String, - pub pool_size: u32, -} - -impl Default for DatabaseConfig { - fn default() -> Self { - Self { - user: "lemmy".into(), - password: "password".into(), - host: "localhost".into(), - port: 5432, - database: "lemmy".into(), - pool_size: 5, - } - } -} - -#[derive(Debug, Deserialize, Clone)] -pub struct FederationConfig { - pub enabled: bool, - pub allowed_instances: Option, - pub blocked_instances: Option, -} - -impl Default for FederationConfig { - fn default() -> Self { - Self { - enabled: false, - allowed_instances: Some("".into()), - blocked_instances: Some("".into()), - } - } -} - lazy_static! { static ref SETTINGS: RwLock = RwLock::new(match Settings::init() { Ok(c) => c, diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs new file mode 100644 index 000000000..f0a6d679b --- /dev/null +++ b/crates/utils/src/settings/structs.rs @@ -0,0 +1,72 @@ +use merge::Merge; +use serde::Deserialize; +use std::net::IpAddr; + +#[derive(Debug, Deserialize, Clone, Merge)] +pub struct Settings { + pub setup: Option, + pub database: Option, + pub hostname: Option, + pub bind: Option, + pub port: Option, + pub tls_enabled: Option, + pub jwt_secret: Option, + pub pictrs_url: Option, + pub iframely_url: Option, + pub rate_limit: Option, + pub email: Option, + pub federation: Option, + pub captcha: Option, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct Setup { + pub admin_username: String, + pub admin_password: String, + pub admin_email: Option, + pub site_name: 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)] +pub struct EmailConfig { + pub smtp_server: String, + pub smtp_login: Option, + pub smtp_password: Option, + pub smtp_from_address: String, + pub use_tls: bool, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct CaptchaConfig { + pub enabled: bool, + pub difficulty: String, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct DatabaseConfig { + pub user: String, + pub password: String, + pub host: String, + pub port: i32, + pub database: String, + pub pool_size: u32, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct FederationConfig { + pub enabled: bool, + pub allowed_instances: Option, + pub blocked_instances: Option, +} diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index dc701b54f..5cb668313 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -1,4 +1,4 @@ -use crate::{settings::Settings, APIError}; +use crate::{settings::structs::Settings, APIError}; use actix_web::dev::ConnectionInfo; use chrono::{DateTime, FixedOffset, NaiveDateTime}; use itertools::Itertools; diff --git a/src/code_migrations.rs b/src/code_migrations.rs index 7478d5af7..d520664fd 100644 --- a/src/code_migrations.rs +++ b/src/code_migrations.rs @@ -24,7 +24,7 @@ use lemmy_db_schema::{ user::{UserForm, User_}, }, }; -use lemmy_utils::{apub::generate_actor_keypair, settings::Settings, LemmyError}; +use lemmy_utils::{apub::generate_actor_keypair, settings::structs::Settings, LemmyError}; use log::info; pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> { diff --git a/src/main.rs b/src/main.rs index 6ca5253d4..58a1a06b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use lemmy_server::{code_migrations::run_advanced_migrations, scheduled_tasks}; use lemmy_structs::blocking; use lemmy_utils::{ rate_limit::{rate_limiter::RateLimiter, RateLimit}, - settings::Settings, + settings::structs::Settings, LemmyError, }; use lemmy_websocket::{chat_server::ChatServer, LemmyContext};