From 26fe7cdf3231a9145cd59bdccd0b0b2df4736597 Mon Sep 17 00:00:00 2001 From: Elad Kaplan <kaplan.elad@gmail.com> Date: Sun, 5 Jan 2025 07:26:56 -0800 Subject: [PATCH] create load config hook (#1143) * create load config hook * demo example * remove tests, already doing it in loco new --- Cargo.toml | 5 +- examples/demo/Cargo.lock | 415 +++++++----------- examples/demo/examples/start.rs | 4 +- examples/demo/examples/task.rs | 3 +- examples/demo/examples/workers.rs | 4 +- examples/demo/src/app.rs | 9 +- examples/demo/tests/cmd/cli.trycmd | 21 - examples/llm-candle-inference/src/app.rs | 9 +- loco-new/base_template/src/app.rs.t | 7 +- ...tes__auth__src_app_rs_auth_false_None.snap | 5 +- ...s__auth__src_app_rs_auth_false_Sqlite.snap | 5 +- ...ates__auth__src_app_rs_auth_true_None.snap | 5 +- ...es__auth__src_app_rs_auth_true_Sqlite.snap | 5 +- ...mplates__background__src_app_rs_Async.snap | 5 +- ...ates__background__src_app_rs_Blocking.snap | 5 +- ...emplates__background__src_app_rs_None.snap | 5 +- ...mplates__background__src_app_rs_Queue.snap | 5 +- ...r#mod__templates__db__src_app_rs_None.snap | 5 +- ...d__templates__db__src_app_rs_Postgres.snap | 5 +- ...mod__templates__db__src_app_rs_Sqlite.snap | 5 +- ...alizers__src_app_rs_with_initializers.snap | 5 +- ...zers__src_app_rs_without_initializers.snap | 5 +- src/app.rs | 19 +- src/boot.rs | 14 +- src/cli.rs | 67 +-- src/controller/mod.rs | 5 +- src/testing/request.rs | 3 +- src/tests_cfg/db.rs | 9 +- 28 files changed, 309 insertions(+), 350 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b232b2e93..9982f8c5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -132,7 +132,10 @@ cfg-if = "1" uuid = { version = "1.10.0", features = ["v4", "fast-rng"] } # File Upload -opendal = { version = "0.50.2", default-features = false,features = ["services-memory","services-fs"] } +opendal = { version = "0.50.2", default-features = false, features = [ + "services-memory", + "services-fs", +] } # cache moka = { version = "0.12.7", features = ["sync"], optional = true } diff --git a/examples/demo/Cargo.lock b/examples/demo/Cargo.lock index ba6a7182b..76dfe9df6 100644 --- a/examples/demo/Cargo.lock +++ b/examples/demo/Cargo.lock @@ -370,7 +370,7 @@ checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -387,7 +387,7 @@ checksum = "1b1244b10dcd56c92219da4e14caa97e312079e185f04ba3eea25061561dc0a0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -417,41 +417,13 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" -[[package]] -name = "axum" -version = "0.7.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" -dependencies = [ - "async-trait", - "axum-core 0.4.5", - "bytes", - "futures-util", - "http 1.2.0", - "http-body", - "http-body-util", - "itoa", - "matchit 0.7.3", - "memchr", - "mime", - "percent-encoding", - "pin-project-lite", - "rustversion", - "serde", - "sync_wrapper", - "tower 0.5.2", - "tower-layer", - "tower-service", - "tracing", -] - [[package]] name = "axum" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d6fd624c75e18b3b4c6b9caf42b1afe24437daaee904069137d8bab077be8b8" dependencies = [ - "axum-core 0.5.0", + "axum-core", "axum-macros", "bytes", "form_urlencoded", @@ -462,7 +434,7 @@ dependencies = [ "hyper", "hyper-util", "itoa", - "matchit 0.8.4", + "matchit", "memchr", "mime", "multer", @@ -481,27 +453,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "axum-core" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" -dependencies = [ - "async-trait", - "bytes", - "futures-util", - "http 1.2.0", - "http-body", - "http-body-util", - "mime", - "pin-project-lite", - "rustversion", - "sync_wrapper", - "tower-layer", - "tower-service", - "tracing", -] - [[package]] name = "axum-core" version = "0.5.0" @@ -522,38 +473,14 @@ dependencies = [ "tracing", ] -[[package]] -name = "axum-extra" -version = "0.9.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c794b30c904f0a1c2fb7740f7df7f7972dfaa14ef6f57cb6178dc63e5dca2f04" -dependencies = [ - "axum 0.7.9", - "axum-core 0.4.5", - "bytes", - "cookie", - "fastrand", - "futures-util", - "http 1.2.0", - "http-body", - "http-body-util", - "mime", - "multer", - "pin-project-lite", - "serde", - "tower 0.5.2", - "tower-layer", - "tower-service", -] - [[package]] name = "axum-extra" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "460fc6f625a1f7705c6cf62d0d070794e94668988b1c38111baeec177c715f7b" dependencies = [ - "axum 0.8.1", - "axum-core 0.5.0", + "axum", + "axum-core", "bytes", "cookie", "futures-util", @@ -576,7 +503,7 @@ checksum = "604fde5e028fea851ce1d8570bbdc034bec850d157f7569d10f347d06808c05c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -587,7 +514,7 @@ dependencies = [ "anyhow", "assert-json-diff", "auto-future", - "axum 0.8.1", + "axum", "bytes", "bytesize", "cookie", @@ -616,7 +543,7 @@ checksum = "9bcd5d9395b53d6a7054f599f4fe6f519d374a5b84fa758d1163af0d278b9213" dependencies = [ "aes-gcm", "async-trait", - "axum 0.8.1", + "axum", "base64 0.22.1", "bytes", "chrono", @@ -639,6 +566,17 @@ dependencies = [ "uuid", ] +[[package]] +name = "backon" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5289ec98f68f28dd809fd601059e6aa908bb8f6108620930828283d4ee23d7" +dependencies = [ + "fastrand", + "gloo-timers", + "tokio", +] + [[package]] name = "backtrace" version = "0.3.74" @@ -782,7 +720,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -989,7 +927,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -1253,7 +1191,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331" dependencies = [ "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -1286,7 +1224,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -1297,7 +1235,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -1319,8 +1257,8 @@ name = "demo_app" version = "0.1.0" dependencies = [ "async-trait", - "axum 0.8.1", - "axum-extra 0.10.0", + "axum", + "axum-extra", "axum-test", "axum_session", "chrono", @@ -1376,7 +1314,7 @@ checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -1432,7 +1370,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -1617,6 +1555,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "flagset" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3ea1ec5f8307826a5b71094dd91fc04d4ae75d5709b20ad351c7fb4815c86ec" + [[package]] name = "flate2" version = "1.0.35" @@ -1706,7 +1650,7 @@ dependencies = [ "log", "once_cell", "serde_json", - "snafu 0.7.5", + "snafu", "tera", "unic-langid", ] @@ -1737,6 +1681,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -1861,7 +1811,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -2065,14 +2015,19 @@ name = "hashbrown" version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] [[package]] name = "hashlink" -version = "0.9.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" +checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" dependencies = [ - "hashbrown 0.14.5", + "hashbrown 0.15.2", ] [[package]] @@ -2154,7 +2109,7 @@ dependencies = [ "markup5ever", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -2441,7 +2396,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -2525,7 +2480,7 @@ checksum = "0122b7114117e64a63ac49f752a5ca4624d534c7b1c7de796ac196381cd2d947" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -2539,13 +2494,13 @@ dependencies = [ [[package]] name = "insta" -version = "1.41.1" +version = "1.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8" +checksum = "6513e4067e16e69ed1db5ab56048ed65db32d10ba5fc1217f5393f8f17d8b5a5" dependencies = [ "console", - "lazy_static", "linked-hash-map", + "once_cell", "pest", "pest_derive", "regex", @@ -2613,15 +2568,6 @@ dependencies = [ "either", ] -[[package]] -name = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - [[package]] name = "itoa" version = "1.0.14" @@ -2797,8 +2743,8 @@ version = "0.13.2" dependencies = [ "argon2", "async-trait", - "axum 0.8.1", - "axum-extra 0.9.6", + "axum", + "axum-extra", "axum-test", "backtrace_printer", "bb8", @@ -2808,6 +2754,7 @@ dependencies = [ "chrono", "clap", "colored", + "cruet 0.13.3", "duct", "duct_sh", "english-to-cron", @@ -2822,7 +2769,7 @@ dependencies = [ "loco-gen", "mime", "moka", - "object_store", + "opendal", "rand", "regex", "reqwest", @@ -2904,12 +2851,6 @@ dependencies = [ "regex-automata 0.1.10", ] -[[package]] -name = "matchit" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" - [[package]] name = "matchit" version = "0.8.4" @@ -3098,7 +3039,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -3150,27 +3091,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "object_store" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cfccb68961a56facde1163f9319e0d15743352344e7808a11795fb99698dcaf" -dependencies = [ - "async-trait", - "bytes", - "chrono", - "futures", - "humantime", - "itertools 0.13.0", - "parking_lot", - "percent-encoding", - "snafu 0.8.5", - "tokio", - "tracing", - "url", - "walkdir", -] - [[package]] name = "once_cell" version = "1.20.2" @@ -3183,6 +3103,34 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" +[[package]] +name = "opendal" +version = "0.50.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb28bb6c64e116ceaf8dd4e87099d3cfea4a58e85e62b104fef74c91afba0f44" +dependencies = [ + "anyhow", + "async-trait", + "backon", + "base64 0.22.1", + "bytes", + "chrono", + "flagset", + "futures", + "getrandom", + "http 1.2.0", + "log", + "md-5", + "once_cell", + "percent-encoding", + "quick-xml", + "reqwest", + "serde", + "serde_json", + "tokio", + "uuid", +] + [[package]] name = "ordered-float" version = "3.9.2" @@ -3220,11 +3168,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39b0deead1528fd0e5947a8546a9642a9777c25f6e1e26f34c97b204bbb465bd" dependencies = [ "heck 0.4.1", - "itertools 0.12.1", + "itertools", "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -3282,12 +3230,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "paste" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" - [[package]] name = "pem" version = "3.0.4" @@ -3344,7 +3286,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -3408,7 +3350,7 @@ dependencies = [ "phf_shared 0.11.2", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -3589,7 +3531,7 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -3615,7 +3557,7 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", "version_check", "yansi", ] @@ -3664,6 +3606,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "quick-xml" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7649a7b4df05aed9ea7ec6f628c67c9953a43869b8bc50929569b2999d443fe" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "quinn" version = "0.11.6" @@ -3931,11 +3883,13 @@ dependencies = [ "system-configuration", "tokio", "tokio-rustls", + "tokio-util", "tower 0.5.2", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", "webpki-roots", "windows-registry", @@ -4060,7 +4014,7 @@ dependencies = [ "regex", "relative-path", "rustc_version", - "syn 2.0.94", + "syn 2.0.95", "unicode-ident", ] @@ -4283,7 +4237,7 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -4341,7 +4295,7 @@ dependencies = [ "proc-macro2", "quote", "sea-bae", - "syn 2.0.94", + "syn 2.0.95", "unicode-ident", ] @@ -4405,7 +4359,7 @@ dependencies = [ "heck 0.4.1", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", "thiserror 1.0.69", ] @@ -4429,7 +4383,7 @@ dependencies = [ "heck 0.4.1", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -4495,7 +4449,7 @@ checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -4595,7 +4549,7 @@ checksum = "5d69265a08751de7844521fd15003ae0a888e035773ba05695c5c759a6f89eef" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -4763,16 +4717,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4de37ad025c587a29e8f3f5605c00f70b98715ef90b9061a815b9e59e9042d6" dependencies = [ "doc-comment", - "snafu-derive 0.7.5", -] - -[[package]] -name = "snafu" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "223891c85e2a29c3fe8fb900c1fae5e69c2e42415e3177752e8718475efa5019" -dependencies = [ - "snafu-derive 0.8.5", + "snafu-derive", ] [[package]] @@ -4787,18 +4732,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "snafu-derive" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03c3c6b7927ffe7ecaa769ee0e3994da3b8cafc8f444578982c83ecb161af917" -dependencies = [ - "heck 0.5.0", - "proc-macro2", - "quote", - "syn 2.0.94", -] - [[package]] name = "snapbox" version = "0.4.17" @@ -4859,21 +4792,11 @@ dependencies = [ "der", ] -[[package]] -name = "sqlformat" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bba3a93db0cc4f7bdece8bb09e77e2e785c20bfebf79eb8340ed80708048790" -dependencies = [ - "nom", - "unicode_categories", -] - [[package]] name = "sqlx" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93334716a037193fac19df402f8571269c84a00852f6a7066b5d2616dcd64d3e" +checksum = "4410e73b3c0d8442c5f99b425d7a435b5ee0ae4167b3196771dd3f7a01be745f" dependencies = [ "sqlx-core", "sqlx-macros", @@ -4884,32 +4807,27 @@ dependencies = [ [[package]] name = "sqlx-core" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4d8060b456358185f7d50c55d9b5066ad956956fddec42ee2e8567134a8936e" +checksum = "6a007b6936676aa9ab40207cde35daab0a04b823be8ae004368c0793b96a61e0" dependencies = [ - "atoi", "bigdecimal", - "byteorder", "bytes", "chrono", "crc", "crossbeam-queue", "either", "event-listener 5.3.1", - "futures-channel", "futures-core", "futures-intrusive", "futures-io", "futures-util", - "hashbrown 0.14.5", + "hashbrown 0.15.2", "hashlink", - "hex", "indexmap", "log", "memchr", "once_cell", - "paste", "percent-encoding", "rust_decimal", "rustls", @@ -4918,8 +4836,7 @@ dependencies = [ "serde_json", "sha2", "smallvec", - "sqlformat", - "thiserror 1.0.69", + "thiserror 2.0.9", "time", "tokio", "tokio-stream", @@ -4931,22 +4848,22 @@ dependencies = [ [[package]] name = "sqlx-macros" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cac0692bcc9de3b073e8d747391827297e075c7710ff6276d9f7a1f3d58c6657" +checksum = "3112e2ad78643fef903618d78cf0aec1cb3134b019730edb039b69eaf531f310" dependencies = [ "proc-macro2", "quote", "sqlx-core", "sqlx-macros-core", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] name = "sqlx-macros-core" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1804e8a7c7865599c9c79be146dc8a9fd8cc86935fa641d3ea58e5f0688abaa5" +checksum = "4e9f90acc5ab146a99bf5061a7eb4976b573f560bc898ef3bf8435448dd5e7ad" dependencies = [ "dotenvy", "either", @@ -4962,7 +4879,7 @@ dependencies = [ "sqlx-mysql", "sqlx-postgres", "sqlx-sqlite", - "syn 2.0.94", + "syn 2.0.95", "tempfile", "tokio", "url", @@ -4970,9 +4887,9 @@ dependencies = [ [[package]] name = "sqlx-mysql" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64bb4714269afa44aef2755150a0fc19d756fb580a67db8885608cf02f47d06a" +checksum = "4560278f0e00ce64938540546f59f590d60beee33fffbd3b9cd47851e5fff233" dependencies = [ "atoi", "base64 0.22.1", @@ -5008,7 +4925,7 @@ dependencies = [ "smallvec", "sqlx-core", "stringprep", - "thiserror 1.0.69", + "thiserror 2.0.9", "time", "tracing", "uuid", @@ -5017,9 +4934,9 @@ dependencies = [ [[package]] name = "sqlx-postgres" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fa91a732d854c5d7726349bb4bb879bb9478993ceb764247660aee25f67c2f8" +checksum = "c5b98a57f363ed6764d5b3a12bfedf62f07aa16e1856a7ddc2a0bb190a959613" dependencies = [ "atoi", "base64 0.22.1", @@ -5032,7 +4949,6 @@ dependencies = [ "etcetera", "futures-channel", "futures-core", - "futures-io", "futures-util", "hex", "hkdf", @@ -5052,7 +4968,7 @@ dependencies = [ "smallvec", "sqlx-core", "stringprep", - "thiserror 1.0.69", + "thiserror 2.0.9", "time", "tracing", "uuid", @@ -5061,9 +4977,9 @@ dependencies = [ [[package]] name = "sqlx-sqlite" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5b2cf34a45953bfd3daaf3db0f7a7878ab9b7a6b91b422d24a7a9e4c857b680" +checksum = "f85ca71d3a5b24e64e1d08dd8fe36c6c95c339a896cc33068148906784620540" dependencies = [ "atoi", "chrono", @@ -5178,9 +5094,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.94" +version = "2.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "987bc0be1cdea8b10216bd06e2ca407d40b9543468fafd3ddfb02f36e77f71f3" +checksum = "46f71c0377baf4ef1cc3e3402ded576dccc315800fbc62dfc7fe04b009773b4a" dependencies = [ "proc-macro2", "quote", @@ -5204,7 +5120,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -5324,7 +5240,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -5335,7 +5251,7 @@ checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -5451,7 +5367,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -5620,7 +5536,7 @@ checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -5789,7 +5705,7 @@ checksum = "1ed7f4237ba393424195053097c1516bd4590dc82b84f2f97c5c69e12704555b" dependencies = [ "proc-macro-hack", "quote", - "syn 2.0.94", + "syn 2.0.95", "unic-langid-impl", ] @@ -5867,12 +5783,6 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" -[[package]] -name = "unicode_categories" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" - [[package]] name = "universal-hash" version = "0.5.1" @@ -5957,7 +5867,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -5998,7 +5908,7 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -6086,7 +5996,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", "wasm-bindgen-shared", ] @@ -6121,7 +6031,7 @@ checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -6132,6 +6042,19 @@ version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "web-sys" version = "0.3.76" @@ -6252,7 +6175,7 @@ checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -6263,7 +6186,7 @@ checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -6446,9 +6369,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.21" +version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6f5bb5257f2407a5425c6e749bfd9692192a73e70a6060516ac04f889087d68" +checksum = "39281189af81c07ec09db316b302a3e67bf9bd7cbf6c820b50e35fee9c2fa980" dependencies = [ "memchr", ] @@ -6500,7 +6423,7 @@ checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", "synstructure", ] @@ -6522,7 +6445,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] @@ -6542,7 +6465,7 @@ checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", "synstructure", ] @@ -6571,7 +6494,7 @@ checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.94", + "syn 2.0.95", ] [[package]] diff --git a/examples/demo/examples/start.rs b/examples/demo/examples/start.rs index a14250e6d..0f398b08c 100644 --- a/examples/demo/examples/start.rs +++ b/examples/demo/examples/start.rs @@ -8,8 +8,10 @@ use migration::Migrator; #[tokio::main] async fn main() -> loco_rs::Result<()> { let environment: Environment = resolve_from_env().into(); + let config = environment.load()?; - let boot_result = create_app::<App, Migrator>(StartMode::ServerAndWorker, &environment).await?; + let boot_result = + create_app::<App, Migrator>(StartMode::ServerAndWorker, &environment, config).await?; let serve_params = ServeParams { port: boot_result.app_context.config.server.port, binding: boot_result.app_context.config.server.binding.to_string(), diff --git a/examples/demo/examples/task.rs b/examples/demo/examples/task.rs index eb2f271f5..3807cea5a 100644 --- a/examples/demo/examples/task.rs +++ b/examples/demo/examples/task.rs @@ -10,10 +10,11 @@ use loco_rs::{ #[tokio::main] async fn main() -> loco_rs::Result<()> { let environment: Environment = resolve_from_env().into(); + let config = environment.load()?; let args = env::args().collect::<Vec<_>>(); let cmd = args.get(1); - let app_context = create_context::<App>(&environment).await?; + let app_context = create_context::<App>(&environment, config).await?; run_task::<App>(&app_context, cmd, &task::Vars::default()).await?; Ok(()) diff --git a/examples/demo/examples/workers.rs b/examples/demo/examples/workers.rs index ce19ffbf6..b36f4c676 100644 --- a/examples/demo/examples/workers.rs +++ b/examples/demo/examples/workers.rs @@ -8,8 +8,10 @@ use migration::Migrator; #[tokio::main] async fn main() -> loco_rs::Result<()> { let environment: Environment = resolve_from_env().into(); + let config = environment.load()?; - let boot_result = create_app::<App, Migrator>(StartMode::WorkerOnly, &environment).await?; + let boot_result = + create_app::<App, Migrator>(StartMode::WorkerOnly, &environment, config).await?; let serve_params = ServeParams { port: boot_result.app_context.config.server.port, binding: boot_result.app_context.config.server.binding.to_string(), diff --git a/examples/demo/src/app.rs b/examples/demo/src/app.rs index 76c72d718..246abde57 100644 --- a/examples/demo/src/app.rs +++ b/examples/demo/src/app.rs @@ -5,6 +5,7 @@ use loco_rs::{ app::{AppContext, Hooks, Initializer}, boot::{create_app, BootResult, StartMode}, cache, + config::Config, controller::AppRoutes, db::{self, truncate_table}, environment::Environment, @@ -69,8 +70,12 @@ impl Hooks for App { .add_route(controllers::cache::routes()) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self, Migrator>(mode, environment).await + async fn boot( + mode: StartMode, + environment: &Environment, + config: Config, + ) -> Result<BootResult> { + create_app::<Self, Migrator>(mode, environment, config).await } async fn after_context(ctx: AppContext) -> Result<AppContext> { diff --git a/examples/demo/tests/cmd/cli.trycmd b/examples/demo/tests/cmd/cli.trycmd index 3d9b3a9c6..8c283a481 100644 --- a/examples/demo/tests/cmd/cli.trycmd +++ b/examples/demo/tests/cmd/cli.trycmd @@ -156,24 +156,3 @@ $ demo_app-cli doctor $ LOCO_ENV=test demo_app-cli db reset ``` - -```console -$ LOCO_ENV=teste2e demo_app-cli db migrate -[..][0m [32m INFO[0m [1mapp[0m: [2mloco_rs::config[0m[2m:[0m loading environment from [3mselected_path[0m[2m=[0m"config/teste2e.yaml" [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [33m WARN[0m [1mapp[0m: [2mloco_rs::boot[0m[2m:[0m migrate: [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [32m INFO[0m [1mapp[0m: [2msea_orm_migration::migrator[0m[2m:[0m Applying all pending migrations [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [32m INFO[0m [1mapp[0m: [2msea_orm_migration::migrator[0m[2m:[0m No pending migrations [2m[3menvironment[0m[2m=[0mteste2e[0m - -``` - -```console -$ LOCO_ENV=teste2e demo_app-cli db status -[..][0m [32m INFO[0m [1mapp[0m: [2mloco_rs::config[0m[2m:[0m loading environment from [3mselected_path[0m[2m=[0m"config/teste2e.yaml" [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [33m WARN[0m [1mapp[0m: [2mloco_rs::boot[0m[2m:[0m status: [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [32m INFO[0m [1mapp[0m: [2msea_orm_migration::migrator[0m[2m:[0m Checking migration status [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [32m INFO[0m [1mapp[0m: [2msea_orm_migration::migrator[0m[2m:[0m Migration 'm20220101_000001_users'... Applied [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [32m INFO[0m [1mapp[0m: [2msea_orm_migration::migrator[0m[2m:[0m Migration 'm20231103_114510_notes'... Applied [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [32m INFO[0m [1mapp[0m: [2msea_orm_migration::migrator[0m[2m:[0m Migration 'm20240416_071825_roles'... Applied [2m[3menvironment[0m[2m=[0mteste2e[0m -[..][0m [32m INFO[0m [1mapp[0m: [2msea_orm_migration::migrator[0m[2m:[0m Migration 'm20240416_082115_users_roles'... Applied [2m[3menvironment[0m[2m=[0mteste2e[0m - -``` diff --git a/examples/llm-candle-inference/src/app.rs b/examples/llm-candle-inference/src/app.rs index 1665ea40c..e1ca418fc 100644 --- a/examples/llm-candle-inference/src/app.rs +++ b/examples/llm-candle-inference/src/app.rs @@ -4,6 +4,7 @@ use kalosm::language::{Llama, LlamaSource}; use loco_rs::{ app::{AppContext, Hooks}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, prelude::*, @@ -20,8 +21,12 @@ impl Hooks for App { env!("CARGO_CRATE_NAME") } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot( + mode: StartMode, + environment: &Environment, + config: Config, + ) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn before_run(_ctx: &AppContext) -> Result<()> { diff --git a/loco-new/base_template/src/app.rs.t b/loco-new/base_template/src/app.rs.t index 5414e23c2..bd4efb458 100644 --- a/loco-new/base_template/src/app.rs.t +++ b/loco-new/base_template/src/app.rs.t @@ -10,6 +10,7 @@ use loco_rs::{ {%- endif %} Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, {%- if settings.auth %} db::{self, truncate_table}, @@ -54,11 +55,11 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { {%- if settings.db %} - create_app::<Self, Migrator>(mode, environment).await + create_app::<Self, Migrator>(mode, environment, config).await {% else %} - create_app::<Self>(mode, environment).await + create_app::<Self>(mode, environment, config).await {%- endif %} } diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_false_None.snap b/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_false_None.snap index 692d668ff..74432aa13 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_false_None.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_false_None.snap @@ -8,6 +8,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -36,8 +37,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_false_Sqlite.snap b/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_false_Sqlite.snap index 908d93f36..73c49b298 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_false_Sqlite.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_false_Sqlite.snap @@ -9,6 +9,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -39,8 +40,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self, Migrator>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self, Migrator>(mode, environment, config).await } diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_true_None.snap b/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_true_None.snap index 1059932aa..e4b17057e 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_true_None.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_true_None.snap @@ -8,6 +8,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, db::{self, truncate_table}, environment::Environment, @@ -38,8 +39,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_true_Sqlite.snap b/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_true_Sqlite.snap index 84e421a78..d2424b6e6 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_true_Sqlite.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__auth__src_app_rs_auth_true_Sqlite.snap @@ -9,6 +9,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, db::{self, truncate_table}, environment::Environment, @@ -41,8 +42,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self, Migrator>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self, Migrator>(mode, environment, config).await } diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Async.snap b/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Async.snap index cad57e737..ef4238632 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Async.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Async.snap @@ -9,6 +9,7 @@ use loco_rs::{ BackgroundWorker, Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -38,8 +39,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Blocking.snap b/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Blocking.snap index cad57e737..ef4238632 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Blocking.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Blocking.snap @@ -9,6 +9,7 @@ use loco_rs::{ BackgroundWorker, Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -38,8 +39,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_None.snap b/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_None.snap index 8d1799bd0..a16af2cfb 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_None.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_None.snap @@ -8,6 +8,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -36,8 +37,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Queue.snap b/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Queue.snap index cad57e737..ef4238632 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Queue.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__background__src_app_rs_Queue.snap @@ -9,6 +9,7 @@ use loco_rs::{ BackgroundWorker, Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -38,8 +39,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_None.snap b/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_None.snap index b09ac84eb..4dbd95391 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_None.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_None.snap @@ -8,6 +8,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -36,8 +37,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_Postgres.snap b/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_Postgres.snap index c9393b9eb..78f653a6b 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_Postgres.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_Postgres.snap @@ -9,6 +9,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -39,8 +40,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self, Migrator>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self, Migrator>(mode, environment, config).await } diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_Sqlite.snap b/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_Sqlite.snap index c9393b9eb..78f653a6b 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_Sqlite.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__db__src_app_rs_Sqlite.snap @@ -9,6 +9,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -39,8 +40,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self, Migrator>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self, Migrator>(mode, environment, config).await } diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__initializers__src_app_rs_with_initializers.snap b/loco-new/tests/templates/snapshots/r#mod__templates__initializers__src_app_rs_with_initializers.snap index 3199b528d..a53e75ee8 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__initializers__src_app_rs_with_initializers.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__initializers__src_app_rs_with_initializers.snap @@ -8,6 +8,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -36,8 +37,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/loco-new/tests/templates/snapshots/r#mod__templates__initializers__src_app_rs_without_initializers.snap b/loco-new/tests/templates/snapshots/r#mod__templates__initializers__src_app_rs_without_initializers.snap index 4691632a0..04f925ee1 100644 --- a/loco-new/tests/templates/snapshots/r#mod__templates__initializers__src_app_rs_without_initializers.snap +++ b/loco-new/tests/templates/snapshots/r#mod__templates__initializers__src_app_rs_without_initializers.snap @@ -8,6 +8,7 @@ use loco_rs::{ bgworker::{ Queue}, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -36,8 +37,8 @@ impl Hooks for App { ) } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self>(mode, environment).await + async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult> { + create_app::<Self>(mode, environment, config).await } async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { diff --git a/src/app.rs b/src/app.rs index 34cd6784f..49657c27f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -87,22 +87,23 @@ pub trait Hooks: Send { /// /// With DB: /// ```rust,ignore - /// async fn boot(mode: StartMode, environment: &str) -> Result<BootResult> { - /// create_app::<Self, Migrator>(mode, environment).await + /// async fn boot(mode: StartMode, environment: &str, config: Config) -> Result<BootResult> { + /// create_app::<Self, Migrator>(mode, environment, config).await /// } /// ```` /// /// Without DB: /// ```rust,ignore - /// async fn boot(mode: StartMode, environment: &str) -> Result<BootResult> { - /// create_app::<Self>(mode, environment).await + /// async fn boot(mode: StartMode, environment: &str, config: Config) -> Result<BootResult> { + /// create_app::<Self>(mode, environment, config).await /// } /// ```` /// /// /// # Errors /// Could not boot the application - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult>; + async fn boot(mode: StartMode, environment: &Environment, config: Config) + -> Result<BootResult>; /// Start serving the Axum web application on the specified address and /// port. @@ -142,6 +143,14 @@ pub trait Hooks: Send { Ok(false) } + /// Loads the configuration settings for the application based on the given environment. + /// + /// This function is responsible for retrieving the configuration for the application + /// based on the current environment. + async fn load_config(env: &Environment) -> Result<Config> { + env.load() + } + /// Returns the initial Axum router for the application, allowing the user /// to control the construction of the Axum router. This is where a fallback /// handler can be installed before middleware or other routes are added. diff --git a/src/boot.rs b/src/boot.rs index 9dafd31bf..31b3c42db 100644 --- a/src/boot.rs +++ b/src/boot.rs @@ -15,6 +15,7 @@ use crate::{ app::{AppContext, Hooks}, banner::print_banner, bgworker, cache, + config::Config, config::{self, WorkerMode}, controller::ListRoutes, environment::Environment, @@ -316,9 +317,10 @@ pub async fn run_db<H: Hooks, M: MigratorTrait>( /// /// # Errors /// When has an error to create DB connection. -pub async fn create_context<H: Hooks>(environment: &Environment) -> Result<AppContext> { - let config = environment.load()?; - +pub async fn create_context<H: Hooks>( + environment: &Environment, + config: Config, +) -> Result<AppContext> { if config.logger.pretty_backtrace { std::env::set_var("RUST_BACKTRACE", "1"); warn!( @@ -359,8 +361,9 @@ pub async fn create_context<H: Hooks>(environment: &Environment) -> Result<AppCo pub async fn create_app<H: Hooks, M: MigratorTrait>( mode: StartMode, environment: &Environment, + config: Config, ) -> Result<BootResult> { - let app_context = create_context::<H>(environment).await?; + let app_context = create_context::<H>(environment, config).await?; db::converge::<H, M>(&app_context.db, &app_context.config.database).await?; if let (Some(queue), Some(config)) = (&app_context.queue_provider, &app_context.config.queue) { @@ -374,8 +377,9 @@ pub async fn create_app<H: Hooks, M: MigratorTrait>( pub async fn create_app<H: Hooks>( mode: StartMode, environment: &Environment, + config: Config, ) -> Result<BootResult> { - let app_context = create_context::<H>(environment).await?; + let app_context = create_context::<H>(environment, config).await?; if let (Some(queue), Some(config)) = (&app_context.queue_provider, &app_context.config.queue) { bgworker::converge(queue, config).await?; diff --git a/src/cli.rs b/src/cli.rs index dcc5e1475..37bbf23c7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -106,8 +106,8 @@ enum Commands { /// Describe all application middlewares Middleware { // print out the middleware configurations. - #[arg(short, long, action)] - config: bool, + #[arg(short = 'c', long = "config", action)] + show_config: bool, }, /// Run a custom task #[clap(alias("t"))] @@ -135,8 +135,8 @@ enum Commands { /// Specify a path to a dedicated scheduler configuration file. by /// default load schedulers job setting from environment config. #[clap(value_parser)] - #[arg(short, long, action)] - config: Option<PathBuf>, + #[arg(short = 'c', long = "config", action)] + config_path: Option<PathBuf>, /// Show all configured jobs #[arg(short, long, action)] list: bool, @@ -520,13 +520,13 @@ pub async fn playground<H: Hooks>() -> crate::Result<AppContext> { let cli = Playground::parse(); let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into(); - let config = environment.load()?; + let config = H::load_config(&environment).await?; if !H::init_logger(&config, &environment)? { logger::init::<H>(&config.logger)?; } - let app_context = create_context::<H>(&environment).await?; + let app_context = create_context::<H>(&environment, config).await?; Ok(app_context) } @@ -561,7 +561,7 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> { let cli: Cli = Cli::parse(); let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into(); - let config = environment.load()?; + let config = H::load_config(&environment).await?; if !H::init_logger(&config, &environment)? { logger::init::<H>(&config.logger)?; @@ -586,7 +586,7 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> { StartMode::ServerOnly }; - let boot_result = create_app::<H, M>(start_mode, &environment).await?; + let boot_result = create_app::<H, M>(start_mode, &environment, config).await?; let serve_params = ServeParams { port: port.map_or(boot_result.app_context.config.server.port, |p| p), binding: binding @@ -597,26 +597,28 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> { #[cfg(feature = "with-db")] Commands::Db { command } => { if matches!(command, DbCommands::Create) { - db::create(&environment.load()?.database.uri).await?; + db::create(&config.database.uri).await?; } else { - let app_context = create_context::<H>(&environment).await?; + let app_context = create_context::<H>(&environment, config).await?; run_db::<H, M>(&app_context, command.into()).await?; } } #[cfg(any(feature = "bg_redis", feature = "bg_pg", feature = "bg_sqlt"))] - Commands::Jobs { command } => handle_job_command::<H>(command, &environment).await?, + Commands::Jobs { command } => { + handle_job_command::<H>(command, &environment, config).await?; + } Commands::Routes {} => { - let app_context = create_context::<H>(&environment).await?; + let app_context = create_context::<H>(&environment, config).await?; show_list_endpoints::<H>(&app_context); } - Commands::Middleware { config } => { - let app_context = create_context::<H>(&environment).await?; + Commands::Middleware { show_config } => { + let app_context = create_context::<H>(&environment, config).await?; let middlewares = list_middlewares::<H>(&app_context); for middleware in middlewares.iter().filter(|m| m.enabled) { println!( "{:<22} {}", middleware.id.bold(), - if config { + if show_config { middleware.detail.as_str() } else { "" @@ -630,17 +632,17 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> { } Commands::Task { name, params } => { let vars = task::Vars::from_cli_args(params); - let app_context = create_context::<H>(&environment).await?; + let app_context = create_context::<H>(&environment, config).await?; run_task::<H>(&app_context, name.as_ref(), &vars).await?; } Commands::Scheduler { name, - config, + config_path, tag, list, } => { - let app_context = create_context::<H>(&environment).await?; - run_scheduler::<H>(&app_context, config.as_ref(), name, tag, list).await?; + let app_context = create_context::<H>(&environment, config).await?; + run_scheduler::<H>(&app_context, config_path.as_ref(), name, tag, list).await?; } Commands::Generate { component } => { handle_generate_command::<H>(component, &config)?; @@ -699,7 +701,7 @@ pub async fn main<H: Hooks>() -> crate::Result<()> { let cli = Cli::parse(); let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into(); - let config = environment.load()?; + let config = H::load_config(&environment).await?; if !H::init_logger(&config, &environment)? { logger::init::<H>(&config.logger)?; @@ -724,7 +726,7 @@ pub async fn main<H: Hooks>() -> crate::Result<()> { StartMode::ServerOnly }; - let boot_result = create_app::<H>(start_mode, &environment).await?; + let boot_result = create_app::<H>(start_mode, &environment, config).await?; let serve_params = ServeParams { port: port.map_or(boot_result.app_context.config.server.port, |p| p), binding: binding.map_or( @@ -735,17 +737,17 @@ pub async fn main<H: Hooks>() -> crate::Result<()> { start::<H>(boot_result, serve_params, no_banner).await?; } Commands::Routes {} => { - let app_context = create_context::<H>(&environment).await?; + let app_context = create_context::<H>(&environment, config).await?; show_list_endpoints::<H>(&app_context) } - Commands::Middleware { config } => { - let app_context = create_context::<H>(&environment).await?; + Commands::Middleware { show_config } => { + let app_context = create_context::<H>(&environment, config).await?; let middlewares = list_middlewares::<H>(&app_context); for middleware in middlewares.iter().filter(|m| m.enabled) { println!( "{:<22} {}", middleware.id.bold(), - if config { + if show_config { middleware.detail.as_str() } else { "" @@ -759,19 +761,21 @@ pub async fn main<H: Hooks>() -> crate::Result<()> { } Commands::Task { name, params } => { let vars = task::Vars::from_cli_args(params); - let app_context = create_context::<H>(&environment).await?; + let app_context = create_context::<H>(&environment, config).await?; run_task::<H>(&app_context, name.as_ref(), &vars).await?; } #[cfg(any(feature = "bg_redis", feature = "bg_pg", feature = "bg_sqlt"))] - Commands::Jobs { command } => handle_job_command::<H>(command, &environment).await?, + Commands::Jobs { command } => { + handle_job_command::<H>(command, &environment, config).await? + } Commands::Scheduler { name, - config, + config_path, tag, list, } => { - let app_context = create_context::<H>(&environment).await?; - run_scheduler::<H>(&app_context, config.as_ref(), name, tag, list).await?; + let app_context = create_context::<H>(&environment, config).await?; + run_scheduler::<H>(&app_context, config_path.as_ref(), name, tag, list).await?; } Commands::Generate { component } => { handle_generate_command::<H>(component, &config)?; @@ -913,8 +917,9 @@ fn create_root_span(environment: &Environment) -> tracing::Span { async fn handle_job_command<H: Hooks>( command: JobsCommands, environment: &Environment, + config: Config, ) -> crate::Result<()> { - let app_context = create_context::<H>(environment).await?; + let app_context = create_context::<H>(environment, config).await?; let queue = app_context.queue_provider.map_or_else( || { println!("queue not configured"); diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 54c6f9870..34f70a087 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -11,6 +11,7 @@ //! use loco_rs::{ //! app::{AppContext, Hooks}, //! boot::{create_app, BootResult, StartMode}, +//! config::Config, //! controller::AppRoutes, //! prelude::*, //! task::Tasks, @@ -43,8 +44,8 @@ //! // .add_route(controllers::notes::routes()) //! } //! -//! async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult>{ -//! create_app::<Self, Migrator>(mode, environment).await +//! async fn boot(mode: StartMode, environment: &Environment, config: Config) -> Result<BootResult>{ +//! create_app::<Self, Migrator>(mode, environment, config).await //! } //! //! async fn connect_workers(_ctx: &AppContext, _queue: &Queue) -> Result<()> { diff --git a/src/testing/request.rs b/src/testing/request.rs index 1aec50ff0..1ed0b578f 100644 --- a/src/testing/request.rs +++ b/src/testing/request.rs @@ -33,7 +33,8 @@ use crate::{ /// } /// ``` pub async fn boot_test<H: Hooks>() -> Result<BootResult> { - H::boot(boot::StartMode::ServerOnly, &Environment::Test).await + let config = H::load_config(&Environment::Test).await?; + H::boot(boot::StartMode::ServerOnly, &Environment::Test, config).await } #[allow(clippy::future_not_send)] diff --git a/src/tests_cfg/db.rs b/src/tests_cfg/db.rs index 136b60077..627703d65 100644 --- a/src/tests_cfg/db.rs +++ b/src/tests_cfg/db.rs @@ -8,6 +8,7 @@ use crate::{ app::{AppContext, Hooks, Initializer}, bgworker::Queue, boot::{create_app, BootResult, StartMode}, + config::Config, controller::AppRoutes, environment::Environment, task::Tasks, @@ -98,8 +99,12 @@ impl Hooks for AppHook { AppRoutes::with_default_routes() } - async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> { - create_app::<Self, Migrator>(mode, environment).await + async fn boot( + mode: StartMode, + environment: &Environment, + config: Config, + ) -> Result<BootResult> { + create_app::<Self, Migrator>(mode, environment, config).await } async fn connect_workers(_ctx: &AppContext, _q: &Queue) -> Result<()> {