From f19c7a2e720ec22de7c64897714e93ab0e71df94 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 16 Feb 2021 19:52:56 -0500 Subject: [PATCH 1/9] A first attempt at using deser-hjson. Fixes #1270 --- .rustfmt.toml | 2 +- Cargo.lock | 233 ++++++++++++----------------- crates/api/src/lib.rs | 6 +- crates/api/src/user.rs | 21 ++- crates/apub/src/activity_queue.rs | 4 +- crates/apub/src/inbox/mod.rs | 2 +- crates/apub/src/lib.rs | 2 +- crates/apub/src/objects/mod.rs | 2 +- crates/apub/src/objects/user.rs | 2 +- crates/apub/src/routes.rs | 7 +- crates/routes/src/feeds.rs | 2 +- crates/routes/src/images.rs | 16 +- crates/routes/src/nodeinfo.rs | 2 +- crates/routes/src/webfinger.rs | 2 +- crates/structs/src/lib.rs | 2 +- crates/utils/Cargo.toml | 3 +- crates/utils/src/claims.rs | 4 +- crates/utils/src/email.rs | 2 +- crates/utils/src/lib.rs | 4 +- crates/utils/src/rate_limit/mod.rs | 2 +- crates/utils/src/request.rs | 8 +- crates/utils/src/settings.rs | 159 ++++++++++++++------ crates/utils/src/utils.rs | 5 +- docker/lemmy.hjson | 4 + src/main.rs | 7 +- tests/integration_test.rs | 2 +- 26 files changed, 285 insertions(+), 220 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index f3efdc3084..59528c80bf 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -1,5 +1,5 @@ tab_spaces = 2 edition="2018" imports_layout="HorizontalVertical" -merge_imports=true +imports_granularity="Crate" reorder_imports=true diff --git a/Cargo.lock b/Cargo.lock index 2edcca8d7d..ea7fdca3a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,7 +8,7 @@ checksum = "fe7ceed015dfca322d3bcec3653909c77557e7e57df72e98cb8806e2c93cc919" dependencies = [ "chrono", "mime", - "serde 1.0.123", + "serde", "serde_json", "thiserror", "url", @@ -21,7 +21,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb8e19a0810cc25df3535061a08b7d8f8a734d309ea4411c57a9767e4a2ffa0e" dependencies = [ "activitystreams", - "serde 1.0.123", + "serde", "serde_json", ] @@ -128,7 +128,7 @@ dependencies = [ "pin-project 1.0.4", "rand 0.7.3", "regex", - "serde 1.0.123", + "serde", "serde_json", "serde_urlencoded", "sha-1 0.9.3", @@ -156,7 +156,7 @@ dependencies = [ "http", "log", "regex", - "serde 1.0.123", + "serde", ] [[package]] @@ -300,7 +300,7 @@ dependencies = [ "pin-project 1.0.4", "regex", "rustls", - "serde 1.0.123", + "serde", "serde_json", "serde_urlencoded", "socket2", @@ -389,12 +389,6 @@ version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1" -[[package]] -name = "arrayvec" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" - [[package]] name = "async-mutex" version = "1.4.0" @@ -464,7 +458,7 @@ dependencies = [ "percent-encoding", "rand 0.7.3", "rustls", - "serde 1.0.123", + "serde", "serde_json", "serde_urlencoded", ] @@ -494,7 +488,7 @@ dependencies = [ "log", "num_cpus", "rand 0.7.3", - "serde 1.0.123", + "serde", "serde_json", "thiserror", "tokio 0.2.25", @@ -513,7 +507,7 @@ dependencies = [ "async-trait", "chrono", "log", - "serde 1.0.123", + "serde", "serde_json", "thiserror", "tokio 0.2.25", @@ -733,8 +727,8 @@ checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" dependencies = [ "libc", "num-integer", - "num-traits 0.2.14", - "serde 1.0.123", + "num-traits", + "serde", "time 0.1.44", "winapi 0.3.9", ] @@ -781,18 +775,6 @@ dependencies = [ "xdg", ] -[[package]] -name = "config" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b076e143e1d9538dde65da30f8481c2a6c44040edb8e02b9bf1351edb92ce3" -dependencies = [ - "lazy_static", - "nom 5.1.2", - "serde 1.0.123", - "serde-hjson", -] - [[package]] name = "const_fn" version = "0.4.5" @@ -995,6 +977,15 @@ dependencies = [ "syn", ] +[[package]] +name = "deser-hjson" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d995b60ff81bc6af01a98f0bf5db70a7418a1ac8bd74ada633968f388139da5e" +dependencies = [ + "serde", +] + [[package]] name = "diesel" version = "1.4.5" @@ -1590,7 +1581,7 @@ dependencies = [ "jpeg-decoder", "num-iter", "num-rational", - "num-traits 0.2.14", + "num-traits", "png", "scoped_threadpool", "tiff", @@ -1684,7 +1675,7 @@ dependencies = [ "base64 0.12.3", "pem", "ring", - "serde 1.0.123", + "serde", "serde_json", "simple_asn1", ] @@ -1745,7 +1736,7 @@ dependencies = [ "openssl", "rand 0.8.3", "reqwest", - "serde 1.0.123", + "serde", "serde_json", "sha2", "strum", @@ -1792,7 +1783,7 @@ dependencies = [ "percent-encoding", "rand 0.8.3", "reqwest", - "serde 1.0.123", + "serde", "serde_json", "sha2", "strum", @@ -1816,7 +1807,7 @@ dependencies = [ "lemmy_utils", "log", "regex", - "serde 1.0.123", + "serde", "serde_json", "sha2", "strum", @@ -1831,7 +1822,7 @@ dependencies = [ "chrono", "diesel", "log", - "serde 1.0.123", + "serde", "serde_json", "url", ] @@ -1844,7 +1835,7 @@ dependencies = [ "lemmy_db_queries", "lemmy_db_schema", "log", - "serde 1.0.123", + "serde", "url", ] @@ -1855,7 +1846,7 @@ dependencies = [ "diesel", "lemmy_db_queries", "lemmy_db_schema", - "serde 1.0.123", + "serde", ] [[package]] @@ -1865,7 +1856,7 @@ dependencies = [ "diesel", "lemmy_db_queries", "lemmy_db_schema", - "serde 1.0.123", + "serde", ] [[package]] @@ -1889,7 +1880,7 @@ dependencies = [ "lemmy_websocket", "log", "rss", - "serde 1.0.123", + "serde", "sha2", "strum", "url", @@ -1925,7 +1916,7 @@ dependencies = [ "log", "openssl", "reqwest", - "serde 1.0.123", + "serde", "serde_json", "strum", "tokio 0.3.7", @@ -1946,7 +1937,7 @@ dependencies = [ "lemmy_db_views_moderator", "lemmy_utils", "log", - "serde 1.0.123", + "serde", "serde_json", "url", ] @@ -1960,7 +1951,7 @@ dependencies = [ "anyhow", "chrono", "comrak", - "config", + "deser-hjson", "diesel", "futures", "http", @@ -1969,12 +1960,13 @@ dependencies = [ "lazy_static", "lettre", "log", + "merge", "openssl", "percent-encoding", "rand 0.8.3", "regex", "reqwest", - "serde 1.0.123", + "serde", "serde_json", "strum", "strum_macros", @@ -2001,7 +1993,7 @@ dependencies = [ "log", "rand 0.8.3", "reqwest", - "serde 1.0.123", + "serde", "serde_json", "strum", "strum_macros", @@ -2020,46 +2012,23 @@ dependencies = [ "idna", "mime", "native-tls", - "nom 6.1.0", + "nom", "once_cell", "quoted_printable", "r2d2", "rand 0.8.3", "regex", - "serde 1.0.123", + "serde", "serde_json", "uuid", ] -[[package]] -name = "lexical-core" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db65c6da02e61f55dae90a0ae427b2a5f6b3e8db09f58d10efab23af92592616" -dependencies = [ - "arrayvec", - "bitflags", - "cfg-if 0.1.10", - "ryu", - "static_assertions", -] - [[package]] name = "libc" version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cca32fa0182e8c0989459524dc356b8f2b5c10f1b9eb521b7d182c03cf8c5ff" -[[package]] -name = "linked-hash-map" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd" -dependencies = [ - "serde 0.8.23", - "serde_test", -] - [[package]] name = "linked-hash-map" version = "0.5.4" @@ -2102,7 +2071,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c" dependencies = [ - "linked-hash-map 0.5.4", + "linked-hash-map", ] [[package]] @@ -2144,6 +2113,28 @@ dependencies = [ "autocfg", ] +[[package]] +name = "merge" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10bbef93abb1da61525bbc45eeaff6473a41907d19f8f9aa5168d214e10693e9" +dependencies = [ + "merge_derive", + "num-traits", +] + +[[package]] +name = "merge_derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "209d075476da2e63b4b29e72a2ef627b840589588e71400a25e3565c4f849d07" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "migrations_internals" version = "1.4.1" @@ -2271,17 +2262,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "nom" -version = "5.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" -dependencies = [ - "lexical-core", - "memchr", - "version_check", -] - [[package]] name = "nom" version = "6.1.0" @@ -2301,7 +2281,7 @@ checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" dependencies = [ "autocfg", "num-integer", - "num-traits 0.2.14", + "num-traits", ] [[package]] @@ -2311,7 +2291,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" dependencies = [ "autocfg", - "num-traits 0.2.14", + "num-traits", ] [[package]] @@ -2322,7 +2302,7 @@ checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59" dependencies = [ "autocfg", "num-integer", - "num-traits 0.2.14", + "num-traits", ] [[package]] @@ -2333,16 +2313,7 @@ checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" dependencies = [ "autocfg", "num-integer", - "num-traits 0.2.14", -] - -[[package]] -name = "num-traits" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" -dependencies = [ - "num-traits 0.2.14", + "num-traits", ] [[package]] @@ -2597,6 +2568,30 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.19" @@ -2838,7 +2833,7 @@ dependencies = [ "native-tls", "percent-encoding", "pin-project-lite 0.2.4", - "serde 1.0.123", + "serde", "serde_json", "serde_urlencoded", "tokio 0.2.25", @@ -3008,12 +3003,6 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -[[package]] -name = "serde" -version = "0.8.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8" - [[package]] name = "serde" version = "1.0.123" @@ -3023,19 +3012,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-hjson" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a3a4e0ea8a88553209f6cc6cfe8724ecad22e1acf372793c27d995290fe74f8" -dependencies = [ - "lazy_static", - "linked-hash-map 0.3.0", - "num-traits 0.1.43", - "regex", - "serde 0.8.23", -] - [[package]] name = "serde_derive" version = "1.0.123" @@ -3056,16 +3032,7 @@ dependencies = [ "indexmap", "itoa", "ryu", - "serde 1.0.123", -] - -[[package]] -name = "serde_test" -version = "0.8.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "110b3dbdf8607ec493c22d5d947753282f3bae73c0f56d322af1e8c78e4c23d5" -dependencies = [ - "serde 0.8.23", + "serde", ] [[package]] @@ -3077,7 +3044,7 @@ dependencies = [ "form_urlencoded", "itoa", "ryu", - "serde 1.0.123", + "serde", ] [[package]] @@ -3147,7 +3114,7 @@ checksum = "692ca13de57ce0613a363c8c2f1de925adebc81b04c923ac60c5488bb44abe4b" dependencies = [ "chrono", "num-bigint", - "num-traits 0.2.14", + "num-traits", ] [[package]] @@ -3188,12 +3155,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "stdweb" version = "0.4.20" @@ -3216,7 +3177,7 @@ checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" dependencies = [ "proc-macro2", "quote", - "serde 1.0.123", + "serde", "serde_derive", "syn", ] @@ -3230,7 +3191,7 @@ dependencies = [ "base-x", "proc-macro2", "quote", - "serde 1.0.123", + "serde", "serde_derive", "serde_json", "sha1", @@ -3666,7 +3627,7 @@ dependencies = [ "idna", "matches", "percent-encoding", - "serde 1.0.123", + "serde", ] [[package]] @@ -3676,7 +3637,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" dependencies = [ "getrandom 0.2.2", - "serde 1.0.123", + "serde", ] [[package]] @@ -3720,7 +3681,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55c0f7123de74f0dab9b7d00fd614e7b19349cd1e2f5252bbe9b1754b59433be" dependencies = [ "cfg-if 1.0.0", - "serde 1.0.123", + "serde", "serde_json", "wasm-bindgen-macro", ] diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 5e9e1c1624..779da57f4c 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -184,7 +184,7 @@ pub(crate) fn check_optional_url(item: &Option>) -> Result<(), Le pub(crate) async fn build_federated_instances( pool: &DbPool, ) -> Result, LemmyError> { - if Settings::get().federation.enabled { + if Settings::get().federation.unwrap_or_default().enabled { let distinct_communities = blocking(pool, move |conn| { Community::distinct_federated_communities(conn) }) @@ -199,7 +199,9 @@ pub(crate) async fn build_federated_instances( .collect::, LemmyError>>()?; linked.extend_from_slice(&allowed); - linked.retain(|a| !blocked.contains(a) && !a.eq("") && !a.eq(&Settings::get().hostname)); + linked.retain(|a| { + !blocked.contains(a) && !a.eq("") && !a.eq(&Settings::get().hostname.unwrap_or_default()) + }); // Sort and remove dupes linked.sort_unstable(); diff --git a/crates/api/src/user.rs b/crates/api/src/user.rs index 93d40bdafc..0d11065cf0 100644 --- a/crates/api/src/user.rs +++ b/crates/api/src/user.rs @@ -121,7 +121,7 @@ impl Perform for Login { // Return the jwt Ok(LoginResponse { - jwt: Claims::jwt(user.id, Settings::get().hostname)?, + jwt: Claims::jwt(user.id, Settings::get().hostname.unwrap_or_default())?, }) } } @@ -161,7 +161,7 @@ impl Perform for Register { .await??; // If its not the admin, check the captcha - if !no_admins && Settings::get().captcha.enabled { + if !no_admins && Settings::get().captcha.unwrap_or_default().enabled { let check = context .chat_server() .send(CheckCaptcha { @@ -303,7 +303,10 @@ impl Perform for Register { // Return the jwt Ok(LoginResponse { - jwt: Claims::jwt(inserted_user.id, Settings::get().hostname)?, + jwt: Claims::jwt( + inserted_user.id, + Settings::get().hostname.unwrap_or_default(), + )?, }) } } @@ -317,7 +320,7 @@ impl Perform for GetCaptcha { context: &Data, _websocket_id: Option, ) -> Result { - let captcha_settings = Settings::get().captcha; + let captcha_settings = Settings::get().captcha.unwrap_or_default(); if !captcha_settings.enabled { return Ok(GetCaptchaResponse { ok: None }); @@ -476,7 +479,10 @@ impl Perform for SaveUserSettings { // Return the jwt Ok(LoginResponse { - jwt: Claims::jwt(updated_user.id, Settings::get().hostname)?, + jwt: Claims::jwt( + updated_user.id, + Settings::get().hostname.unwrap_or_default(), + )?, }) } } @@ -1012,7 +1018,10 @@ impl Perform for PasswordChange { // Return the jwt Ok(LoginResponse { - jwt: Claims::jwt(updated_user.id, Settings::get().hostname)?, + jwt: Claims::jwt( + updated_user.id, + Settings::get().hostname.unwrap_or_default(), + )?, }) } } diff --git a/crates/apub/src/activity_queue.rs b/crates/apub/src/activity_queue.rs index c0c4ac4680..b4ccac754e 100644 --- a/crates/apub/src/activity_queue.rs +++ b/crates/apub/src/activity_queue.rs @@ -88,7 +88,7 @@ where .await? .iter() .unique() - .filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname)) + .filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname.unwrap_or_default())) .filter(|inbox| check_is_apub_id_valid(inbox).is_ok()) .map(|inbox| inbox.to_owned()) .collect(); @@ -215,7 +215,7 @@ where Kind: Serialize, >::Error: From + Send + Sync + 'static, { - if !Settings::get().federation.enabled || inboxes.is_empty() { + if !Settings::get().federation.unwrap_or_default().enabled || inboxes.is_empty() { return Ok(()); } diff --git a/crates/apub/src/inbox/mod.rs b/crates/apub/src/inbox/mod.rs index 765d5dff46..751992966e 100644 --- a/crates/apub/src/inbox/mod.rs +++ b/crates/apub/src/inbox/mod.rs @@ -167,7 +167,7 @@ where let id = activity.id_unchecked().context(location_info!())?; let activity_domain = id.domain().context(location_info!())?; - if activity_domain == Settings::get().hostname { + if activity_domain == Settings::get().hostname.unwrap_or_default() { return Err( anyhow!( "Error: received activity which was sent by local instance: {:?}", diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 388d57e6a9..5d6744e334 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -64,7 +64,7 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> { let domain = apub_id.domain().context(location_info!())?.to_string(); let local_instance = settings.get_hostname_without_port()?; - if !settings.federation.enabled { + if !settings.federation.unwrap_or_default().enabled { return if domain == local_instance { Ok(()) } else { diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index bf0ac24cbd..f6910cce00 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -189,7 +189,7 @@ where let domain = object_id.domain().context(location_info!())?; // if its a local object, return it directly from the database - if Settings::get().hostname == domain { + if Settings::get().hostname.unwrap_or_default() == domain { let object = blocking(context.pool(), move |conn| { To::read_from_apub_id(conn, &object_id.into()) }) diff --git a/crates/apub/src/objects/user.rs b/crates/apub/src/objects/user.rs index 8a911de27c..c23f4c509a 100644 --- a/crates/apub/src/objects/user.rs +++ b/crates/apub/src/objects/user.rs @@ -99,7 +99,7 @@ impl FromApub for User_ { ) -> Result { let user_id = person.id_unchecked().context(location_info!())?.to_owned(); let domain = user_id.domain().context(location_info!())?; - if domain == Settings::get().hostname { + if domain == Settings::get().hostname.unwrap_or_default() { let user = blocking(context.pool(), move |conn| { User_::read_from_apub_id(conn, &user_id.into()) }) diff --git a/crates/apub/src/routes.rs b/crates/apub/src/routes.rs index 3717a894f0..ac946831ae 100644 --- a/crates/apub/src/routes.rs +++ b/crates/apub/src/routes.rs @@ -23,8 +23,11 @@ static APUB_JSON_CONTENT_TYPE_LONG: &str = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""; pub fn config(cfg: &mut web::ServiceConfig) { - if Settings::get().federation.enabled { - println!("federation enabled, host is {}", Settings::get().hostname); + if Settings::get().federation.unwrap_or_default().enabled { + println!( + "federation enabled, host is {}", + Settings::get().hostname.unwrap_or_default() + ); let digest_verifier = VerifyDigest::new(Sha256::new()); let header_guard_accept = guard::Any(guard::Header("Accept", APUB_JSON_CONTENT_TYPE)) diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 837e1489af..6e0ae8e1fd 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -163,7 +163,7 @@ fn get_feed_user( ) -> Result { let site_view = SiteView::read(&conn)?; let user = User_::find_by_username(&conn, &user_name)?; - let user_url = user.get_profile_url(&Settings::get().hostname); + let user_url = user.get_profile_url(&Settings::get().hostname.unwrap_or_default()); let posts = PostQueryBuilder::create(&conn) .listing_type(&ListingType::All) diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index 4d7656e1b2..e58f59e31b 100644 --- a/crates/routes/src/images.rs +++ b/crates/routes/src/images.rs @@ -54,8 +54,10 @@ async fn upload( return Ok(HttpResponse::Unauthorized().finish()); }; - let mut client_req = - client.request_from(format!("{}/image", Settings::get().pictrs_url), req.head()); + let mut client_req = client.request_from( + format!("{}/image", Settings::get().pictrs_url.unwrap_or_default()), + req.head(), + ); if let Some(addr) = req.head().peer_addr { client_req = client_req.header("X-Forwarded-For", addr.to_string()) @@ -78,14 +80,18 @@ async fn full_res( // If there are no query params, the URL is original let url = if params.format.is_none() && params.thumbnail.is_none() { - format!("{}/image/original/{}", Settings::get().pictrs_url, name,) + format!( + "{}/image/original/{}", + Settings::get().pictrs_url.unwrap_or_default(), + name, + ) } else { // Use jpg as a default when none is given let format = params.format.unwrap_or_else(|| "jpg".to_string()); let mut url = format!( "{}/image/process.{}?src={}", - Settings::get().pictrs_url, + Settings::get().pictrs_url.unwrap_or_default(), format, name, ); @@ -134,7 +140,7 @@ async fn delete( let url = format!( "{}/image/delete/{}/{}", - Settings::get().pictrs_url, + Settings::get().pictrs_url.unwrap_or_default(), &token, &file ); diff --git a/crates/routes/src/nodeinfo.rs b/crates/routes/src/nodeinfo.rs index 46ef7ce4df..b9768d9a0e 100644 --- a/crates/routes/src/nodeinfo.rs +++ b/crates/routes/src/nodeinfo.rs @@ -31,7 +31,7 @@ async fn node_info(context: web::Data) -> Result( &jwt, - &DecodingKey::from_secret(Settings::get().jwt_secret.as_ref()), + &DecodingKey::from_secret(Settings::get().jwt_secret.unwrap_or_default().as_ref()), &v, ) } @@ -31,7 +31,7 @@ impl Claims { encode( &Header::default(), &my_claims, - &EncodingKey::from_secret(Settings::get().jwt_secret.as_ref()), + &EncodingKey::from_secret(Settings::get().jwt_secret.unwrap_or_default().as_ref()), ) } } diff --git a/crates/utils/src/email.rs b/crates/utils/src/email.rs index 8e61500c0f..31ee4133da 100644 --- a/crates/utils/src/email.rs +++ b/crates/utils/src/email.rs @@ -20,7 +20,7 @@ pub fn send_email( html: &str, ) -> Result<(), String> { let email_config = Settings::get().email.ok_or("no_email_setup")?; - let domain = Settings::get().hostname; + let domain = Settings::get().hostname.unwrap_or_default(); let (smtp_server, smtp_port) = { let email_and_port = email_config.smtp_server.split(':').collect::>(); diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 6d8265aa3f..9fb56d4b91 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -83,12 +83,12 @@ impl actix_web::error::ResponseError for LemmyError { lazy_static! { pub static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!( "^group:([a-z0-9_]{{3, 20}})@{}$", - Settings::get().hostname + Settings::get().hostname.unwrap_or_default() )) .unwrap(); pub static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!( "^acct:([a-z0-9_]{{3, 20}})@{}$", - Settings::get().hostname + Settings::get().hostname.unwrap_or_default() )) .unwrap(); } diff --git a/crates/utils/src/rate_limit/mod.rs b/crates/utils/src/rate_limit/mod.rs index 5a18ffd549..f1c6e360d9 100644 --- a/crates/utils/src/rate_limit/mod.rs +++ b/crates/utils/src/rate_limit/mod.rs @@ -70,7 +70,7 @@ impl RateLimited { { // Does not need to be blocking because the RwLock in settings never held across await points, // and the operation here locks only long enough to clone - let rate_limit: RateLimitConfig = Settings::get().rate_limit; + let rate_limit: RateLimitConfig = Settings::get().rate_limit.unwrap_or_default(); // before { diff --git a/crates/utils/src/request.rs b/crates/utils/src/request.rs index 411d434276..805f1b130f 100644 --- a/crates/utils/src/request.rs +++ b/crates/utils/src/request.rs @@ -58,7 +58,11 @@ pub(crate) async fn fetch_iframely( client: &Client, url: &str, ) -> Result { - let fetch_url = format!("{}/oembed?url={}", Settings::get().iframely_url, url); + let fetch_url = format!( + "{}/oembed?url={}", + Settings::get().iframely_url.unwrap_or_default(), + url + ); let response = retry(|| client.get(&fetch_url).send()).await?; @@ -89,7 +93,7 @@ pub(crate) async fn fetch_pictrs( let fetch_url = format!( "{}/image/download?url={}", - Settings::get().pictrs_url, + Settings::get().pictrs_url.unwrap_or_default(), utf8_percent_encode(image_url, NON_ALPHANUMERIC) // TODO this might not be needed ); diff --git a/crates/utils/src/settings.rs b/crates/utils/src/settings.rs index 8301dcee41..6400264fd3 100644 --- a/crates/utils/src/settings.rs +++ b/crates/utils/src/settings.rs @@ -1,27 +1,53 @@ -use crate::location_info; +use crate::{location_info, LemmyError}; use anyhow::Context; -use config::{Config, ConfigError, Environment, File}; +use deser_hjson::from_str; +use merge::Merge; use serde::Deserialize; -use std::{env, fs, io::Error, net::IpAddr, sync::RwLock}; +use std::{ + env, + fs, + io::Error, + net::{IpAddr, Ipv4Addr}, + sync::RwLock, +}; -static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson"; static CONFIG_FILE: &str = "config/config.hjson"; -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Deserialize, Clone, Merge)] pub struct Settings { pub setup: Option, - pub database: DatabaseConfig, - pub hostname: String, - pub bind: IpAddr, - pub port: u16, - pub tls_enabled: bool, - pub jwt_secret: String, - pub pictrs_url: String, - pub iframely_url: String, - pub rate_limit: RateLimitConfig, + 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: FederationConfig, - pub captcha: CaptchaConfig, + 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)] @@ -44,6 +70,21 @@ pub struct RateLimitConfig { 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, @@ -56,7 +97,16 @@ pub struct EmailConfig { #[derive(Debug, Deserialize, Clone)] pub struct CaptchaConfig { pub enabled: bool, - pub difficulty: String, // easy, medium, or hard + pub difficulty: String, +} + +impl Default for CaptchaConfig { + fn default() -> Self { + Self { + enabled: true, + difficulty: "medium".into(), + } + } } #[derive(Debug, Deserialize, Clone)] @@ -69,6 +119,19 @@ pub struct DatabaseConfig { 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, @@ -76,6 +139,16 @@ pub struct FederationConfig { pub blocked_instances: String, } +impl Default for FederationConfig { + fn default() -> Self { + Self { + enabled: false, + allowed_instances: "".into(), + blocked_instances: "".into(), + } + } +} + lazy_static! { static ref SETTINGS: RwLock = RwLock::new(match Settings::init() { Ok(c) => c, @@ -91,21 +164,14 @@ impl Settings { /// /// Note: The env var `LEMMY_DATABASE_URL` is parsed in /// `lemmy_db_queries/src/lib.rs::get_database_url_from_env()` - fn init() -> Result { - let mut s = Config::new(); - - s.merge(File::with_name(&Self::get_config_defaults_location()))?; + fn init() -> Result { + // Read the config file + let mut custom_config = from_str::(&Self::read_config_file()?)?; - s.merge(File::with_name(&Self::get_config_location()).required(false))?; + // Merge with default + custom_config.merge(Settings::default()); - // Add in settings from the environment (with a prefix of LEMMY) - // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key - // Note: we need to use double underscore here, because otherwise variables containing - // underscore cant be set from environmnet. - // https://github.com/mehcode/config-rs/issues/73 - s.merge(Environment::with_prefix("LEMMY").separator("__"))?; - - s.try_into() + Ok(custom_config) } /// Returns the config as a struct. @@ -114,20 +180,13 @@ impl Settings { } pub fn get_database_url(&self) -> String { + let conf = self.database.to_owned().unwrap_or_default(); format!( "postgres://{}:{}@{}:{}/{}", - self.database.user, - self.database.password, - self.database.host, - self.database.port, - self.database.database + conf.user, conf.password, conf.host, conf.port, conf.database, ) } - pub fn get_config_defaults_location() -> String { - env::var("LEMMY_CONFIG_DEFAULTS_LOCATION").unwrap_or_else(|_| CONFIG_FILE_DEFAULTS.to_string()) - } - pub fn get_config_location() -> String { env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE.to_string()) } @@ -139,6 +198,8 @@ impl Settings { pub fn get_allowed_instances(&self) -> Vec { let mut allowed_instances: Vec = self .federation + .to_owned() + .unwrap_or_default() .allowed_instances .split(',') .map(|d| d.trim().to_string()) @@ -146,13 +207,14 @@ impl Settings { // The defaults.hjson config always returns a [""] allowed_instances.retain(|d| !d.eq("")); - allowed_instances } pub fn get_blocked_instances(&self) -> Vec { let mut blocked_instances: Vec = self .federation + .to_owned() + .unwrap_or_default() .blocked_instances .split(',') .map(|d| d.trim().to_string()) @@ -160,14 +222,17 @@ impl Settings { // The defaults.hjson config always returns a [""] blocked_instances.retain(|d| !d.eq("")); - blocked_instances } /// Returns either "http" or "https", depending on tls_enabled setting pub fn get_protocol_string(&self) -> &'static str { - if self.tls_enabled { - "https" + if let Some(tls_enabled) = self.tls_enabled { + if tls_enabled { + "https" + } else { + "http" + } } else { "http" } @@ -176,7 +241,11 @@ impl Settings { /// Returns something like `http://localhost` or `https://lemmy.ml`, /// with the correct protocol and hostname. pub fn get_protocol_and_hostname(&self) -> String { - format!("{}://{}", self.get_protocol_string(), self.hostname) + format!( + "{}://{}", + self.get_protocol_string(), + self.hostname.to_owned().unwrap_or_default() + ) } /// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname` @@ -186,6 +255,8 @@ impl Settings { Ok( self .hostname + .to_owned() + .unwrap_or_default() .split(':') .collect::>() .first() diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index 6297435689..dc701b54fc 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -85,7 +85,10 @@ pub struct MentionData { impl MentionData { pub fn is_local(&self) -> bool { - Settings::get().hostname.eq(&self.domain) + Settings::get() + .hostname + .unwrap_or_default() + .eq(&self.domain) } pub fn full_name(&self) -> String { format!("@{}@{}", &self.name, &self.domain) diff --git a/docker/lemmy.hjson b/docker/lemmy.hjson index bea65d8f9d..bceb5f6cd4 100644 --- a/docker/lemmy.hjson +++ b/docker/lemmy.hjson @@ -29,6 +29,10 @@ password: "password" # host where postgres is running host: "postgres" + # port where postgres can be accessed + port: 5432 + # maximum number of active sql connections + pool_size: 5 } # # optional: email sending configuration # email: { diff --git a/src/main.rs b/src/main.rs index 4ebedb4f84..6ca5253d4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ async fn main() -> Result<(), LemmyError> { }; let manager = ConnectionManager::::new(&db_url); let pool = Pool::builder() - .max_size(settings.database.pool_size) + .max_size(settings.database.unwrap_or_default().pool_size) .build(manager) .unwrap_or_else(|_| panic!("Error connecting to {}", db_url)); @@ -61,7 +61,8 @@ async fn main() -> Result<(), LemmyError> { println!( "Starting http server at {}:{}", - settings.bind, settings.port + settings.bind.unwrap(), + settings.port.unwrap_or_default() ); let activity_queue = create_activity_queue(); @@ -94,7 +95,7 @@ async fn main() -> Result<(), LemmyError> { .configure(nodeinfo::config) .configure(webfinger::config) }) - .bind((settings.bind, settings.port))? + .bind((settings.bind.unwrap(), settings.port.unwrap_or_default()))? .run() .await?; diff --git a/tests/integration_test.rs b/tests/integration_test.rs index f205ceb51d..6658cc726b 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -58,7 +58,7 @@ fn create_context() -> LemmyContext { }; let manager = ConnectionManager::::new(&db_url); let pool = Pool::builder() - .max_size(settings.database.pool_size) + .max_size(settings.database.unwrap_or_default().pool_size) .build(manager) .unwrap(); embedded_migrations::run(&pool.get().unwrap()).unwrap(); From cddf9a6a85998434716657c3352e0c6c71d19dc5 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 17 Feb 2021 11:12:20 -0500 Subject: [PATCH 2/9] Trying to fix tests, try 1 --- api_tests/prepare-drone-federation-test.sh | 44 ++++--------- crates/utils/src/settings.rs | 12 ++-- docker/dev/Dockerfile | 1 - docker/federation/docker-compose.yml | 75 +++------------------- docker/prod/Dockerfile | 1 - docker/prod/Dockerfile.arm | 1 - 6 files changed, 28 insertions(+), 106 deletions(-) diff --git a/api_tests/prepare-drone-federation-test.sh b/api_tests/prepare-drone-federation-test.sh index de9b7b8444..32eb751367 100755 --- a/api_tests/prepare-drone-federation-test.sh +++ b/api_tests/prepare-drone-federation-test.sh @@ -1,13 +1,6 @@ #!/bin/bash set -e -export LEMMY_JWT_SECRET=changeme -export LEMMY_FEDERATION__ENABLED=true -export LEMMY_TLS_ENABLED=false -export LEMMY_SETUP__ADMIN_PASSWORD=lemmy -export LEMMY_RATE_LIMIT__POST=99999 -export LEMMY_RATE_LIMIT__REGISTER=99999 -export LEMMY_CAPTCHA__ENABLED=false export LEMMY_TEST_SEND_SYNC=1 export RUST_BACKTRACE=1 @@ -35,52 +28,39 @@ fi killall lemmy_server || true +echo "$PWD" + echo "start alpha" LEMMY_HOSTNAME=lemmy-alpha:8541 \ - LEMMY_PORT=8541 \ + LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_alpha.hjson \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_alpha" \ - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon \ - LEMMY_SETUP__ADMIN_USERNAME=lemmy_alpha \ - LEMMY_SETUP__SITE_NAME=lemmy-alpha \ - target/lemmy_server >/dev/null 2>&1 & + target/lemmy_server >/tmp/lemmy_alpha.out 2>&1 & echo "start beta" LEMMY_HOSTNAME=lemmy-beta:8551 \ - LEMMY_PORT=8551 \ + LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_beta.hjson \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_beta" \ - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon \ - LEMMY_SETUP__ADMIN_USERNAME=lemmy_beta \ - LEMMY_SETUP__SITE_NAME=lemmy-beta \ - target/lemmy_server >/dev/null 2>&1 & + target/lemmy_server >/tmp/lemmy_beta.out 2>&1 & echo "start gamma" LEMMY_HOSTNAME=lemmy-gamma:8561 \ - LEMMY_PORT=8561 \ + LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_gamma.hjson \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_gamma" \ - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon \ - LEMMY_SETUP__ADMIN_USERNAME=lemmy_gamma \ - LEMMY_SETUP__SITE_NAME=lemmy-gamma \ - target/lemmy_server >/dev/null 2>&1 & + target/lemmy_server >/tmp/lemmy_gamma.out 2>&1 & echo "start delta" # An instance with only an allowlist for beta LEMMY_HOSTNAME=lemmy-delta:8571 \ - LEMMY_PORT=8571 \ + LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_delta.hjson \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_delta" \ - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta \ - LEMMY_SETUP__ADMIN_USERNAME=lemmy_delta \ - LEMMY_SETUP__SITE_NAME=lemmy-delta \ - target/lemmy_server >/dev/null 2>&1 & + target/lemmy_server >/tmp/lemmy_delta.out 2>&1 & echo "start epsilon" # An instance who has a blocklist, with lemmy-alpha blocked LEMMY_HOSTNAME=lemmy-epsilon:8581 \ - LEMMY_PORT=8581 \ + LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_epsilon.hjson \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_epsilon" \ - LEMMY_FEDERATION__BLOCKED_INSTANCES=lemmy-alpha \ - LEMMY_SETUP__ADMIN_USERNAME=lemmy_epsilon \ - LEMMY_SETUP__SITE_NAME=lemmy-epsilon \ - target/lemmy_server >/dev/null 2>&1 & + target/lemmy_server >/tmp/lemmy_epsilon.out 2>&1 & echo "wait for all instances to start" while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'localhost:8541/api/v2/site')" != "200" ]]; do sleep 1; done diff --git a/crates/utils/src/settings.rs b/crates/utils/src/settings.rs index 6400264fd3..0a6859f9be 100644 --- a/crates/utils/src/settings.rs +++ b/crates/utils/src/settings.rs @@ -135,16 +135,16 @@ impl Default for DatabaseConfig { #[derive(Debug, Deserialize, Clone)] pub struct FederationConfig { pub enabled: bool, - pub allowed_instances: String, - pub blocked_instances: String, + pub allowed_instances: Option, + pub blocked_instances: Option, } impl Default for FederationConfig { fn default() -> Self { Self { enabled: false, - allowed_instances: "".into(), - blocked_instances: "".into(), + allowed_instances: Some("".into()), + blocked_instances: Some("".into()), } } } @@ -201,11 +201,11 @@ impl Settings { .to_owned() .unwrap_or_default() .allowed_instances + .unwrap_or_default() .split(',') .map(|d| d.trim().to_string()) .collect(); - // The defaults.hjson config always returns a [""] allowed_instances.retain(|d| !d.eq("")); allowed_instances } @@ -216,11 +216,11 @@ impl Settings { .to_owned() .unwrap_or_default() .blocked_instances + .unwrap_or_default() .split(',') .map(|d| d.trim().to_string()) .collect(); - // The defaults.hjson config always returns a [""] blocked_instances.retain(|d| !d.eq("")); blocked_instances } diff --git a/docker/dev/Dockerfile b/docker/dev/Dockerfile index a2712f9f23..d56a1fdcf3 100644 --- a/docker/dev/Dockerfile +++ b/docker/dev/Dockerfile @@ -56,7 +56,6 @@ RUN addgroup -g 1000 lemmy RUN adduser -D -s /bin/sh -u 1000 -G lemmy lemmy # Copy resources -COPY --chown=lemmy:lemmy config/defaults.hjson /config/defaults.hjson COPY --chown=lemmy:lemmy --from=builder /app/lemmy_server /app/lemmy RUN chown lemmy:lemmy /app/lemmy diff --git a/docker/federation/docker-compose.yml b/docker/federation/docker-compose.yml index 72e6c5cec5..ed662e4410 100644 --- a/docker/federation/docker-compose.yml +++ b/docker/federation/docker-compose.yml @@ -38,20 +38,9 @@ services: - lemmy-alpha lemmy-alpha: image: lemmy-federation:latest + volumes: + - ./lemmy_alpha.hjson:/config/config.hjson environment: - - LEMMY_HOSTNAME=lemmy-alpha:8541 - - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_alpha:5432/lemmy - - LEMMY_JWT_SECRET=changeme - - LEMMY_FEDERATION__ENABLED=true - - LEMMY_TLS_ENABLED=false - - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon - - LEMMY_PORT=8541 - - LEMMY_SETUP__ADMIN_USERNAME=lemmy_alpha - - LEMMY_SETUP__ADMIN_PASSWORD=lemmy - - LEMMY_SETUP__SITE_NAME=lemmy-alpha - - LEMMY_RATE_LIMIT__POST=99999 - - LEMMY_RATE_LIMIT__REGISTER=99999 - - LEMMY_CAPTCHA__ENABLED=false - LEMMY_TEST_SEND_SYNC=1 - RUST_BACKTRACE=1 - RUST_LOG=debug @@ -78,20 +67,9 @@ services: - lemmy-beta lemmy-beta: image: lemmy-federation:latest + volumes: + - ./lemmy_beta.hjson:/config/config.hjson environment: - - LEMMY_HOSTNAME=lemmy-beta:8551 - - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_beta:5432/lemmy - - LEMMY_JWT_SECRET=changeme - - LEMMY_FEDERATION__ENABLED=true - - LEMMY_TLS_ENABLED=false - - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon - - LEMMY_PORT=8551 - - LEMMY_SETUP__ADMIN_USERNAME=lemmy_beta - - LEMMY_SETUP__ADMIN_PASSWORD=lemmy - - LEMMY_SETUP__SITE_NAME=lemmy-beta - - LEMMY_RATE_LIMIT__POST=99999 - - LEMMY_RATE_LIMIT__REGISTER=99999 - - LEMMY_CAPTCHA__ENABLED=false - LEMMY_TEST_SEND_SYNC=1 - RUST_BACKTRACE=1 - RUST_LOG=debug @@ -118,20 +96,9 @@ services: - lemmy-gamma lemmy-gamma: image: lemmy-federation:latest + volumes: + - ./lemmy_gamma.hjson:/config/config.hjson environment: - - LEMMY_HOSTNAME=lemmy-gamma:8561 - - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_gamma:5432/lemmy - - LEMMY_JWT_SECRET=changeme - - LEMMY_FEDERATION__ENABLED=true - - LEMMY_TLS_ENABLED=false - - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon - - LEMMY_PORT=8561 - - LEMMY_SETUP__ADMIN_USERNAME=lemmy_gamma - - LEMMY_SETUP__ADMIN_PASSWORD=lemmy - - LEMMY_SETUP__SITE_NAME=lemmy-gamma - - LEMMY_RATE_LIMIT__POST=99999 - - LEMMY_RATE_LIMIT__REGISTER=99999 - - LEMMY_CAPTCHA__ENABLED=false - LEMMY_TEST_SEND_SYNC=1 - RUST_BACKTRACE=1 - RUST_LOG=debug @@ -159,20 +126,9 @@ services: - lemmy-delta lemmy-delta: image: lemmy-federation:latest + volumes: + - ./lemmy_delta.hjson:/config/config.hjson environment: - - LEMMY_HOSTNAME=lemmy-delta:8571 - - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_delta:5432/lemmy - - LEMMY_JWT_SECRET=changeme - - LEMMY_FEDERATION__ENABLED=true - - LEMMY_TLS_ENABLED=false - - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta - - LEMMY_PORT=8571 - - LEMMY_SETUP__ADMIN_USERNAME=lemmy_delta - - LEMMY_SETUP__ADMIN_PASSWORD=lemmy - - LEMMY_SETUP__SITE_NAME=lemmy-delta - - LEMMY_RATE_LIMIT__POST=99999 - - LEMMY_RATE_LIMIT__REGISTER=99999 - - LEMMY_CAPTCHA__ENABLED=false - LEMMY_TEST_SEND_SYNC=1 - RUST_BACKTRACE=1 - RUST_LOG=debug @@ -200,20 +156,9 @@ services: - lemmy-epsilon lemmy-epsilon: image: lemmy-federation:latest + volumes: + - ./lemmy_epsilon.hjson:/config/config.hjson environment: - - LEMMY_HOSTNAME=lemmy-epsilon:8581 - - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_epsilon:5432/lemmy - - LEMMY_JWT_SECRET=changeme - - LEMMY_FEDERATION__ENABLED=true - - LEMMY_TLS_ENABLED=false - - LEMMY_FEDERATION__BLOCKED_INSTANCES=lemmy-alpha - - LEMMY_PORT=8581 - - LEMMY_SETUP__ADMIN_USERNAME=lemmy_epsilon - - LEMMY_SETUP__ADMIN_PASSWORD=lemmy - - LEMMY_SETUP__SITE_NAME=lemmy-epsilon - - LEMMY_RATE_LIMIT__POST=99999 - - LEMMY_RATE_LIMIT__REGISTER=99999 - - LEMMY_CAPTCHA__ENABLED=false - LEMMY_TEST_SEND_SYNC=1 - RUST_BACKTRACE=1 - RUST_LOG=debug diff --git a/docker/prod/Dockerfile b/docker/prod/Dockerfile index 8f307bf602..c4f0358d1f 100644 --- a/docker/prod/Dockerfile +++ b/docker/prod/Dockerfile @@ -56,7 +56,6 @@ RUN addgroup -g 1000 lemmy RUN adduser -D -s /bin/sh -u 1000 -G lemmy lemmy # Copy resources -COPY --chown=lemmy:lemmy config/defaults.hjson /config/defaults.hjson COPY --chown=lemmy:lemmy --from=builder /app/lemmy_server /app/lemmy RUN chown lemmy:lemmy /app/lemmy diff --git a/docker/prod/Dockerfile.arm b/docker/prod/Dockerfile.arm index bcf590034b..a6e698f0d7 100644 --- a/docker/prod/Dockerfile.arm +++ b/docker/prod/Dockerfile.arm @@ -31,7 +31,6 @@ RUN addgroup --gid 1000 lemmy RUN adduser --no-create-home --shell /bin/sh --uid 1000 --gid 1000 lemmy # Copy resources -COPY --chown=lemmy:lemmy config/defaults.hjson /config/defaults.hjson COPY --chown=lemmy:lemmy --from=builder /app/lemmy_server /app/lemmy RUN chown lemmy:lemmy /app/lemmy From 1a02ab348227833ab96624d6a06880154ee0183b Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 17 Feb 2021 11:22:49 -0500 Subject: [PATCH 3/9] Trying to fix tests, try 2 --- docker/federation/lemmy_alpha.hjson | 37 +++++++++++++++++++++++++++ docker/federation/lemmy_beta.hjson | 37 +++++++++++++++++++++++++++ docker/federation/lemmy_delta.hjson | 37 +++++++++++++++++++++++++++ docker/federation/lemmy_epsilon.hjson | 37 +++++++++++++++++++++++++++ docker/federation/lemmy_gamma.hjson | 37 +++++++++++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 docker/federation/lemmy_alpha.hjson create mode 100644 docker/federation/lemmy_beta.hjson create mode 100644 docker/federation/lemmy_delta.hjson create mode 100644 docker/federation/lemmy_epsilon.hjson create mode 100644 docker/federation/lemmy_gamma.hjson diff --git a/docker/federation/lemmy_alpha.hjson b/docker/federation/lemmy_alpha.hjson new file mode 100644 index 0000000000..3f8fc03d6e --- /dev/null +++ b/docker/federation/lemmy_alpha.hjson @@ -0,0 +1,37 @@ +{ + hostname: lemmy-alpha:8541 + port: 8541 + tls_enabled: false + jwt_secret: changeme + setup: { + admin_username: lemmy_alpha + admin_password: lemmy + site_name: lemmy-alpha + } + database: { + database: lemmy + user: lemmy + password: password + host: postgres_alpha + port: 5432 + pool_size: 5 + } + federation: { + enabled: true + allowed_instances: lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon + } + captcha: { + enabled: false + difficulty: medium + } + rate_limit: { + message: 180 + message_per_second: 60 + post: 99999 + post_per_second: 600 + register: 99999 + register_per_second: 3600 + image: 6 + image_per_second: 3600 + } +} diff --git a/docker/federation/lemmy_beta.hjson b/docker/federation/lemmy_beta.hjson new file mode 100644 index 0000000000..f23d8f1c5e --- /dev/null +++ b/docker/federation/lemmy_beta.hjson @@ -0,0 +1,37 @@ +{ + hostname: lemmy-beta:8551 + port: 8551 + tls_enabled: false + jwt_secret: changeme + setup: { + admin_username: lemmy_beta + admin_password: lemmy + site_name: lemmy-beta + } + database: { + database: lemmy + user: lemmy + password: password + host: postgres_beta + port: 5432 + pool_size: 5 + } + federation: { + enabled: true + allowed_instances: lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon + } + captcha: { + enabled: false + difficulty: medium + } + rate_limit: { + message: 180 + message_per_second: 60 + post: 99999 + post_per_second: 600 + register: 99999 + register_per_second: 3600 + image: 6 + image_per_second: 3600 + } +} diff --git a/docker/federation/lemmy_delta.hjson b/docker/federation/lemmy_delta.hjson new file mode 100644 index 0000000000..f4fb0ecc54 --- /dev/null +++ b/docker/federation/lemmy_delta.hjson @@ -0,0 +1,37 @@ +{ + hostname: lemmy-delta:8571 + port: 8571 + tls_enabled: false + jwt_secret: changeme + setup: { + admin_username: lemmy_delta + admin_password: lemmy + site_name: lemmy-delta + } + database: { + database: lemmy + user: lemmy + password: password + host: postgres_delta + port: 5432 + pool_size: 5 + } + federation: { + enabled: true + allowed_instances: lemmy-beta + } + captcha: { + enabled: false + difficulty: medium + } + rate_limit: { + message: 180 + message_per_second: 60 + post: 99999 + post_per_second: 600 + register: 99999 + register_per_second: 3600 + image: 6 + image_per_second: 3600 + } +} diff --git a/docker/federation/lemmy_epsilon.hjson b/docker/federation/lemmy_epsilon.hjson new file mode 100644 index 0000000000..c04c312769 --- /dev/null +++ b/docker/federation/lemmy_epsilon.hjson @@ -0,0 +1,37 @@ +{ + hostname: lemmy-epsilon:8581 + port: 8581 + tls_enabled: false + jwt_secret: changeme + setup: { + admin_username: lemmy_epsilon + admin_password: lemmy + site_name: lemmy-epsilon + } + database: { + database: lemmy + user: lemmy + password: password + host: postgres_epsilon + port: 5432 + pool_size: 5 + } + federation: { + enabled: true + blocked_instances: lemmy-alpha + } + captcha: { + enabled: false + difficulty: medium + } + rate_limit: { + message: 180 + message_per_second: 60 + post: 99999 + post_per_second: 600 + register: 99999 + register_per_second: 3600 + image: 6 + image_per_second: 3600 + } +} diff --git a/docker/federation/lemmy_gamma.hjson b/docker/federation/lemmy_gamma.hjson new file mode 100644 index 0000000000..f08e2e63f2 --- /dev/null +++ b/docker/federation/lemmy_gamma.hjson @@ -0,0 +1,37 @@ +{ + hostname: lemmy-gamma:8561 + port: 8561 + tls_enabled: false + jwt_secret: changeme + setup: { + admin_username: lemmy_gamma + admin_password: lemmy + site_name: lemmy-gamma + } + database: { + database: lemmy + user: lemmy + password: password + host: postgres_gamma + port: 5432 + pool_size: 5 + } + federation: { + enabled: true + allowed_instances: lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon + } + captcha: { + enabled: false + difficulty: medium + } + rate_limit: { + message: 180 + message_per_second: 60 + post: 99999 + post_per_second: 600 + register: 99999 + register_per_second: 3600 + image: 6 + image_per_second: 3600 + } +} From 755c6b4a99b94bbc98d55a0671d699767dc5b328 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 18 Feb 2021 19:09:05 -0500 Subject: [PATCH 4/9] A few fixes to deser_hjson - Removing unwrap_or_defaults, using impl functions. - Reorganized settings --- ansible/templates/config.hjson | 5 +- config/defaults.hjson | 5 +- crates/api/src/lib.rs | 13 +- crates/api/src/site.rs | 2 +- crates/api/src/user.rs | 21 +- crates/apub/src/activity_queue.rs | 4 +- crates/apub/src/inbox/mod.rs | 2 +- crates/apub/src/lib.rs | 17 +- crates/apub/src/objects/mod.rs | 2 +- crates/apub/src/objects/user.rs | 2 +- crates/apub/src/routes.rs | 7 +- crates/routes/src/feeds.rs | 2 +- crates/routes/src/images.rs | 12 +- crates/routes/src/nodeinfo.rs | 2 +- crates/routes/src/webfinger.rs | 2 +- crates/structs/src/lib.rs | 2 +- crates/structs/src/site.rs | 4 +- crates/utils/src/claims.rs | 4 +- crates/utils/src/email.rs | 4 +- crates/utils/src/lib.rs | 5 +- crates/utils/src/rate_limit/mod.rs | 4 +- crates/utils/src/request.rs | 8 +- crates/utils/src/settings/captcha_config.rs | 16 ++ crates/utils/src/settings/database_config.rs | 24 ++ crates/utils/src/settings/email_config.rs | 10 + .../utils/src/settings/federation_config.rs | 18 ++ .../src/{settings.rs => settings/mod.rs} | 227 ++++++------------ .../utils/src/settings/rate_limit_config.rs | 28 +++ crates/utils/src/settings/setup_config.rs | 9 + crates/utils/src/utils.rs | 5 +- docker/federation/lemmy_alpha.hjson | 2 +- docker/federation/lemmy_beta.hjson | 2 +- docker/federation/lemmy_delta.hjson | 2 +- docker/federation/lemmy_epsilon.hjson | 2 +- docker/federation/lemmy_gamma.hjson | 2 +- src/main.rs | 8 +- tests/integration_test.rs | 2 +- 37 files changed, 253 insertions(+), 233 deletions(-) create mode 100644 crates/utils/src/settings/captcha_config.rs create mode 100644 crates/utils/src/settings/database_config.rs create mode 100644 crates/utils/src/settings/email_config.rs create mode 100644 crates/utils/src/settings/federation_config.rs rename crates/utils/src/{settings.rs => settings/mod.rs} (51%) create mode 100644 crates/utils/src/settings/rate_limit_config.rs create mode 100644 crates/utils/src/settings/setup_config.rs diff --git a/ansible/templates/config.hjson b/ansible/templates/config.hjson index 29da81c345..10624f4d27 100644 --- a/ansible/templates/config.hjson +++ b/ansible/templates/config.hjson @@ -29,8 +29,9 @@ # https://join.lemmy.ml/docs/en/federation/administration.html#instance-allowlist-and-blocklist # # comma separated list of instances with which federation is allowed - # allowed_instances: "" + # Only one of these blocks should be uncommented + # allowed_instances: [instance1.tld,instance2.tld] # comma separated list of instances which are blocked from federating - # blocked_instances: "" + # blocked_instances: [] } } diff --git a/config/defaults.hjson b/config/defaults.hjson index 6c6a68fc2d..0b6582110e 100644 --- a/config/defaults.hjson +++ b/config/defaults.hjson @@ -66,9 +66,10 @@ # https://join.lemmy.ml/docs/en/federation/administration.html#instance-allowlist-and-blocklist # # comma separated list of instances with which federation is allowed - allowed_instances: "" + # Only one of these blocks should be uncommented + # allowed_instances: [instance1.tld,instance2.tld] # comma separated list of instances which are blocked from federating - blocked_instances: "" + # blocked_instances: [] } captcha: { enabled: true diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 779da57f4c..fbc1e8ed10 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -184,7 +184,7 @@ pub(crate) fn check_optional_url(item: &Option>) -> Result<(), Le pub(crate) async fn build_federated_instances( pool: &DbPool, ) -> Result, LemmyError> { - if Settings::get().federation.unwrap_or_default().enabled { + if Settings::get().federation().enabled { let distinct_communities = blocking(pool, move |conn| { Community::distinct_federated_communities(conn) }) @@ -198,10 +198,13 @@ pub(crate) async fn build_federated_instances( .map(|actor_id| Ok(Url::parse(actor_id)?.host_str().unwrap_or("").to_string())) .collect::, LemmyError>>()?; - linked.extend_from_slice(&allowed); - linked.retain(|a| { - !blocked.contains(a) && !a.eq("") && !a.eq(&Settings::get().hostname.unwrap_or_default()) - }); + if let Some(allowed) = allowed.as_ref() { + linked.extend_from_slice(allowed); + } + + if let Some(blocked) = blocked.as_ref() { + linked.retain(|a| !blocked.contains(a) && !a.eq(&Settings::get().hostname())); + } // Sort and remove dupes linked.sort_unstable(); diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index 1bdce91abd..9551024fa2 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -270,7 +270,7 @@ impl Perform for GetSite { Ok(site_view) => Some(site_view), // If the site isn't created yet, check the setup Err(_) => { - if let Some(setup) = Settings::get().setup.as_ref() { + if let Some(setup) = Settings::get().setup().as_ref() { let register = Register { username: setup.admin_username.to_owned(), email: setup.admin_email.to_owned(), diff --git a/crates/api/src/user.rs b/crates/api/src/user.rs index 0d11065cf0..c2382231ce 100644 --- a/crates/api/src/user.rs +++ b/crates/api/src/user.rs @@ -121,7 +121,7 @@ impl Perform for Login { // Return the jwt Ok(LoginResponse { - jwt: Claims::jwt(user.id, Settings::get().hostname.unwrap_or_default())?, + jwt: Claims::jwt(user.id, Settings::get().hostname())?, }) } } @@ -161,7 +161,7 @@ impl Perform for Register { .await??; // If its not the admin, check the captcha - if !no_admins && Settings::get().captcha.unwrap_or_default().enabled { + if !no_admins && Settings::get().captcha().enabled { let check = context .chat_server() .send(CheckCaptcha { @@ -303,10 +303,7 @@ impl Perform for Register { // Return the jwt Ok(LoginResponse { - jwt: Claims::jwt( - inserted_user.id, - Settings::get().hostname.unwrap_or_default(), - )?, + jwt: Claims::jwt(inserted_user.id, Settings::get().hostname())?, }) } } @@ -320,7 +317,7 @@ impl Perform for GetCaptcha { context: &Data, _websocket_id: Option, ) -> Result { - let captcha_settings = Settings::get().captcha.unwrap_or_default(); + let captcha_settings = Settings::get().captcha(); if !captcha_settings.enabled { return Ok(GetCaptchaResponse { ok: None }); @@ -479,10 +476,7 @@ impl Perform for SaveUserSettings { // Return the jwt Ok(LoginResponse { - jwt: Claims::jwt( - updated_user.id, - Settings::get().hostname.unwrap_or_default(), - )?, + jwt: Claims::jwt(updated_user.id, Settings::get().hostname())?, }) } } @@ -1018,10 +1012,7 @@ impl Perform for PasswordChange { // Return the jwt Ok(LoginResponse { - jwt: Claims::jwt( - updated_user.id, - Settings::get().hostname.unwrap_or_default(), - )?, + jwt: Claims::jwt(updated_user.id, Settings::get().hostname())?, }) } } diff --git a/crates/apub/src/activity_queue.rs b/crates/apub/src/activity_queue.rs index b4ccac754e..9586fd38f5 100644 --- a/crates/apub/src/activity_queue.rs +++ b/crates/apub/src/activity_queue.rs @@ -88,7 +88,7 @@ where .await? .iter() .unique() - .filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname.unwrap_or_default())) + .filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname())) .filter(|inbox| check_is_apub_id_valid(inbox).is_ok()) .map(|inbox| inbox.to_owned()) .collect(); @@ -215,7 +215,7 @@ where Kind: Serialize, >::Error: From + Send + Sync + 'static, { - if !Settings::get().federation.unwrap_or_default().enabled || inboxes.is_empty() { + if !Settings::get().federation().enabled || inboxes.is_empty() { return Ok(()); } diff --git a/crates/apub/src/inbox/mod.rs b/crates/apub/src/inbox/mod.rs index 751992966e..9c787e7d96 100644 --- a/crates/apub/src/inbox/mod.rs +++ b/crates/apub/src/inbox/mod.rs @@ -167,7 +167,7 @@ where let id = activity.id_unchecked().context(location_info!())?; let activity_domain = id.domain().context(location_info!())?; - if activity_domain == Settings::get().hostname.unwrap_or_default() { + if activity_domain == Settings::get().hostname() { return Err( anyhow!( "Error: received activity which was sent by local instance: {:?}", diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 5d6744e334..0d6f489232 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -64,7 +64,7 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> { let domain = apub_id.domain().context(location_info!())?.to_string(); let local_instance = settings.get_hostname_without_port()?; - if !settings.federation.unwrap_or_default().enabled { + if !settings.federation().enabled { return if domain == local_instance { Ok(()) } else { @@ -88,22 +88,23 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> { return Err(anyhow!("invalid apub id scheme {}: {}", apub_id.scheme(), apub_id).into()); } - let mut allowed_instances = Settings::get().get_allowed_instances(); + let allowed_instances = Settings::get().get_allowed_instances(); let blocked_instances = Settings::get().get_blocked_instances(); - if allowed_instances.is_empty() && blocked_instances.is_empty() { + + if allowed_instances.is_none() && blocked_instances.is_none() { Ok(()) - } else if !allowed_instances.is_empty() { + } else if let Some(mut allowed) = allowed_instances { // need to allow this explicitly because apub receive might contain objects from our local // instance. split is needed to remove the port in our federation test setup. - allowed_instances.push(local_instance); + allowed.push(local_instance); - if allowed_instances.contains(&domain) { + if allowed.contains(&domain) { Ok(()) } else { Err(anyhow!("{} not in federation allowlist", domain).into()) } - } else if !blocked_instances.is_empty() { - if blocked_instances.contains(&domain) { + } else if let Some(blocked) = blocked_instances { + if blocked.contains(&domain) { Err(anyhow!("{} is in federation blocklist", domain).into()) } else { Ok(()) diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index f6910cce00..285266342b 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -189,7 +189,7 @@ where let domain = object_id.domain().context(location_info!())?; // if its a local object, return it directly from the database - if Settings::get().hostname.unwrap_or_default() == domain { + if Settings::get().hostname() == domain { let object = blocking(context.pool(), move |conn| { To::read_from_apub_id(conn, &object_id.into()) }) diff --git a/crates/apub/src/objects/user.rs b/crates/apub/src/objects/user.rs index c23f4c509a..1474bb4dd0 100644 --- a/crates/apub/src/objects/user.rs +++ b/crates/apub/src/objects/user.rs @@ -99,7 +99,7 @@ impl FromApub for User_ { ) -> Result { let user_id = person.id_unchecked().context(location_info!())?.to_owned(); let domain = user_id.domain().context(location_info!())?; - if domain == Settings::get().hostname.unwrap_or_default() { + if domain == Settings::get().hostname() { let user = blocking(context.pool(), move |conn| { User_::read_from_apub_id(conn, &user_id.into()) }) diff --git a/crates/apub/src/routes.rs b/crates/apub/src/routes.rs index ac946831ae..eea9534443 100644 --- a/crates/apub/src/routes.rs +++ b/crates/apub/src/routes.rs @@ -23,11 +23,8 @@ static APUB_JSON_CONTENT_TYPE_LONG: &str = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""; pub fn config(cfg: &mut web::ServiceConfig) { - if Settings::get().federation.unwrap_or_default().enabled { - println!( - "federation enabled, host is {}", - Settings::get().hostname.unwrap_or_default() - ); + if Settings::get().federation().enabled { + println!("federation enabled, host is {}", Settings::get().hostname()); let digest_verifier = VerifyDigest::new(Sha256::new()); let header_guard_accept = guard::Any(guard::Header("Accept", APUB_JSON_CONTENT_TYPE)) diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 6e0ae8e1fd..7885247f50 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -163,7 +163,7 @@ fn get_feed_user( ) -> Result { let site_view = SiteView::read(&conn)?; let user = User_::find_by_username(&conn, &user_name)?; - let user_url = user.get_profile_url(&Settings::get().hostname.unwrap_or_default()); + let user_url = user.get_profile_url(&Settings::get().hostname()); let posts = PostQueryBuilder::create(&conn) .listing_type(&ListingType::All) diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index e58f59e31b..8b8c9e9849 100644 --- a/crates/routes/src/images.rs +++ b/crates/routes/src/images.rs @@ -55,7 +55,7 @@ async fn upload( }; let mut client_req = client.request_from( - format!("{}/image", Settings::get().pictrs_url.unwrap_or_default()), + format!("{}/image", Settings::get().pictrs_url()), req.head(), ); @@ -80,18 +80,14 @@ async fn full_res( // If there are no query params, the URL is original let url = if params.format.is_none() && params.thumbnail.is_none() { - format!( - "{}/image/original/{}", - Settings::get().pictrs_url.unwrap_or_default(), - name, - ) + format!("{}/image/original/{}", Settings::get().pictrs_url(), name,) } else { // Use jpg as a default when none is given let format = params.format.unwrap_or_else(|| "jpg".to_string()); let mut url = format!( "{}/image/process.{}?src={}", - Settings::get().pictrs_url.unwrap_or_default(), + Settings::get().pictrs_url(), format, name, ); @@ -140,7 +136,7 @@ async fn delete( let url = format!( "{}/image/delete/{}/{}", - Settings::get().pictrs_url.unwrap_or_default(), + Settings::get().pictrs_url(), &token, &file ); diff --git a/crates/routes/src/nodeinfo.rs b/crates/routes/src/nodeinfo.rs index b9768d9a0e..f0b9d9dde8 100644 --- a/crates/routes/src/nodeinfo.rs +++ b/crates/routes/src/nodeinfo.rs @@ -31,7 +31,7 @@ async fn node_info(context: web::Data) -> Result, - pub allowed: Vec, - pub blocked: Vec, + pub allowed: Option>, + pub blocked: Option>, } diff --git a/crates/utils/src/claims.rs b/crates/utils/src/claims.rs index 1f9404742b..6d6f6e1c70 100644 --- a/crates/utils/src/claims.rs +++ b/crates/utils/src/claims.rs @@ -18,7 +18,7 @@ impl Claims { }; decode::( &jwt, - &DecodingKey::from_secret(Settings::get().jwt_secret.unwrap_or_default().as_ref()), + &DecodingKey::from_secret(Settings::get().jwt_secret().as_ref()), &v, ) } @@ -31,7 +31,7 @@ impl Claims { encode( &Header::default(), &my_claims, - &EncodingKey::from_secret(Settings::get().jwt_secret.unwrap_or_default().as_ref()), + &EncodingKey::from_secret(Settings::get().jwt_secret().as_ref()), ) } } diff --git a/crates/utils/src/email.rs b/crates/utils/src/email.rs index 31ee4133da..09c0f55295 100644 --- a/crates/utils/src/email.rs +++ b/crates/utils/src/email.rs @@ -19,8 +19,8 @@ pub fn send_email( to_username: &str, html: &str, ) -> Result<(), String> { - let email_config = Settings::get().email.ok_or("no_email_setup")?; - let domain = Settings::get().hostname.unwrap_or_default(); + let email_config = Settings::get().email().ok_or("no_email_setup")?; + let domain = Settings::get().hostname(); let (smtp_server, smtp_port) = { let email_and_port = email_config.smtp_server.split(':').collect::>(); diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 9fb56d4b91..9df2b60300 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -9,6 +9,7 @@ pub mod email; pub mod rate_limit; pub mod request; pub mod settings; + #[cfg(test)] mod test; pub mod utils; @@ -83,12 +84,12 @@ impl actix_web::error::ResponseError for LemmyError { lazy_static! { pub static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!( "^group:([a-z0-9_]{{3, 20}})@{}$", - Settings::get().hostname.unwrap_or_default() + Settings::get().hostname() )) .unwrap(); pub static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!( "^acct:([a-z0-9_]{{3, 20}})@{}$", - Settings::get().hostname.unwrap_or_default() + Settings::get().hostname() )) .unwrap(); } diff --git a/crates/utils/src/rate_limit/mod.rs b/crates/utils/src/rate_limit/mod.rs index f1c6e360d9..ca24a2b8f1 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::{rate_limit_config::RateLimitConfig, Settings}, utils::get_ip, LemmyError, }; @@ -70,7 +70,7 @@ impl RateLimited { { // Does not need to be blocking because the RwLock in settings never held across await points, // and the operation here locks only long enough to clone - let rate_limit: RateLimitConfig = Settings::get().rate_limit.unwrap_or_default(); + let rate_limit: RateLimitConfig = Settings::get().rate_limit(); // before { diff --git a/crates/utils/src/request.rs b/crates/utils/src/request.rs index 805f1b130f..e06226abac 100644 --- a/crates/utils/src/request.rs +++ b/crates/utils/src/request.rs @@ -58,11 +58,7 @@ pub(crate) async fn fetch_iframely( client: &Client, url: &str, ) -> Result { - let fetch_url = format!( - "{}/oembed?url={}", - Settings::get().iframely_url.unwrap_or_default(), - url - ); + let fetch_url = format!("{}/oembed?url={}", Settings::get().iframely_url(), url); let response = retry(|| client.get(&fetch_url).send()).await?; @@ -93,7 +89,7 @@ pub(crate) async fn fetch_pictrs( let fetch_url = format!( "{}/image/download?url={}", - Settings::get().pictrs_url.unwrap_or_default(), + Settings::get().pictrs_url(), utf8_percent_encode(image_url, NON_ALPHANUMERIC) // TODO this might not be needed ); diff --git a/crates/utils/src/settings/captcha_config.rs b/crates/utils/src/settings/captcha_config.rs new file mode 100644 index 0000000000..ab62940eb1 --- /dev/null +++ b/crates/utils/src/settings/captcha_config.rs @@ -0,0 +1,16 @@ +use serde::Deserialize; + +#[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(), + } + } +} diff --git a/crates/utils/src/settings/database_config.rs b/crates/utils/src/settings/database_config.rs new file mode 100644 index 0000000000..b234827943 --- /dev/null +++ b/crates/utils/src/settings/database_config.rs @@ -0,0 +1,24 @@ +use serde::Deserialize; + +#[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, + } + } +} diff --git a/crates/utils/src/settings/email_config.rs b/crates/utils/src/settings/email_config.rs new file mode 100644 index 0000000000..ce27455594 --- /dev/null +++ b/crates/utils/src/settings/email_config.rs @@ -0,0 +1,10 @@ +use serde::Deserialize; + +#[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, +} diff --git a/crates/utils/src/settings/federation_config.rs b/crates/utils/src/settings/federation_config.rs new file mode 100644 index 0000000000..963c56d0e3 --- /dev/null +++ b/crates/utils/src/settings/federation_config.rs @@ -0,0 +1,18 @@ +use serde::Deserialize; + +#[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: None, + blocked_instances: None, + } + } +} diff --git a/crates/utils/src/settings.rs b/crates/utils/src/settings/mod.rs similarity index 51% rename from crates/utils/src/settings.rs rename to crates/utils/src/settings/mod.rs index 0a6859f9be..f41b0b33e3 100644 --- a/crates/utils/src/settings.rs +++ b/crates/utils/src/settings/mod.rs @@ -1,4 +1,15 @@ -use crate::{location_info, LemmyError}; +use crate::{ + location_info, + settings::{ + captcha_config::CaptchaConfig, + database_config::DatabaseConfig, + email_config::EmailConfig, + federation_config::FederationConfig, + rate_limit_config::RateLimitConfig, + setup_config::SetupConfig, + }, + LemmyError, +}; use anyhow::Context; use deser_hjson::from_str; use merge::Merge; @@ -11,23 +22,30 @@ use std::{ sync::RwLock, }; +pub(crate) mod captcha_config; +pub(crate) mod database_config; +pub(crate) mod email_config; +pub(crate) mod federation_config; +pub(crate) mod rate_limit_config; +pub(crate) mod setup_config; + 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, + database: Option, + rate_limit: Option, + federation: Option, + hostname: Option, + bind: Option, + port: Option, + tls_enabled: Option, + jwt_secret: Option, + pictrs_url: Option, + iframely_url: Option, + captcha: Option, + email: Option, + setup: Option, } impl Default for Settings { @@ -50,105 +68,6 @@ impl Default for Settings { } } -#[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, @@ -180,7 +99,7 @@ impl Settings { } pub fn get_database_url(&self) -> String { - let conf = self.database.to_owned().unwrap_or_default(); + let conf = self.database(); format!( "postgres://{}:{}@{}:{}/{}", conf.user, conf.password, conf.host, conf.port, conf.database, @@ -195,34 +114,12 @@ impl Settings { fs::read_to_string(Self::get_config_location()) } - pub fn get_allowed_instances(&self) -> Vec { - let mut allowed_instances: Vec = self - .federation - .to_owned() - .unwrap_or_default() - .allowed_instances - .unwrap_or_default() - .split(',') - .map(|d| d.trim().to_string()) - .collect(); - - allowed_instances.retain(|d| !d.eq("")); - allowed_instances + pub fn get_allowed_instances(&self) -> Option> { + self.federation().allowed_instances.to_owned() } - pub fn get_blocked_instances(&self) -> Vec { - let mut blocked_instances: Vec = self - .federation - .to_owned() - .unwrap_or_default() - .blocked_instances - .unwrap_or_default() - .split(',') - .map(|d| d.trim().to_string()) - .collect(); - - blocked_instances.retain(|d| !d.eq("")); - blocked_instances + pub fn get_blocked_instances(&self) -> Option> { + self.federation().blocked_instances.to_owned() } /// Returns either "http" or "https", depending on tls_enabled setting @@ -241,11 +138,7 @@ impl Settings { /// Returns something like `http://localhost` or `https://lemmy.ml`, /// with the correct protocol and hostname. pub fn get_protocol_and_hostname(&self) -> String { - format!( - "{}://{}", - self.get_protocol_string(), - self.hostname.to_owned().unwrap_or_default() - ) + format!("{}://{}", self.get_protocol_string(), self.hostname()) } /// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname` @@ -254,9 +147,7 @@ impl Settings { pub fn get_hostname_without_port(&self) -> Result { Ok( self - .hostname - .to_owned() - .unwrap_or_default() + .hostname() .split(':') .collect::>() .first() @@ -278,4 +169,44 @@ impl Settings { Self::read_config_file() } + + pub fn database(&self) -> DatabaseConfig { + self.database.to_owned().unwrap_or_default() + } + pub fn hostname(&self) -> String { + self.hostname.to_owned().unwrap_or_default() + } + pub fn bind(&self) -> IpAddr { + self.bind.unwrap() + } + pub fn port(&self) -> u16 { + self.port.unwrap_or_default() + } + pub fn tls_enabled(&self) -> bool { + self.tls_enabled.unwrap_or_default() + } + pub fn jwt_secret(&self) -> String { + self.jwt_secret.to_owned().unwrap_or_default() + } + pub fn pictrs_url(&self) -> String { + self.pictrs_url.to_owned().unwrap_or_default() + } + pub fn iframely_url(&self) -> String { + self.iframely_url.to_owned().unwrap_or_default() + } + pub fn rate_limit(&self) -> RateLimitConfig { + self.rate_limit.to_owned().unwrap_or_default() + } + pub fn federation(&self) -> FederationConfig { + self.federation.to_owned().unwrap_or_default() + } + pub fn captcha(&self) -> CaptchaConfig { + self.captcha.to_owned().unwrap_or_default() + } + pub fn email(&self) -> Option { + self.email.to_owned() + } + pub fn setup(&self) -> Option { + self.setup.to_owned() + } } diff --git a/crates/utils/src/settings/rate_limit_config.rs b/crates/utils/src/settings/rate_limit_config.rs new file mode 100644 index 0000000000..dbfab14f9b --- /dev/null +++ b/crates/utils/src/settings/rate_limit_config.rs @@ -0,0 +1,28 @@ +use serde::Deserialize; + +#[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, + } + } +} diff --git a/crates/utils/src/settings/setup_config.rs b/crates/utils/src/settings/setup_config.rs new file mode 100644 index 0000000000..5aa4b05620 --- /dev/null +++ b/crates/utils/src/settings/setup_config.rs @@ -0,0 +1,9 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize, Clone)] +pub struct SetupConfig { + pub admin_username: String, + pub admin_password: String, + pub admin_email: Option, + pub site_name: String, +} diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index dc701b54fc..feaa7292b0 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -85,10 +85,7 @@ pub struct MentionData { impl MentionData { pub fn is_local(&self) -> bool { - Settings::get() - .hostname - .unwrap_or_default() - .eq(&self.domain) + Settings::get().hostname().eq(&self.domain) } pub fn full_name(&self) -> String { format!("@{}@{}", &self.name, &self.domain) diff --git a/docker/federation/lemmy_alpha.hjson b/docker/federation/lemmy_alpha.hjson index 3f8fc03d6e..a253208a60 100644 --- a/docker/federation/lemmy_alpha.hjson +++ b/docker/federation/lemmy_alpha.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - allowed_instances: lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon + allowed_instances: [lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon] } captcha: { enabled: false diff --git a/docker/federation/lemmy_beta.hjson b/docker/federation/lemmy_beta.hjson index f23d8f1c5e..08b070205d 100644 --- a/docker/federation/lemmy_beta.hjson +++ b/docker/federation/lemmy_beta.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - allowed_instances: lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon + allowed_instances: [lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon] } captcha: { enabled: false diff --git a/docker/federation/lemmy_delta.hjson b/docker/federation/lemmy_delta.hjson index f4fb0ecc54..2855c5077d 100644 --- a/docker/federation/lemmy_delta.hjson +++ b/docker/federation/lemmy_delta.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - allowed_instances: lemmy-beta + allowed_instances: [lemmy-beta] } captcha: { enabled: false diff --git a/docker/federation/lemmy_epsilon.hjson b/docker/federation/lemmy_epsilon.hjson index c04c312769..8845db2f82 100644 --- a/docker/federation/lemmy_epsilon.hjson +++ b/docker/federation/lemmy_epsilon.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - blocked_instances: lemmy-alpha + blocked_instances: [lemmy-alpha] } captcha: { enabled: false diff --git a/docker/federation/lemmy_gamma.hjson b/docker/federation/lemmy_gamma.hjson index f08e2e63f2..b85a87d132 100644 --- a/docker/federation/lemmy_gamma.hjson +++ b/docker/federation/lemmy_gamma.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - allowed_instances: lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon + allowed_instances: [lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon] } captcha: { enabled: false diff --git a/src/main.rs b/src/main.rs index 6ca5253d4a..0c3c10fb8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ async fn main() -> Result<(), LemmyError> { }; let manager = ConnectionManager::::new(&db_url); let pool = Pool::builder() - .max_size(settings.database.unwrap_or_default().pool_size) + .max_size(settings.database().pool_size) .build(manager) .unwrap_or_else(|_| panic!("Error connecting to {}", db_url)); @@ -61,8 +61,8 @@ async fn main() -> Result<(), LemmyError> { println!( "Starting http server at {}:{}", - settings.bind.unwrap(), - settings.port.unwrap_or_default() + settings.bind(), + settings.port() ); let activity_queue = create_activity_queue(); @@ -95,7 +95,7 @@ async fn main() -> Result<(), LemmyError> { .configure(nodeinfo::config) .configure(webfinger::config) }) - .bind((settings.bind.unwrap(), settings.port.unwrap_or_default()))? + .bind((settings.bind(), settings.port()))? .run() .await?; diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 6658cc726b..4a95951c17 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -58,7 +58,7 @@ fn create_context() -> LemmyContext { }; let manager = ConnectionManager::::new(&db_url); let pool = Pool::builder() - .max_size(settings.database.unwrap_or_default().pool_size) + .max_size(settings.database().pool_size) .build(manager) .unwrap(); embedded_migrations::run(&pool.get().unwrap()).unwrap(); From 2b4e70e3ae2a8c591c450aae1de52fd5bfd7e521 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 18 Feb 2021 19:24:18 -0500 Subject: [PATCH 5/9] Make clippy happy --- crates/utils/src/settings/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index f41b0b33e3..d1679ff8d2 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -115,11 +115,11 @@ impl Settings { } pub fn get_allowed_instances(&self) -> Option> { - self.federation().allowed_instances.to_owned() + self.federation().allowed_instances } pub fn get_blocked_instances(&self) -> Option> { - self.federation().blocked_instances.to_owned() + self.federation().blocked_instances } /// Returns either "http" or "https", depending on tls_enabled setting From 07dcbc9fba9c0230250d6faa806c50a83eb93078 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 18 Feb 2021 19:40:13 -0500 Subject: [PATCH 6/9] hjson list strings must be quoted. --- ansible/templates/config.hjson | 2 +- config/defaults.hjson | 2 +- docker/federation/lemmy_alpha.hjson | 2 +- docker/federation/lemmy_beta.hjson | 2 +- docker/federation/lemmy_delta.hjson | 2 +- docker/federation/lemmy_epsilon.hjson | 2 +- docker/federation/lemmy_gamma.hjson | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ansible/templates/config.hjson b/ansible/templates/config.hjson index 10624f4d27..55537ca50c 100644 --- a/ansible/templates/config.hjson +++ b/ansible/templates/config.hjson @@ -30,7 +30,7 @@ # # comma separated list of instances with which federation is allowed # Only one of these blocks should be uncommented - # allowed_instances: [instance1.tld,instance2.tld] + # allowed_instances: ["instance1.tld","instance2.tld"] # comma separated list of instances which are blocked from federating # blocked_instances: [] } diff --git a/config/defaults.hjson b/config/defaults.hjson index 0b6582110e..c3eaba6e5a 100644 --- a/config/defaults.hjson +++ b/config/defaults.hjson @@ -67,7 +67,7 @@ # # comma separated list of instances with which federation is allowed # Only one of these blocks should be uncommented - # allowed_instances: [instance1.tld,instance2.tld] + # allowed_instances: ["instance1.tld","instance2.tld"] # comma separated list of instances which are blocked from federating # blocked_instances: [] } diff --git a/docker/federation/lemmy_alpha.hjson b/docker/federation/lemmy_alpha.hjson index a253208a60..4819fb26ab 100644 --- a/docker/federation/lemmy_alpha.hjson +++ b/docker/federation/lemmy_alpha.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - allowed_instances: [lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon] + allowed_instances: ["lemmy-beta","lemmy-gamma","lemmy-delta","lemmy-epsilon"] } captcha: { enabled: false diff --git a/docker/federation/lemmy_beta.hjson b/docker/federation/lemmy_beta.hjson index 08b070205d..e16ecd53b6 100644 --- a/docker/federation/lemmy_beta.hjson +++ b/docker/federation/lemmy_beta.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - allowed_instances: [lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon] + allowed_instances: ["lemmy-alpha","lemmy-gamma","lemmy-delta","lemmy-epsilon"] } captcha: { enabled: false diff --git a/docker/federation/lemmy_delta.hjson b/docker/federation/lemmy_delta.hjson index 2855c5077d..321775fb56 100644 --- a/docker/federation/lemmy_delta.hjson +++ b/docker/federation/lemmy_delta.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - allowed_instances: [lemmy-beta] + allowed_instances: ["lemmy-beta"] } captcha: { enabled: false diff --git a/docker/federation/lemmy_epsilon.hjson b/docker/federation/lemmy_epsilon.hjson index 8845db2f82..20fb2c3536 100644 --- a/docker/federation/lemmy_epsilon.hjson +++ b/docker/federation/lemmy_epsilon.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - blocked_instances: [lemmy-alpha] + blocked_instances: ["lemmy-alpha"] } captcha: { enabled: false diff --git a/docker/federation/lemmy_gamma.hjson b/docker/federation/lemmy_gamma.hjson index b85a87d132..784f98eb78 100644 --- a/docker/federation/lemmy_gamma.hjson +++ b/docker/federation/lemmy_gamma.hjson @@ -18,7 +18,7 @@ } federation: { enabled: true - allowed_instances: [lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon] + allowed_instances: ["lemmy-alpha","lemmy-beta","lemmy-delta","lemmy-epsilon"] } captcha: { enabled: false From f411f511a6a0b6d60c18703ba09a22ca32aecedb Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 24 Feb 2021 19:53:25 -0500 Subject: [PATCH 7/9] Adding support for env vars. --- Cargo.lock | 10 ++++++++++ api_tests/prepare-drone-federation-test.sh | 1 + crates/utils/Cargo.toml | 1 + crates/utils/src/settings/mod.rs | 9 ++++++++- docker/federation/lemmy_alpha.hjson | 1 - 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea7fdca3a4..d2505d7f21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1101,6 +1101,15 @@ dependencies = [ "termcolor", ] +[[package]] +name = "envy" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f47e0157f2cb54f5ae1bd371b30a2ae4311e1c028f575cd4e81de7353215965" +dependencies = [ + "serde", +] + [[package]] name = "event-listener" version = "2.5.1" @@ -1953,6 +1962,7 @@ dependencies = [ "comrak", "deser-hjson", "diesel", + "envy", "futures", "http", "itertools", diff --git a/api_tests/prepare-drone-federation-test.sh b/api_tests/prepare-drone-federation-test.sh index 32eb751367..dd357db83e 100755 --- a/api_tests/prepare-drone-federation-test.sh +++ b/api_tests/prepare-drone-federation-test.sh @@ -34,6 +34,7 @@ echo "start alpha" LEMMY_HOSTNAME=lemmy-alpha:8541 \ LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_alpha.hjson \ LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_alpha" \ + LEMMY_HOSTNAME="lemmy-alpha:8541" \ target/lemmy_server >/tmp/lemmy_alpha.out 2>&1 & echo "start beta" diff --git a/crates/utils/Cargo.toml b/crates/utils/Cargo.toml index d080ea7299..3d399ccf31 100644 --- a/crates/utils/Cargo.toml +++ b/crates/utils/Cargo.toml @@ -35,3 +35,4 @@ http = "0.2.3" jsonwebtoken = "7.2.0" deser-hjson = "0.1.12" merge = "0.1.0" +envy = "0.4.2" diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index d1679ff8d2..63e9456e38 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -10,7 +10,7 @@ use crate::{ }, LemmyError, }; -use anyhow::Context; +use anyhow::{anyhow, Context}; use deser_hjson::from_str; use merge::Merge; use serde::Deserialize; @@ -87,9 +87,16 @@ impl Settings { // Read the config file let mut custom_config = from_str::(&Self::read_config_file()?)?; + // Merge with env vars + custom_config.merge(envy::prefixed("LEMMY_").from_env::()?); + // Merge with default custom_config.merge(Settings::default()); + if custom_config.hostname == Settings::default().hostname { + return Err(anyhow!("Hostname variable is not set!").into()); + } + Ok(custom_config) } diff --git a/docker/federation/lemmy_alpha.hjson b/docker/federation/lemmy_alpha.hjson index 4819fb26ab..e806397a8a 100644 --- a/docker/federation/lemmy_alpha.hjson +++ b/docker/federation/lemmy_alpha.hjson @@ -1,5 +1,4 @@ { - hostname: lemmy-alpha:8541 port: 8541 tls_enabled: false jwt_secret: changeme From dd4d75a11b1815914c54d63d1c12341023755691 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 26 Feb 2021 09:04:58 -0500 Subject: [PATCH 8/9] Moving to structs and defaults file. --- crates/utils/src/rate_limit/mod.rs | 2 +- crates/utils/src/settings/captcha_config.rs | 16 ------ crates/utils/src/settings/database_config.rs | 24 --------- crates/utils/src/settings/defaults.rs | 48 +++++++++++++++++ crates/utils/src/settings/email_config.rs | 10 ---- .../utils/src/settings/federation_config.rs | 18 ------- crates/utils/src/settings/mod.rs | 22 ++++---- .../utils/src/settings/rate_limit_config.rs | 28 ---------- crates/utils/src/settings/setup_config.rs | 9 ---- crates/utils/src/settings/structs.rs | 53 +++++++++++++++++++ 10 files changed, 111 insertions(+), 119 deletions(-) delete mode 100644 crates/utils/src/settings/captcha_config.rs delete mode 100644 crates/utils/src/settings/database_config.rs create mode 100644 crates/utils/src/settings/defaults.rs delete mode 100644 crates/utils/src/settings/email_config.rs delete mode 100644 crates/utils/src/settings/federation_config.rs delete mode 100644 crates/utils/src/settings/rate_limit_config.rs delete mode 100644 crates/utils/src/settings/setup_config.rs create mode 100644 crates/utils/src/settings/structs.rs diff --git a/crates/utils/src/rate_limit/mod.rs b/crates/utils/src/rate_limit/mod.rs index ca24a2b8f1..bd7fc1e885 100644 --- a/crates/utils/src/rate_limit/mod.rs +++ b/crates/utils/src/rate_limit/mod.rs @@ -1,5 +1,5 @@ use crate::{ - settings::{rate_limit_config::RateLimitConfig, Settings}, + settings::{structs::RateLimitConfig, Settings}, utils::get_ip, LemmyError, }; diff --git a/crates/utils/src/settings/captcha_config.rs b/crates/utils/src/settings/captcha_config.rs deleted file mode 100644 index ab62940eb1..0000000000 --- a/crates/utils/src/settings/captcha_config.rs +++ /dev/null @@ -1,16 +0,0 @@ -use serde::Deserialize; - -#[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(), - } - } -} diff --git a/crates/utils/src/settings/database_config.rs b/crates/utils/src/settings/database_config.rs deleted file mode 100644 index b234827943..0000000000 --- a/crates/utils/src/settings/database_config.rs +++ /dev/null @@ -1,24 +0,0 @@ -use serde::Deserialize; - -#[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, - } - } -} diff --git a/crates/utils/src/settings/defaults.rs b/crates/utils/src/settings/defaults.rs new file mode 100644 index 0000000000..de6da4679c --- /dev/null +++ b/crates/utils/src/settings/defaults.rs @@ -0,0 +1,48 @@ +use crate::settings::{CaptchaConfig, DatabaseConfig, FederationConfig, RateLimitConfig}; + +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 CaptchaConfig { + fn default() -> Self { + Self { + enabled: true, + difficulty: "medium".into(), + } + } +} + +impl Default for FederationConfig { + fn default() -> Self { + Self { + enabled: false, + allowed_instances: None, + blocked_instances: None, + } + } +} + +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, + } + } +} diff --git a/crates/utils/src/settings/email_config.rs b/crates/utils/src/settings/email_config.rs deleted file mode 100644 index ce27455594..0000000000 --- a/crates/utils/src/settings/email_config.rs +++ /dev/null @@ -1,10 +0,0 @@ -use serde::Deserialize; - -#[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, -} diff --git a/crates/utils/src/settings/federation_config.rs b/crates/utils/src/settings/federation_config.rs deleted file mode 100644 index 963c56d0e3..0000000000 --- a/crates/utils/src/settings/federation_config.rs +++ /dev/null @@ -1,18 +0,0 @@ -use serde::Deserialize; - -#[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: None, - blocked_instances: None, - } - } -} diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index 63e9456e38..3ab5f7d828 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -1,12 +1,12 @@ use crate::{ location_info, - settings::{ - captcha_config::CaptchaConfig, - database_config::DatabaseConfig, - email_config::EmailConfig, - federation_config::FederationConfig, - rate_limit_config::RateLimitConfig, - setup_config::SetupConfig, + settings::structs::{ + CaptchaConfig, + DatabaseConfig, + EmailConfig, + FederationConfig, + RateLimitConfig, + SetupConfig, }, LemmyError, }; @@ -22,12 +22,8 @@ use std::{ sync::RwLock, }; -pub(crate) mod captcha_config; -pub(crate) mod database_config; -pub(crate) mod email_config; -pub(crate) mod federation_config; -pub(crate) mod rate_limit_config; -pub(crate) mod setup_config; +pub(crate) mod defaults; +pub(crate) mod structs; static CONFIG_FILE: &str = "config/config.hjson"; diff --git a/crates/utils/src/settings/rate_limit_config.rs b/crates/utils/src/settings/rate_limit_config.rs deleted file mode 100644 index dbfab14f9b..0000000000 --- a/crates/utils/src/settings/rate_limit_config.rs +++ /dev/null @@ -1,28 +0,0 @@ -use serde::Deserialize; - -#[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, - } - } -} diff --git a/crates/utils/src/settings/setup_config.rs b/crates/utils/src/settings/setup_config.rs deleted file mode 100644 index 5aa4b05620..0000000000 --- a/crates/utils/src/settings/setup_config.rs +++ /dev/null @@ -1,9 +0,0 @@ -use serde::Deserialize; - -#[derive(Debug, Deserialize, Clone)] -pub struct SetupConfig { - pub admin_username: String, - pub admin_password: String, - pub admin_email: Option, - pub site_name: String, -} diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs new file mode 100644 index 0000000000..c0e063e074 --- /dev/null +++ b/crates/utils/src/settings/structs.rs @@ -0,0 +1,53 @@ +use serde::Deserialize; + +#[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 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 FederationConfig { + pub enabled: bool, + pub allowed_instances: Option>, + pub blocked_instances: Option>, +} + +#[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 SetupConfig { + pub admin_username: String, + pub admin_password: String, + pub admin_email: Option, + pub site_name: String, +} From d86bc6e34d33303c9812b42549e96f17cc14b8d5 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 1 Mar 2021 11:58:32 -0500 Subject: [PATCH 9/9] Moving settings default and struct. --- crates/api/src/lib.rs | 8 +++- crates/api/src/site.rs | 2 +- crates/api/src/user.rs | 2 +- crates/apub/src/activities/send/comment.rs | 2 +- crates/apub/src/activities/send/mod.rs | 2 +- crates/apub/src/activity_queue.rs | 2 +- crates/apub/src/fetcher/search.rs | 2 +- crates/apub/src/http/mod.rs | 2 +- crates/apub/src/inbox/mod.rs | 2 +- crates/apub/src/lib.rs | 2 +- crates/apub/src/objects/mod.rs | 2 +- crates/apub/src/objects/user.rs | 2 +- crates/apub/src/routes.rs | 2 +- crates/db_queries/src/source/user.rs | 2 +- crates/routes/src/feeds.rs | 7 +++- crates/routes/src/images.rs | 2 +- crates/routes/src/nodeinfo.rs | 2 +- crates/routes/src/webfinger.rs | 2 +- crates/structs/src/lib.rs | 2 +- crates/utils/src/claims.rs | 2 +- crates/utils/src/email.rs | 2 +- crates/utils/src/lib.rs | 2 +- crates/utils/src/rate_limit/mod.rs | 2 +- crates/utils/src/request.rs | 2 +- crates/utils/src/settings/defaults.rs | 23 +++++++++- crates/utils/src/settings/mod.rs | 49 ++-------------------- crates/utils/src/settings/structs.rs | 19 +++++++++ crates/utils/src/utils.rs | 2 +- src/code_migrations.rs | 2 +- src/main.rs | 2 +- 30 files changed, 82 insertions(+), 74 deletions(-) diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 4c6e3e3007..b8db40e20e 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 3269a67535..d9b798bb99 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -41,7 +41,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 55fe3e1005..8f57fd9d23 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 f007cda42d..be96b6c279 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 166855e204..2da0b48c2d 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 9586fd38f5..152fcaf9b7 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 a831ac4040..b7eb70cd66 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 f0ffbcb1e1..f2c8a20830 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 9c787e7d96..109a57dec3 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 0d6f489232..00be6c7dac 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 5e491fe65c..c0d48e45b8 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -17,7 +17,7 @@ use lemmy_db_schema::source::community::Community; use lemmy_structs::blocking; use lemmy_utils::{ location_info, - settings::Settings, + settings::structs::Settings, utils::{convert_datetime, markdown_to_html}, LemmyError, }; diff --git a/crates/apub/src/objects/user.rs b/crates/apub/src/objects/user.rs index d2bc3a69b0..6aa01a5d5a 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 71f71ef98a..07dcc7f88e 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 20b187b46f..56023f09f2 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 7885247f50..6dfa487374 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 8b8c9e9849..fe23fe029a 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 f0b9d9dde8..4e3f586460 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 f2b740c27f..26f6b16676 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 91ee572d30..800fe6c8bb 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 6d6f6e1c70..3d9232e6bf 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 09c0f55295..ead5349247 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 727c1fabac..58d2abb9d7 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -15,7 +15,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 bd7fc1e885..d3e74ed5bc 100644 --- a/crates/utils/src/rate_limit/mod.rs +++ b/crates/utils/src/rate_limit/mod.rs @@ -1,5 +1,5 @@ use crate::{ - settings::{structs::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 125e73a3fd..428d789746 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 index de6da4679c..56d24c7fb2 100644 --- a/crates/utils/src/settings/defaults.rs +++ b/crates/utils/src/settings/defaults.rs @@ -1,4 +1,25 @@ -use crate::settings::{CaptchaConfig, DatabaseConfig, FederationConfig, RateLimitConfig}; +use crate::settings::{CaptchaConfig, DatabaseConfig, FederationConfig, RateLimitConfig, Settings}; +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 DatabaseConfig { fn default() -> Self { diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index 3ab5f7d828..abd6f733cd 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -6,6 +6,7 @@ use crate::{ EmailConfig, FederationConfig, RateLimitConfig, + Settings, SetupConfig, }, LemmyError, @@ -13,57 +14,13 @@ use crate::{ use anyhow::{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, net::IpAddr, sync::RwLock}; pub(crate) mod defaults; -pub(crate) mod structs; +pub mod structs; static CONFIG_FILE: &str = "config/config.hjson"; -#[derive(Debug, Deserialize, Clone, Merge)] -pub struct Settings { - database: Option, - rate_limit: Option, - federation: Option, - hostname: Option, - bind: Option, - port: Option, - tls_enabled: Option, - jwt_secret: Option, - pictrs_url: Option, - iframely_url: Option, - captcha: Option, - email: Option, - setup: 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()), - } - } -} - 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 index c0e063e074..21d89b98a6 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -1,4 +1,23 @@ +use merge::Merge; use serde::Deserialize; +use std::net::IpAddr; + +#[derive(Debug, Deserialize, Clone, Merge)] +pub struct Settings { + pub(crate) database: Option, + pub(crate) rate_limit: Option, + pub(crate) federation: Option, + pub(crate) hostname: Option, + pub(crate) bind: Option, + pub(crate) port: Option, + pub(crate) tls_enabled: Option, + pub(crate) jwt_secret: Option, + pub(crate) pictrs_url: Option, + pub(crate) iframely_url: Option, + pub(crate) captcha: Option, + pub(crate) email: Option, + pub(crate) setup: Option, +} #[derive(Debug, Deserialize, Clone)] pub struct CaptchaConfig { diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index a3b0ebcda8..98eada07ed 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 ea11e32d3b..f3bc3df53f 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 0c3c10fb8c..0354a388c1 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};