From f19c7a2e720ec22de7c64897714e93ab0e71df94 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 16 Feb 2021 19:52:56 -0500 Subject: [PATCH] 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 f3efdc308..59528c80b 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 2edcca8d7..ea7fdca3a 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 5e9e1c162..779da57f4 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 93d40bdaf..0d11065cf 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 c0c4ac468..b4ccac754 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 765d5dff4..751992966 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 388d57e6a..5d6744e33 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 bf0ac24cb..f6910cce0 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 8a911de27..c23f4c509 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 3717a894f..ac946831a 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 837e1489a..6e0ae8e1f 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 4d7656e1b..e58f59e31 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 46ef7ce4d..b9768d9a0 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 8e61500c0..31ee4133d 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 6d8265aa3..9fb56d4b9 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 5a18ffd54..f1c6e360d 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 411d43427..805f1b130 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 8301dcee4..6400264fd 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(); + 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_defaults_location()))?; + // Merge with default + custom_config.merge(Settings::default()); - s.merge(File::with_name(&Self::get_config_location()).required(false))?; - - // 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 629743568..dc701b54f 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 bea65d8f9..bceb5f6cd 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 4ebedb4f8..6ca5253d4 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 f205ceb51..6658cc726 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();