diff --git a/Cargo.lock b/Cargo.lock index d0ff41e1..62c2361b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,7 +119,7 @@ dependencies = [ "actix-utils", "actix-web", "futures-util", - "serde 1.0.147", + "serde", "serde_json", "time 0.3.22", ] @@ -143,7 +143,7 @@ dependencies = [ "bytestring", "http", "regex", - "serde 1.0.147", + "serde", "tracing", ] @@ -246,7 +246,7 @@ dependencies = [ "once_cell", "pin-project-lite", "regex", - "serde 1.0.147", + "serde", "serde_json", "serde_urlencoded", "smallvec", @@ -427,12 +427,6 @@ version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" -[[package]] -name = "arrayvec" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" - [[package]] name = "arrayvec" version = "0.7.4" @@ -456,7 +450,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" dependencies = [ - "num-traits 0.2.15", + "num-traits", ] [[package]] @@ -504,7 +498,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "rand", - "serde 1.0.147", + "serde", "serde_json", "serde_urlencoded", "tokio", @@ -561,7 +555,7 @@ version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" dependencies = [ - "serde 1.0.147", + "serde", ] [[package]] @@ -577,7 +571,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c2f0dc9a68c6317d884f97cc36cf5a3d20ba14ce404227df55e1af708ab04bc" dependencies = [ "arrayref", - "arrayvec 0.7.4", + "arrayvec", "constant_time_eq 0.2.6", ] @@ -647,7 +641,7 @@ dependencies = [ "derive_builder", "mime", "mime_guess", - "serde 1.0.147", + "serde", "serde_json", "sha2", "walkdir", @@ -694,15 +688,18 @@ dependencies = [ [[package]] name = "config" -version = "0.11.0" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b1b9d958c2b1368a663f05538fc1b5975adce1e19f435acceae987aceeeb369" +checksum = "d379af7f68bfc21714c6c7dea883544201741d2ce8274bb12fa54f89507f52a7" dependencies = [ + "async-trait", + "json5", "lazy_static", - "nom 5.1.3", + "nom", + "pathdiff", + "ron", "rust-ini", - "serde 1.0.147", - "serde-hjson", + "serde", "serde_json", "toml 0.5.11", "yaml-rust", @@ -917,7 +914,7 @@ version = "0.1.0" dependencies = [ "async-trait", "libmcaptcha", - "serde 1.0.147", + "serde", "serde_json", "thiserror", "url", @@ -1041,6 +1038,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" +[[package]] +name = "dlv-list" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" + [[package]] name = "dotenv" version = "0.15.0" @@ -1053,7 +1056,7 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" dependencies = [ - "serde 1.0.147", + "serde", ] [[package]] @@ -1368,6 +1371,9 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash 0.7.6", +] [[package]] name = "hashbrown" @@ -1629,6 +1635,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json5" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1" +dependencies = [ + "pest", + "pest_derive", + "serde", +] + [[package]] name = "language-tags" version = "0.3.2" @@ -1662,7 +1679,7 @@ dependencies = [ "idna 0.3.0", "mime", "native-tls", - "nom 7.1.3", + "nom", "once_cell", "quoted_printable", "socket2", @@ -1670,19 +1687,6 @@ dependencies = [ "tokio-native-tls", ] -[[package]] -name = "lexical-core" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe" -dependencies = [ - "arrayvec 0.5.2", - "bitflags", - "cfg-if", - "ryu", - "static_assertions", -] - [[package]] name = "libc" version = "0.2.147" @@ -1710,7 +1714,7 @@ dependencies = [ "pretty_env_logger", "rand", "redis", - "serde 1.0.147", + "serde", "serde_json", "tokio", ] @@ -1836,7 +1840,7 @@ dependencies = [ "rand", "rust-embed", "sailfish", - "serde 1.0.147", + "serde", "serde_json", "sqlx", "tokio", @@ -1928,17 +1932,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" -[[package]] -name = "nom" -version = "5.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08959a387a676302eebf4ddbcbc611da04285579f76f88ee0506c63b1a61dd4b" -dependencies = [ - "lexical-core", - "memchr", - "version_check", -] - [[package]] name = "nom" version = "7.1.3" @@ -1960,7 +1953,7 @@ dependencies = [ "num-integer", "num-iter", "num-rational", - "num-traits 0.2.15", + "num-traits", ] [[package]] @@ -1971,7 +1964,7 @@ checksum = "5f6f7833f2cbf2360a6cfd58cd41a53aa7a90bd4c202f5b1c7dd2ed73c57b2c3" dependencies = [ "autocfg", "num-integer", - "num-traits 0.2.15", + "num-traits", ] [[package]] @@ -1982,8 +1975,8 @@ checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" dependencies = [ "autocfg", "num-integer", - "num-traits 0.2.15", - "serde 1.0.147", + "num-traits", + "serde", ] [[package]] @@ -1997,7 +1990,7 @@ dependencies = [ "libm", "num-integer", "num-iter", - "num-traits 0.2.15", + "num-traits", "rand", "smallvec", "zeroize", @@ -2009,8 +2002,8 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02e0d21255c828d6f128a1e41534206671e8c3ea0c62f32291e808dc82cff17d" dependencies = [ - "num-traits 0.2.15", - "serde 1.0.147", + "num-traits", + "serde", ] [[package]] @@ -2020,7 +2013,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" dependencies = [ "autocfg", - "num-traits 0.2.15", + "num-traits", ] [[package]] @@ -2031,7 +2024,7 @@ checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" dependencies = [ "autocfg", "num-integer", - "num-traits 0.2.15", + "num-traits", ] [[package]] @@ -2042,17 +2035,8 @@ checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" dependencies = [ "autocfg", "num-integer", - "num-traits 0.2.15", - "serde 1.0.147", -] - -[[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.15", + "num-traits", + "serde", ] [[package]] @@ -2150,6 +2134,16 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "ordered-multimap" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" +dependencies = [ + "dlv-list", + "hashbrown 0.12.3", +] + [[package]] name = "parking_lot" version = "0.11.2" @@ -2204,6 +2198,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "pem-rfc7468" version = "0.3.1" @@ -2219,6 +2219,51 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" +[[package]] +name = "pest" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c022f1e7b65d6a24c0dbbd5fb344c66881bc01f3e5ae74a1c8100f2f985d98a4" +dependencies = [ + "memchr", + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35513f630d46400a977c4cb58f78e1bfbe01434316e60c37d27b9ad6139c66d8" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc9fc1b9e7057baba189b5c626e2d6f40681ae5b6eb064dc7c7834101ec8123a" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn 2.0.38", +] + +[[package]] +name = "pest_meta" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df74e9e7ec4053ceb980e7c0c8bd3594e977fde1af91daba9c928e8e8c6708d" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + [[package]] name = "phf" version = "0.10.1" @@ -2317,7 +2362,7 @@ dependencies = [ "bincode", "derive_builder", "num", - "serde 1.0.147", + "serde", "sha2", ] @@ -2329,7 +2374,7 @@ dependencies = [ "bincode", "derive_builder", "num", - "serde 1.0.147", + "serde", "sha2", ] @@ -2542,6 +2587,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "ron" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" +dependencies = [ + "base64 0.13.1", + "bitflags", + "serde", +] + [[package]] name = "rsa" version = "0.6.1" @@ -2553,7 +2609,7 @@ dependencies = [ "num-bigint-dig", "num-integer", "num-iter", - "num-traits 0.2.15", + "num-traits", "pkcs1", "pkcs8", "rand_core", @@ -2610,9 +2666,13 @@ dependencies = [ [[package]] name = "rust-ini" -version = "0.13.0" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2" +checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" +dependencies = [ + "cfg-if", + "ordered-multimap", +] [[package]] name = "rustc-demangle" @@ -2694,7 +2754,7 @@ dependencies = [ "memchr", "proc-macro2", "quote", - "serde 1.0.147", + "serde", "syn 2.0.38", "toml 0.7.8", ] @@ -2796,12 +2856,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.147" @@ -2811,18 +2865,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", - "num-traits 0.1.43", - "regex", - "serde 0.8.23", -] - [[package]] name = "serde_derive" version = "1.0.147" @@ -2842,7 +2884,7 @@ checksum = "46266871c240a00b8f503b877622fe33430b3c7d963bdc0f2adc511e54a1eae3" dependencies = [ "itoa", "ryu", - "serde 1.0.147", + "serde", ] [[package]] @@ -2851,7 +2893,7 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" dependencies = [ - "serde 1.0.147", + "serde", ] [[package]] @@ -2863,7 +2905,7 @@ dependencies = [ "form_urlencoded", "itoa", "ryu", - "serde 1.0.147", + "serde", ] [[package]] @@ -2977,7 +3019,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4b7922be017ee70900be125523f38bdd644f4f06a1b16e8fa5a8ee8c34bffd4" dependencies = [ "itertools", - "nom 7.1.3", + "nom", "unicode_categories", ] @@ -3031,7 +3073,7 @@ dependencies = [ "rand", "rsa", "rustls", - "serde 1.0.147", + "serde", "serde_json", "sha-1", "sha2", @@ -3061,7 +3103,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "serde 1.0.147", + "serde", "serde_json", "sha2", "sqlx-core", @@ -3091,12 +3133,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" @@ -3119,7 +3155,7 @@ checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" dependencies = [ "proc-macro2", "quote", - "serde 1.0.147", + "serde", "serde_derive", "syn 1.0.94", ] @@ -3133,7 +3169,7 @@ dependencies = [ "base-x", "proc-macro2", "quote", - "serde 1.0.147", + "serde", "serde_derive", "serde_json", "sha1 0.6.1", @@ -3157,7 +3193,7 @@ dependencies = [ "parking_lot 0.12.1", "phf_shared", "precomputed-hash", - "serde 1.0.147", + "serde", ] [[package]] @@ -3292,7 +3328,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea9e1b3cf1243ae005d9e74085d4d542f3125458f3a81af210d901dcd7411efd" dependencies = [ "itoa", - "serde 1.0.147", + "serde", "time-core", "time-macros 0.2.9", ] @@ -3421,7 +3457,7 @@ version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" dependencies = [ - "serde 1.0.147", + "serde", ] [[package]] @@ -3430,7 +3466,7 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" dependencies = [ - "serde 1.0.147", + "serde", "serde_spanned", "toml_datetime", "toml_edit", @@ -3442,7 +3478,7 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" dependencies = [ - "serde 1.0.147", + "serde", ] [[package]] @@ -3452,7 +3488,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" dependencies = [ "indexmap 2.0.2", - "serde 1.0.147", + "serde", "serde_spanned", "toml_datetime", "winnow", @@ -3485,6 +3521,12 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +[[package]] +name = "ucd-trie" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" + [[package]] name = "unicase" version = "2.6.0" @@ -3558,7 +3600,7 @@ dependencies = [ "form_urlencoded", "idna 0.4.0", "percent-encoding", - "serde 1.0.147", + "serde", ] [[package]] @@ -3580,7 +3622,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d023da39d1fde5a8a3fe1f3e01ca9632ada0a63e9797de55a879d6e2236277be" dependencies = [ "getrandom", - "serde 1.0.147", + "serde", ] [[package]] @@ -3592,7 +3634,7 @@ dependencies = [ "idna 0.2.3", "lazy_static", "regex", - "serde 1.0.147", + "serde", "serde_derive", "serde_json", "url", diff --git a/Cargo.toml b/Cargo.toml index 2bf717fb..8375b515 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ tokio = { version = "1.14", features = ["sync"]} sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline", "mysql"] } argon2-creds = { branch = "master", git = "https://github.com/realaravinth/argon2-creds"} #argon2-creds = { version="*", path = "../../argon2-creds/" } -config = "0.11" +config = "0.13" validator = { version = "0.15", features = ["derive"]} derive_builder = "0.11" diff --git a/Makefile b/Makefile index 79902191..ec966c18 100644 --- a/Makefile +++ b/Makefile @@ -34,8 +34,7 @@ endef define run_dev_migrations ## run database migrations cd db/db-sqlx-maria/ && \ - DATABASE_URL=${MARIA_DATABASE_URL} sqlx migrate run - cd db/db-sqlx-postgres/ && \ + DATABASE_URL=${MARIA_DATABASE_URL} sqlx migrate run cd db/db-sqlx-postgres/ && \ DATABASE_URL=${POSTGRES_DATABASE_URL} sqlx migrate run endef @@ -67,11 +66,9 @@ define test_db_sqlx_maria endef define test_core - cargo test --no-fail-fast + cargo test --no-fail-fast --jobs=4 endef - - default: frontend ## Build app in debug mode $(call cache_bust) cargo build diff --git a/config/default.toml b/config/default.toml index 55c77906..43583e64 100644 --- a/config/default.toml +++ b/config/default.toml @@ -42,15 +42,10 @@ duration = 30 # cooldown period in seconds # This section deals with the database location and how to access it # Please note that at the moment, we have support for only postgresqa. # Example, if you are Batman, your config would be: -# hostname = "batcave.org" -# port = "5432" -# username = "batman" -# password = "somereallycomplicatedBatmanpassword" -hostname = "localhost" -port = "5432" -username = "postgres" -password = "password" -name = "postgres" +# url = "postgres://batman:password@batcave.org:5432/batcave" +# database_type = "postgres" +# pool = 4 +url = "http://example.org" # hack for tests to run successfully pool = 4 database_type="postgres" # "postgres", "maria" diff --git a/src/settings.rs b/src/settings.rs index e64b8a67..5eaa66d2 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -6,7 +6,7 @@ use std::path::Path; use std::{env, fs}; -use config::{Config, ConfigError, Environment, File}; +use config::{Config, ConfigError, Environment, File, ConfigBuilder}; use derive_more::Display; use serde::{Deserialize, Serialize}; @@ -57,32 +57,6 @@ impl Server { } } -//#[derive(Debug, Clone, Deserialize)] -//struct DatabaseBuilder { -// pub port: u32, -// pub hostname: String, -// pub username: String, -// pub password: String, -// pub name: String, -//} - -//impl DatabaseBuilder { -// #[cfg(not(tarpaulin_include))] -// fn extract_database_url(url: &Url) -> Self { -// debug!("Database name: {}", url.path()); -// let mut path = url.path().split('/'); -// path.next(); -// let name = path.next().expect("no database name").to_string(); -// DatabaseBuilder { -// port: url.port().expect("Enter database port").into(), -// hostname: url.host().expect("Enter database host").to_string(), -// username: url.username().into(), -// password: url.password().expect("Enter database password").into(), -// name, -// } -// } -//} - #[derive(Deserialize, Serialize, Display, PartialEq, Clone, Debug)] #[serde(rename_all = "lowercase")] pub enum DBType { @@ -132,12 +106,12 @@ pub struct Settings { #[cfg(not(tarpaulin_include))] impl Settings { pub fn new() -> Result { - let mut s = Config::new(); + let mut s = Config::builder(); const CURRENT_DIR: &str = "./config/default.toml"; const ETC: &str = "/etc/mcaptcha/config.toml"; - s.set("capatcha.enable_stats", true.to_string()) + s = s.set_default("capatcha.enable_stats", true.to_string()) .expect("unable to set capatcha.enable_stats default config"); if let Ok(path) = env::var("MCAPTCHA_CONFIG") { @@ -146,7 +120,7 @@ impl Settings { "Loading config file from {}", absolute_path.to_str().unwrap() ); - s.merge(File::with_name(absolute_path.to_str().unwrap()))?; + s = s.add_source(File::with_name(absolute_path.to_str().unwrap())); } else if Path::new(CURRENT_DIR).exists() { let absolute_path = fs::canonicalize(CURRENT_DIR).unwrap(); log::info!( @@ -154,56 +128,55 @@ impl Settings { absolute_path.to_str().unwrap() ); // merging default config from file - s.merge(File::with_name(absolute_path.to_str().unwrap()))?; + s = s.add_source(File::with_name(absolute_path.to_str().unwrap())); } else if Path::new(ETC).exists() { log::info!("{}", format!("Loading config file from {}", ETC)); - s.merge(File::with_name(ETC))?; + s = s.add_source(File::with_name(ETC)); } else { log::warn!("Configuration file not found"); } - s.merge(Environment::with_prefix("MCAPTCHA").separator("_"))?; + s = s.add_source(Environment::with_prefix("MCAPTCHA").separator("_")); - check_url(&s); if let Ok(val) = env::var("PORT") { - s.set("server.port", val).unwrap(); + s = s.set_override("server.port", val).unwrap(); log::info!("Overriding [server].port with environment variable"); } match env::var("DATABASE_URL") { Ok(val) => { let url = Url::parse(&val).expect("couldn't parse Database URL"); - s.set("database.url", url.to_string()).unwrap(); + s = s.set_override("database.url", url.to_string()).unwrap(); let database_type = DBType::from_url(&url).unwrap(); - s.set("database.database_type", database_type.to_string()) + s = s.set_override("database.database_type", database_type.to_string()) .unwrap(); log::info!("Overriding [database].url and [database].database_type with environment variable"); } - Err(_e) => { - set_database_url(&mut s); + Err(e) => { + log::warn!("Couldn't interpret DATABASE_URL: {:?}", e); } } - // setting default values - #[cfg(test)] - s.set("database.pool", 2.to_string()) - .expect("Couldn't set database pool count"); + let mut settings = s.build()?.try_deserialize::()?; + settings.check_url(); - match s.try_into() { - Ok(val) => Ok(val), - Err(e) => Err(ConfigError::Message(format!("\n\nError: {}. If it says missing fields, then please refer to https://github.com/mCaptcha/mcaptcha#configuration to learn more about how mcaptcha reads configuration\n\n", e))), - } + #[cfg(test)] + settings.set_test_defaults(); + + Ok(settings) + + } + + // bypass for issue #15701 for more information + fn set_test_defaults(&mut self) { + self.database.pool = 2; } -} #[cfg(not(tarpaulin_include))] -fn check_url(s: &Config) { - let url = s - .get::("source_code") - .expect("Couldn't access source_code"); - - Url::parse(&url).expect("Please enter a URL for source_code in settings"); +fn check_url(&self) { + Url::parse(&self.source_code).expect("Please enter a URL for source_code in settings"); +} } #[cfg(not(tarpaulin_include))] diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 89986675..5234be7a 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -40,6 +40,7 @@ pub mod pg { settings.captcha.runners = Some(1); settings.database.url = url.clone(); settings.database.database_type = DBType::Postgres; + settings.database.pool = 2; Data::new(&settings).await } @@ -59,6 +60,7 @@ pub mod maria { settings.captcha.runners = Some(1); settings.database.url = url.clone(); settings.database.database_type = DBType::Maria; + settings.database.pool = 2; Data::new(&settings).await }