From d7798783ba50735dbae5a68d97384918be2956eb Mon Sep 17 00:00:00 2001 From: august kline Date: Wed, 13 Nov 2024 21:38:41 -0500 Subject: [PATCH] Initial commit --- .gitignore | 3 + Cargo.lock | 1703 ++++++++++++++++++ Cargo.toml | 14 + db/main/migrations/20241013202105_init.sql | 27 + db/memory/migrations/20241014020038_init.sql | 10 + src/assets.rs | 201 +++ src/drafts.rs | 216 +++ src/lib.rs | 83 + src/pages.rs | 155 ++ src/posts.rs | 97 + src/users.rs | 475 +++++ src/util.rs | 54 + 12 files changed, 3038 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 db/main/migrations/20241013202105_init.sql create mode 100644 db/memory/migrations/20241014020038_init.sql create mode 100644 src/assets.rs create mode 100644 src/drafts.rs create mode 100644 src/lib.rs create mode 100644 src/pages.rs create mode 100644 src/posts.rs create mode 100644 src/users.rs create mode 100644 src/util.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac0b067 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +.DS_Store + diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ad503fd --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1703 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "allocator-api2" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" + +[[package]] +name = "anyhow" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" + +[[package]] +name = "argon2" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072" +dependencies = [ + "base64ct", + "blake2", + "cpufeatures", + "password-hash", +] + +[[package]] +name = "atoi" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28d99ec8bfea296261ca1af174f24225171fea9664ba9003cbebee704810528" +dependencies = [ + "num-traits", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "base64ct" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +dependencies = [ + "serde", +] + +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "blogdb" +version = "0.1.0" +dependencies = [ + "anyhow", + "argon2", + "infer", + "mime_guess", + "rand", + "rand_chacha", + "sqlx", + "tokio", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" + +[[package]] +name = "cc" +version = "1.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfb" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f" +dependencies = [ + "byteorder", + "fnv", + "uuid", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "cpufeatures" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608697df725056feaccfa42cffdaeeec3fccc4ffc38358ecd19b243e716a78e0" +dependencies = [ + "libc", +] + +[[package]] +name = "crc" +version = "3.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69e6e4d7b33a94f0991c26729976b10ebde1d34c3ee82408fb536164fa10d636" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5" + +[[package]] +name = "crossbeam-queue" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "der" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" +dependencies = [ + "const-oid", + "pem-rfc7468", + "zeroize", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "const-oid", + "crypto-common", + "subtle", +] + +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +dependencies = [ + "serde", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "etcetera" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943" +dependencies = [ + "cfg-if", + "home", + "windows-sys 0.48.0", +] + +[[package]] +name = "event-listener" +version = "5.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6032be9bd27023a771701cc49f9f053c751055f71efb2e0ae5c15809093675ba" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "fastrand" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" + +[[package]] +name = "flume" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55ac459de2512911e4b674ce33cf20befaba382d05b62b008afc1c8b57cbf181" +dependencies = [ + "futures-core", + "futures-sink", + "spin", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-intrusive" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d930c203dd0b6ff06e0201a4a2fe9149b43c684fd4420555b26d21b1a02956f" +dependencies = [ + "futures-core", + "lock_api", + "parking_lot", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", + "allocator-api2", +] + +[[package]] +name = "hashbrown" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" + +[[package]] +name = "hashlink" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" +dependencies = [ + "hashbrown 0.14.5", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +dependencies = [ + "equivalent", + "hashbrown 0.15.0", +] + +[[package]] +name = "infer" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc150e5ce2330295b8616ce0e3f53250e53af31759a9dbedad1621ba29151847" +dependencies = [ + "cfb", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +dependencies = [ + "spin", +] + +[[package]] +name = "libc" +version = "0.2.159" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" + +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + +[[package]] +name = "libsqlite3-sys" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi", + "libc", + "wasi", + "windows-sys 0.52.0", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-bigint-dig" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc84195820f291c7697304f3cbdadd1cb7199c0efc917ff5eafd71225c136151" +dependencies = [ + "byteorder", + "lazy_static", + "libm", + "num-integer", + "num-iter", + "num-traits", + "rand", + "smallvec", + "zeroize", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", + "libm", +] + +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.6", +] + +[[package]] +name = "password-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" +dependencies = [ + "base64ct", + "rand_core", + "subtle", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pem-rfc7468" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +dependencies = [ + "base64ct", +] + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkcs1" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8ffb9f10fa047879315e6625af03c164b16962a5368d724ed16323b68ace47f" +dependencies = [ + "der", + "pkcs8", + "spki", +] + +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rsa" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0e5124fcb30e76a7e79bfee683a2746db83784b86289f6251b54b7950a0dfc" +dependencies = [ + "const-oid", + "digest", + "num-bigint-dig", + "num-integer", + "num-traits", + "pkcs1", + "pkcs8", + "rand_core", + "signature", + "spki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "rustix" +version = "0.38.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "digest", + "rand_core", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +dependencies = [ + "serde", +] + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93334716a037193fac19df402f8571269c84a00852f6a7066b5d2616dcd64d3e" +dependencies = [ + "sqlx-core", + "sqlx-macros", + "sqlx-mysql", + "sqlx-postgres", + "sqlx-sqlite", +] + +[[package]] +name = "sqlx-core" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d8060b456358185f7d50c55d9b5066ad956956fddec42ee2e8567134a8936e" +dependencies = [ + "atoi", + "byteorder", + "bytes", + "crc", + "crossbeam-queue", + "either", + "event-listener", + "futures-channel", + "futures-core", + "futures-intrusive", + "futures-io", + "futures-util", + "hashbrown 0.14.5", + "hashlink", + "hex", + "indexmap", + "log", + "memchr", + "once_cell", + "paste", + "percent-encoding", + "serde", + "serde_json", + "sha2", + "smallvec", + "sqlformat", + "thiserror", + "tokio", + "tokio-stream", + "tracing", + "url", +] + +[[package]] +name = "sqlx-macros" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cac0692bcc9de3b073e8d747391827297e075c7710ff6276d9f7a1f3d58c6657" +dependencies = [ + "proc-macro2", + "quote", + "sqlx-core", + "sqlx-macros-core", + "syn", +] + +[[package]] +name = "sqlx-macros-core" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1804e8a7c7865599c9c79be146dc8a9fd8cc86935fa641d3ea58e5f0688abaa5" +dependencies = [ + "dotenvy", + "either", + "heck", + "hex", + "once_cell", + "proc-macro2", + "quote", + "serde", + "serde_json", + "sha2", + "sqlx-core", + "sqlx-mysql", + "sqlx-postgres", + "sqlx-sqlite", + "syn", + "tempfile", + "tokio", + "url", +] + +[[package]] +name = "sqlx-mysql" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64bb4714269afa44aef2755150a0fc19d756fb580a67db8885608cf02f47d06a" +dependencies = [ + "atoi", + "base64", + "bitflags", + "byteorder", + "bytes", + "crc", + "digest", + "dotenvy", + "either", + "futures-channel", + "futures-core", + "futures-io", + "futures-util", + "generic-array", + "hex", + "hkdf", + "hmac", + "itoa", + "log", + "md-5", + "memchr", + "once_cell", + "percent-encoding", + "rand", + "rsa", + "serde", + "sha1", + "sha2", + "smallvec", + "sqlx-core", + "stringprep", + "thiserror", + "tracing", + "whoami", +] + +[[package]] +name = "sqlx-postgres" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fa91a732d854c5d7726349bb4bb879bb9478993ceb764247660aee25f67c2f8" +dependencies = [ + "atoi", + "base64", + "bitflags", + "byteorder", + "crc", + "dotenvy", + "etcetera", + "futures-channel", + "futures-core", + "futures-io", + "futures-util", + "hex", + "hkdf", + "hmac", + "home", + "itoa", + "log", + "md-5", + "memchr", + "once_cell", + "rand", + "serde", + "serde_json", + "sha2", + "smallvec", + "sqlx-core", + "stringprep", + "thiserror", + "tracing", + "whoami", +] + +[[package]] +name = "sqlx-sqlite" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5b2cf34a45953bfd3daaf3db0f7a7878ab9b7a6b91b422d24a7a9e4c857b680" +dependencies = [ + "atoi", + "flume", + "futures-channel", + "futures-core", + "futures-executor", + "futures-intrusive", + "futures-util", + "libsqlite3-sys", + "log", + "percent-encoding", + "serde", + "serde_urlencoded", + "sqlx-core", + "tracing", + "url", +] + +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" +dependencies = [ + "cfg-if", + "fastrand", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + +[[package]] +name = "thiserror" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-stream" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicase" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e51b68083f157f853b6379db119d1c1be0e6e4dec98101079dec41f6f5cf6df" + +[[package]] +name = "unicode-bidi" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "unicode-normalization" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-properties" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0" + +[[package]] +name = "unicode_categories" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" + +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "uuid" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + +[[package]] +name = "whoami" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "372d5b87f58ec45c384ba03563b03544dc5fadc3983e434b286913f5b4a9bb6d" +dependencies = [ + "redox_syscall", + "wasite", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d545cda --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "blogdb" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.89" +argon2 = { version = "0.5.3", features = ["std"] } +infer = "0.16.0" +mime_guess = "2.0.5" +rand = "0.8.5" +rand_chacha = { version = "0.3.1", features = ["std"] } +sqlx = { version = "0.8.2", features = ["sqlite", "runtime-tokio", "macros"] } +tokio = { version = "1.40.0", features = ["macros", "rt", "rt-multi-thread"] } diff --git a/db/main/migrations/20241013202105_init.sql b/db/main/migrations/20241013202105_init.sql new file mode 100644 index 0000000..650b0ac --- /dev/null +++ b/db/main/migrations/20241013202105_init.sql @@ -0,0 +1,27 @@ +CREATE TABLE IF NOT EXISTS users ( + name VARCHAR(255) UNIQUE NOT NULL, + password_hash TEXT NOT NULL, + about TEXT, + home TEXT +); + +CREATE TABLE IF NOT EXISTS assets ( + slug TEXT UNIQUE NOT NULL, + mime TEXT NOT NULL, + data BLOB NOT NULL +); + +CREATE TABLE IF NOT EXISTS posts ( + id TEXT PRIMARY KEY DEFAULT (lower(hex (randomblob (16)))), + title VARCHAR(255) NOT NULL, + date TEXT, + tags TEXT, + content TEXT +); + +CREATE TABLE IF NOT EXISTS drafts ( + id TEXT PRIMARY KEY DEFAULT (lower(hex (randomblob (16)))), + title VARCHAR(255) NOT NULL, + tags TEXT, + content TEXT +); diff --git a/db/memory/migrations/20241014020038_init.sql b/db/memory/migrations/20241014020038_init.sql new file mode 100644 index 0000000..c098913 --- /dev/null +++ b/db/memory/migrations/20241014020038_init.sql @@ -0,0 +1,10 @@ +CREATE TABLE IF NOT EXISTS pages ( + slug STRING PRIMARY KEY UNIQUE NOT NULL, + content STRING NOT NULL +); + +CREATE TABLE IF NOT EXISTS sessions ( + id INTEGER PRIMARY KEY NOT NULL, + user STRING NOT NULL, + expires TEXT +); diff --git a/src/assets.rs b/src/assets.rs new file mode 100644 index 0000000..2ffb944 --- /dev/null +++ b/src/assets.rs @@ -0,0 +1,201 @@ +use std::{fmt::Debug, path::PathBuf, str::FromStr}; + +use anyhow::anyhow; +use infer::Type; +use mime_guess::Mime; +use rand::random; +use sqlx::prelude::FromRow; + +use crate::BlogDb; + +#[derive(Clone, FromRow)] +pub struct Asset { + pub slug: String, + pub mime: String, + pub data: Vec, +} + +impl Debug for Asset { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Asset(slug: {}, mime: {})", self.slug, self.mime)?; + Ok(()) + } +} + +const SUPPORTED_MIME_TYPES: &[&str] = &["image/jpeg", "font/woff", "font/woff2"]; + +impl BlogDb { + /// Add an asset with the given name and data. + /// + /// If the asset shares a name with another asset in the parent path, the name will be prefixed with a unique id. Use the returned [`Asset`] name to access this asset in the future. + pub async fn add_asset(&self, slug: S, data: D) -> anyhow::Result + where + S: AsRef, + D: Into>, + { + let data = data.into(); + + let slug = PathBuf::from(slug.as_ref()); + let asset_ext: &str; + let mime = match slug.extension() { + Some(_) => { + asset_ext = ""; + match mime_guess::from_path(&slug).first() { + Some(mime) => &mime.to_string(), + None => match infer::get(&data) { + Some(mime) => mime.mime_type(), + None => { + return Err(anyhow::Error::msg( + "Couldn't get the mime type of the provided asset", + )) + } + }, + } + } + None => match infer::get(&data) { + Some(mime) => { + asset_ext = mime.extension(); + mime.mime_type() + } + None => { + return Err(anyhow::Error::msg( + "Couldn't get the mime type of the provided asset", + )) + } + }, + }; + + let mut asset_name = slug + .parent() + .map(|parent| parent.to_str().unwrap()) + .unwrap_or("") + .to_string(); + asset_name.push_str(&random::().to_string()); + if asset_ext.is_empty() { + asset_name.push_str(&format!("-{}", slug.file_name().unwrap().to_str().unwrap())); + } else { + asset_name.push_str(&format!( + "-{}.{asset_ext}", + slug.file_stem().unwrap().to_str().unwrap() + )); + } + + let asset: Asset = + sqlx::query_as("INSERT INTO assets (slug, mime, data) VALUES (?,?,?) RETURNING *") + .bind(&asset_name) + .bind(&mime) + .bind(&data) + .fetch_one(&self.db) + .await?; + Ok(asset) + } + + async fn asset_path_collides(&self, slug: S) -> bool + where + S: AsRef, + { + self.get_asset(slug).await.is_ok() + } + + async fn mime_is_supported(&self, r#type: &Type) -> bool { + SUPPORTED_MIME_TYPES.contains(&r#type.mime_type()) + } + + /// Adds a collection of asset tuples to the db + pub async fn add_assets(&self, assets: A) -> anyhow::Result<()> + where + A: IntoIterator, + S: AsRef, + D: Into>, + { + let assets = assets.into_iter(); + for asset in assets.into_iter() { + let slug = asset.0; + let data = asset.1.into(); + + let path = PathBuf::from(slug.as_ref()); + let mime = match path.extension() { + Some(ext) => match ext.to_str() { + Some(str) => match mime_guess::from_ext(str).first() { + Some(mime) => mime.to_string(), + None => match infer::get(&data) { + Some(ext) => ext.mime_type().to_string(), + None => "text/plain".to_string(), + }, + }, + None => match infer::get(&data) { + Some(ext) => ext.mime_type().to_string(), + None => "text/plain".to_string(), + }, + }, + None => match infer::get(&data) { + Some(ext) => ext.mime_type().to_string(), + None => "text/plain".to_string(), + }, + }; + + let result = + sqlx::query("REPLACE INTO assets (slug, mime, data) VALUES (?,?,?) RETURNING *") + .bind(slug.as_ref()) + .bind(mime) + .bind(data) + .execute(&self.db) + .await; + } + Ok(()) + } + + /// Get asset by slug + pub async fn get_asset(&self, slug: S) -> anyhow::Result + where + S: AsRef, + { + let asset = sqlx::query_as("SELECT * FROM assets WHERE slug=?") + .bind(slug.as_ref()) + .fetch_one(&self.db) + .await; + // let asset: Asset = sqlx::query_as("SELECT * FROM assets WHERE slug=?") + // .bind(slug.as_ref()) + // .fetch_one(&self.db) + // .await?; + Ok(asset?) + } + + /// Get all assets + pub async fn get_assets(&self) -> anyhow::Result> { + let assets: Vec = sqlx::query_as("SELECT * FROM assets") + .fetch_all(&self.db) + .await?; + Ok(assets) + } + + /// Delete asset by name + pub async fn delete_asset(&self, name: S) -> anyhow::Result<()> + where + S: AsRef, + { + sqlx::query("DELETE FROM assets WHERE name=?") + .bind(name.as_ref()) + .execute(&self.db) + .await?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use crate::util::tests::*; + use sqlx::SqlitePool; + + #[sqlx::test] + async fn assets(pool: SqlitePool) -> anyhow::Result<()> { + let (db, session_id) = get_init_db(pool).await?; + let slug = "picture"; + // PNG magic numbers + let data = &[0x89, 0x50, 0x4e, 0x47]; + let asset = db.add_asset(slug, data).await; + assert!(asset.is_ok()); + + Ok(()) + } +} diff --git a/src/drafts.rs b/src/drafts.rs new file mode 100644 index 0000000..d140165 --- /dev/null +++ b/src/drafts.rs @@ -0,0 +1,216 @@ +use sqlx::prelude::FromRow; + +use crate::{posts::Post, BlogDb}; + +#[derive(Clone, FromRow, Debug)] +pub struct Draft { + pub id: String, + pub title: String, + // Valid markdown + pub content: String, + // Separated by commas, no whitespace + pub tags: String, +} + +impl BlogDb { + /// Create a draft from an existing post, returning a [Draft] either for the new draft or for an existing draft + pub async fn edit_post(&self, id: S) -> anyhow::Result + where + S: AsRef, + { + let draft_exists = sqlx::query_as::<_, Draft>("SELECT * FROM drafts WHERE id=?") + .bind(id.as_ref()) + .fetch_one(&self.db) + .await; + + if let Ok(draft) = draft_exists { + Ok(draft) + } else { + let post_exists = sqlx::query_as::<_, Post>("SELECT * FROM posts WHERE id=?") + .bind(id.as_ref()) + .fetch_one(&self.db) + .await; + if let Ok(post) = post_exists { + let new_draft: Draft = sqlx::query_as("INSERT INTO drafts (id, title, tags, content) SELECT id, title, tags, content FROM posts WHERE id=? RETURNING *") + .bind(post.id).fetch_one(&self.db).await?; + Ok(new_draft) + } else { + Err(anyhow::Error::msg( + "Tried to edit a post that doesn't exist", + )) + } + } + } + + /// Get draft by id + pub async fn get_draft(&self, id: S) -> anyhow::Result + where + S: AsRef, + { + let draft: Draft = sqlx::query_as("SELECT * FROM drafts WHERE id=?") + .bind(id.as_ref()) + .fetch_one(&self.db) + .await?; + Ok(draft) + } + + /// Delete draft by id + pub async fn delete_draft(&self, id: S) -> anyhow::Result<()> + where + S: AsRef, + { + sqlx::query_as::<_, Draft>("DELETE FROM drafts WHERE id=? RETURNING *") + .bind(id.as_ref()) + .fetch_one(&self.db) + .await?; + Ok(()) + } + + /// Get all drafts + pub async fn get_drafts(&self, session_id: i64) -> anyhow::Result> { + self.check_session(session_id).await?; + let drafts: Vec = sqlx::query_as("SELECT * FROM drafts") + .fetch_all(&self.db) + .await?; + Ok(drafts) + } + + /// Create a new draft with the given data. See [`edit_post`](Self::edit_post) to create a draft from an existing post + pub async fn add_draft(&self, title: S, tags: T, content: R) -> anyhow::Result + where + S: AsRef, + T: AsRef, + R: AsRef, + { + let draft: Draft = + sqlx::query_as("REPLACE INTO drafts (title, tags, content) VALUES (?,?,?) RETURNING *") + .bind(title.as_ref()) + .bind(tags.as_ref()) + .bind(content.as_ref()) + .fetch_one(&self.db) + .await?; + Ok(draft) + } + + pub async fn update_draft_title(&self, id: S, title: T) -> anyhow::Result<()> + where + S: AsRef, + T: AsRef, + { + sqlx::query("UPDATE drafts SET (title) = ? WHERE id=?") + .bind(title.as_ref()) + .bind(id.as_ref()) + .execute(&self.db) + .await?; + Ok(()) + } + + pub async fn update_draft_tags(&self, id: S, tags: T) -> anyhow::Result<()> + where + S: AsRef, + T: AsRef, + { + sqlx::query("UPDATE drafts SET (tags) = ? WHERE id=?") + .bind(tags.as_ref()) + .bind(id.as_ref()) + .execute(&self.db) + .await?; + Ok(()) + } + + pub async fn update_draft_content(&self, id: S, content: T) -> anyhow::Result<()> + where + S: AsRef, + T: AsRef, + { + sqlx::query("UPDATE drafts SET (content) = ? WHERE id=?") + .bind(content.as_ref()) + .bind(id.as_ref()) + .execute(&self.db) + .await?; + Ok(()) + } + + /// Publish draft by id, creating a new post if one with the id doesn't already exist, and updating the post if it does. + /// + /// Note that if a new post is created its id may be incremented, making the id passed into this function invalid. + /// Always use the returned [`Post`] id for future actions. + pub async fn publish_draft(&self, id: S) -> anyhow::Result + where + S: AsRef, + { + let update = sqlx::query_as::<_, Post>( + "UPDATE posts + SET (title, tags, content) = (SELECT title, tags, content FROM drafts WHERE id=?) + WHERE id=? + RETURNING *", + ) + .bind(id.as_ref()) + .fetch_one(&self.db) + .await; + + if let Ok(post) = update { + self.delete_draft(id).await?; + Ok(post) + } else { + let new_post: Post = sqlx::query_as( + "INSERT INTO posts (title, date, tags, content) + SELECT title, DATE('now', 'localtime'), tags, content FROM drafts WHERE id=? + RETURNING *", + ) + .bind(id.as_ref()) + .fetch_one(&self.db) + .await?; + self.delete_draft(id).await?; + Ok(new_post) + } + } +} + +#[cfg(test)] +mod tests { + use crate::util::tests::*; + use sqlx::SqlitePool; + + #[sqlx::test] + async fn drafts(pool: SqlitePool) -> anyhow::Result<()> { + let (db, session_id, post) = init_and_add_post(pool).await?; + + // Test draft that updates existing post + + let id = post.id; + db.edit_post(id).await?; + + let title = "this is an updated title"; + let content = "this is some updated content"; + let tags = "updated"; + + db.update_draft_title(id, title).await?; + db.update_draft_content(id, content).await?; + db.update_draft_tags(id, tags).await?; + db.publish_draft(id).await?; + assert!(db.get_draft(id).await.is_err()); // Published drafts should be deleted + + let post = db.get_post(id).await?; + assert_eq!(post.title, title.to_string()); + assert_eq!(post.content, content.to_string()); + assert_eq!(post.tags, tags.to_string()); + + // Test new draft + + let title = "this is a new title"; + let content = "this is new content"; + let tags = "new"; + + let draft = db.add_draft(title, tags, content).await?; + assert!(db.get_draft(draft.id).await.is_ok()); + + let post = db.publish_draft(draft.id).await?; + assert!(db.get_post(post.id).await.is_ok()); + assert_eq!(post.title, title.to_string()); + assert_eq!(post.content, content.to_string()); + assert_eq!(post.tags, tags.to_string()); + + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..9072835 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,83 @@ +mod assets; +mod drafts; +mod pages; +pub mod posts; +pub mod users; +mod util; + +use posts::Post; +use util::*; + +use std::fs::File; +use std::str::FromStr; + +use sqlx::sqlite::SqliteJournalMode; +use sqlx::{ + sqlite::{SqliteConnectOptions, SqlitePoolOptions}, + SqlitePool, +}; + +#[cfg(not(debug_assertions))] +const MEMORY_URL: &str = "sqlite://:memory:"; +#[cfg(debug_assertions)] +const MEMORY_URL: &str = "sqlite://cache.db"; + +/// A Sqlite database for all blog data. +/// +/// It uses a file-backed db for posts, drafts, users, and assets, and an in-memory db for cached +/// web pages and session management. +#[derive(Clone)] +pub struct BlogDb { + db: SqlitePool, + memory: SqlitePool, +} + +impl BlogDb { + /// Create a new BlogDb with an initial user. + pub async fn new(username: S, password: String, db_url: T) -> anyhow::Result + where + S: AsRef, + T: AsRef, + { + let db_name = db_url.as_ref().strip_prefix("sqlite://").unwrap(); + if File::open(db_name).is_err() { + let _ = File::create_new(db_name); + } + let db = SqlitePoolOptions::new() + .connect_with( + SqliteConnectOptions::from_str(db_url.as_ref())? + .journal_mode(SqliteJournalMode::Wal), + ) + .await?; + #[cfg(debug_assertions)] + { + let db_name = MEMORY_URL.strip_prefix("sqlite://").unwrap(); + if File::open(db_name).is_err() { + let _ = File::create_new(db_name); + } + } + let memory = SqlitePoolOptions::new() + .connect_with( + #[cfg(not(debug_assertions))] + SqliteConnectOptions::from_str(MEMORY_URL)?.journal_mode(SqliteJournalMode::Memory), + #[cfg(debug_assertions)] + SqliteConnectOptions::from_str(MEMORY_URL.as_ref())? + .journal_mode(SqliteJournalMode::Wal), + ) + .await?; + Self::run_main_migrations(&db).await?; + Self::run_memory_migrations(&memory).await?; + Self::add_initial_user(&db, username.as_ref(), password).await?; + Ok(Self { db, memory }) + } + + async fn run_main_migrations(db: &SqlitePool) -> anyhow::Result<()> { + sqlx::migrate!("db/main/migrations").run(db).await?; + Ok(()) + } + + async fn run_memory_migrations(cache: &SqlitePool) -> anyhow::Result<()> { + sqlx::migrate!("db/memory/migrations").run(cache).await?; + Ok(()) + } +} diff --git a/src/pages.rs b/src/pages.rs new file mode 100644 index 0000000..4ee7114 --- /dev/null +++ b/src/pages.rs @@ -0,0 +1,155 @@ +use std::path::StripPrefixError; + +use crate::BlogDb; + +impl BlogDb { + // pub async fn cache_post(&self, id: i64, cacher: C) -> anyhow::Result<()> + // where + // C: Cacher + Send + 'static, + // { + // let post = self.get_post(id).await?; + // let cached_page = + // task::spawn_blocking(move || -> anyhow::Result { cacher.cache(post) }) + // .await??; // await?? me?? me await?? + // let slug = format!("blog/{id}"); + // sqlx::query("REPLACE INTO pages (slug, content) VALUES (?, ?)") + // .bind(slug) + // .bind(cached_page) + // .execute(&self.memory) + // .await?; + // Ok(()) + // } + // + + pub async fn add_page(&self, slug: S, content: T) -> anyhow::Result<()> + where + S: AsRef, + T: AsRef, + { + let slug = prepare_slug(slug.as_ref()).await; + sqlx::query("INSERT INTO pages (slug, content) VALUES (?, ?)") + .bind(slug) + .bind(content.as_ref()) + .execute(&self.memory) + .await?; + Ok(()) + } + + pub async fn get_page(&self, slug: S) -> anyhow::Result + where + S: AsRef, + { + let slug = prepare_slug_query(slug.as_ref()).await; + let page: (String,) = sqlx::query_as("SELECT content FROM pages WHERE slug=?") + .bind(slug) + .fetch_one(&self.memory) + .await?; + Ok(page.0) + } + + pub async fn update_page(&self, slug: S, content: T) -> anyhow::Result<()> + where + S: AsRef, + T: AsRef, + { + let slug = prepare_slug(slug.as_ref()).await; + sqlx::query("REPLACE INTO pages (slug, content) VALUES (?, ?)") + .bind(slug) + .bind(content.as_ref()) + .execute(&self.memory) + .await?; + Ok(()) + } + + pub async fn delete_page(&self, slug: S) -> anyhow::Result<()> + where + S: AsRef, + { + let slug = prepare_slug(slug.as_ref()).await; + sqlx::query("DELETE FROM pages WHERE slug=?") + .bind(slug) + .execute(&self.memory) + .await?; + Ok(()) + } + + pub async fn add_pages(&self, pages: P) -> anyhow::Result<()> + where + P: IntoIterator, + S: AsRef, + C: AsRef, + { + let pages = pages.into_iter(); + for page in pages.into_iter() { + let slug = prepare_slug(page.0.as_ref()).await; + let content = page.1.as_ref(); + + let result: (String, String) = + sqlx::query_as("REPLACE INTO pages (slug, content) VALUES (?,?) RETURNING *") + .bind(slug) + .bind(content) + .fetch_one(&self.memory) + .await?; + } + Ok(()) + } + + pub async fn get_pages(&self) -> anyhow::Result> { + let assets: Vec<(String, String)> = sqlx::query_as("SELECT * FROM assets") + .fetch_all(&self.db) + .await?; + Ok(assets) + } +} + +/// Combine multiple patterns that match a page (e.g. `/blog/index.html` and `/blog/`) +async fn prepare_slug<'a>(mut slug: &'a str) -> &str { + if slug.ends_with(".html") { + slug = slug.strip_suffix(".html").unwrap() + } else if slug.ends_with("/index.html") { + slug = slug.strip_suffix("/index.html").unwrap() + } + if slug.ends_with("/") { + slug = slug.strip_suffix("/").unwrap() + } + slug +} + +/// Combine multiple patterns that match a page (e.g. `/blog/index.html` and `/blog/`) +async fn prepare_slug_query<'a>(mut slug: &'a str) -> &str { + if slug.ends_with(".html") { + slug = slug.strip_suffix(".html").unwrap() + } else if slug.ends_with("/index.html") { + slug = slug.strip_suffix("/index.html").unwrap() + } + if slug.ends_with("/") { + slug = slug.strip_suffix("/").unwrap() + } + if slug == "" { + slug = "index" + } + slug +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::util::tests::*; + use sqlx::SqlitePool; + + // struct PostCacher; + + // impl Cacher for PostCacher { + // fn cache(&self, cacheable: Post) -> anyhow::Result { + // Ok("cached page!".to_string()) + // } + // } + + // #[sqlx::test] + // async fn cache(pool: SqlitePool) -> anyhow::Result<()> { + // let (db, session_id, post) = init_and_add_post(pool).await?; + // db.cache_post(post.id, PostCacher).await?; + + // Ok(()) + // } +} diff --git a/src/posts.rs b/src/posts.rs new file mode 100644 index 0000000..d5d2db7 --- /dev/null +++ b/src/posts.rs @@ -0,0 +1,97 @@ +use sqlx::prelude::FromRow; + +use crate::BlogDb; + +#[derive(Clone, FromRow, Debug, Default)] +pub struct Post { + pub id: String, + pub title: String, + pub date: String, + // Valid markdown + pub content: String, + // Separated by commas, no whitespace + pub tags: String, +} + +impl BlogDb { + /// Create a single post with the given data. + /// + /// This is mostly used for testing: most of the time you should use [`BlogDb::publish_draft`] to publish a post from a draft + pub async fn add_post(&self, title: S, tags: T, content: R) -> anyhow::Result + where + S: AsRef, + T: AsRef, + R: AsRef, + { + let post: Post = sqlx::query_as( + "REPLACE INTO posts (title, date, tags, content) VALUES (?, date(), ?, ?) RETURNING *", + ) + .bind(title.as_ref()) + .bind(tags.as_ref()) + .bind(content.as_ref()) + .fetch_one(&self.db) + .await?; + Ok(post) + } + + /// Get a single post by id + pub async fn get_post(&self, id: S) -> anyhow::Result + where + S: AsRef, + { + let post: Post = sqlx::query_as("SELECT * FROM posts WHERE id=?") + .bind(id.as_ref()) + .fetch_one(&self.db) + .await?; + Ok(post) + } + + /// Get all posts + pub async fn get_posts(&self) -> anyhow::Result> { + let posts: Vec = sqlx::query_as("SELECT * FROM posts") + .fetch_all(&self.db) + .await?; + Ok(posts) + } + + /// Delete post by id + pub async fn delete_post(&self, id: S) -> anyhow::Result<()> + where + S: AsRef, + { + sqlx::query_as::<_, Post>("DELETE FROM posts WHERE id=? RETURNING *") + .bind(id.as_ref()) + .fetch_one(&self.db) + .await?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use crate::util::tests::*; + use sqlx::SqlitePool; + + #[sqlx::test] + async fn posts(pool: SqlitePool) -> anyhow::Result<()> { + let (db, session_id) = get_init_db(pool).await?; + + let title = "ʕ·ᴥ·ʔ"; + let content = "ʕ·ᴥ·ʔ- hello there"; + let tags = "george"; + let post = db.add_post(title, tags, content).await?; + + let id = post.id; + + let post = db.get_post(id).await?; + assert_eq!(post.title, title.to_string()); + assert_eq!(post.content, content.to_string()); + assert_eq!(post.tags, tags.to_string()); + + db.delete_post(id).await?; + let deleted = db.get_post(id).await.is_err(); + assert!(deleted); + + Ok(()) + } +} diff --git a/src/users.rs b/src/users.rs new file mode 100644 index 0000000..6ef0c5e --- /dev/null +++ b/src/users.rs @@ -0,0 +1,475 @@ +use std::fmt::Display; + +use anyhow::{anyhow, Context}; +use rand::{RngCore, SeedableRng}; +use rand_chacha::ChaCha20Rng; +use sqlx::{prelude::FromRow, SqlitePool}; + +use crate::{assets::Asset, hash_value, verify_hashed_value, BlogDb}; + +#[derive(Debug, PartialEq)] +pub struct UserInfo { + pub name: String, + pub about: String, + pub home: String, +} + +impl From for UserInfo { + fn from(value: User) -> Self { + Self { + name: value.name, + about: value.about, + home: value.home, + } + } +} + +#[derive(Clone, FromRow, Default)] +struct User { + name: String, + password_hash: String, + about: String, // this isn't great but these r the home and about page html + home: String, +} + +impl Display for User { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "User(name: {}, password_hash: ###, homepage: {:?})", + self.name, self.home + ) + } +} + +impl BlogDb { + pub(super) async fn verify_password(&self, user: S, password: String) -> anyhow::Result<()> + where + S: AsRef, + { + let user: User = sqlx::query_as("SELECT * FROM users WHERE name=?") + .bind(user.as_ref()) + .fetch_one(&self.db) + .await?; + let hash = user.password_hash; + verify_hashed_value(password, hash).await + } + + pub(super) async fn add_initial_user( + db: &SqlitePool, + username: S, + password: String, + ) -> anyhow::Result<()> + where + S: AsRef, + { + let password_hash = hash_value(password).await?; + sqlx::query("INSERT OR IGNORE INTO users (name, password_hash) VALUES (?,?)") + .bind(username.as_ref()) + .bind(password_hash) + .execute(db) + .await + .with_context(|| "Something went wrong while executing sqlite query")?; + Ok(()) + } + + /// Update password with new string, authenticated with current valid password hash + pub async fn update_password( + &self, + user: S, + password: String, + session_id: i64, + ) -> anyhow::Result<()> + where + S: AsRef, + { + if user.as_ref() != &self.check_session(session_id).await? { + return Err(anyhow::Error::msg( + "Updating password, username doesn't match session username", + )); + }; + + let hash = hash_value(password.clone()).await?; + + sqlx::query("UPDATE users SET (password_hash) = ?") + .bind(&hash) + .execute(&self.db) + .await?; + sqlx::query("UPDATE sessions SET (password_hash) = ? WHERE user = ?") + .bind(&hash) + .bind(user.as_ref()) + .execute(&self.db) + .await?; + + Ok(()) + } + + pub async fn get_user(&self) -> UserInfo { + let user: UserInfo = sqlx::query_as::<_, User>("SELECT * FROM users") + .fetch_one(&self.db) + .await + .unwrap_or_default() + .into(); + + user + } + + pub async fn get_user_info(&self, user: S) -> anyhow::Result + where + S: AsRef, + { + let user: User = sqlx::query_as("SELECT * FROM users WHERE name=?") + .bind(user.as_ref()) + .fetch_one(&self.db) + .await?; + + Ok(user.into()) + } + + pub async fn update_user_name( + &self, + user: S, + name: T, + session_id: i64, + ) -> anyhow::Result + where + S: AsRef, + T: AsRef, + { + self.authorize_user(user.as_ref(), session_id) + .await + .map_err(|_| { + anyhow!( + "An unauthorized user tried to update user {}'s name", + user.as_ref() + ) + })?; + + let info: UserInfo = + sqlx::query_as::<_, User>("UPDATE users SET (name) = ? WHERE name=? RETURNING *") + .bind(name.as_ref()) + .bind(user.as_ref()) + .fetch_one(&self.db) + .await? + .into(); + sqlx::query("UPDATE sessions SET (user) = ? WHERE user=?") + .bind(&info.name) + .bind(user.as_ref()) + .execute(&self.memory) + .await?; + Ok(info) + } + + pub async fn get_homepage(&self) -> String { + let user: User = sqlx::query_as("SELECT * FROM users") + .fetch_one(&self.db) + .await + .unwrap_or_default(); + user.home + } + + pub async fn get_about(&self) -> String { + let user: User = sqlx::query_as("SELECT * FROM users") + .fetch_one(&self.db) + .await + .unwrap_or_default(); + user.about + } + + pub async fn update_user_homepage( + &self, + user: S, + home: T, + session_id: i64, + ) -> anyhow::Result<()> + where + S: AsRef, + T: AsRef, + { + self.authorize_user(user.as_ref(), session_id) + .await + .map_err(|_| { + anyhow!( + "An unauthorized user tried to update user {}'s name", + user.as_ref() + ) + })?; + + sqlx::query("UPDATE users SET (home) = ? WHERE name=? RETURNING *") + .bind(home.as_ref()) + .bind(user.as_ref()) + .execute(&self.db) + .await?; + Ok(()) + } + + pub async fn update_user_about( + &self, + user: S, + about: T, + session_id: i64, + ) -> anyhow::Result + where + S: AsRef, + T: AsRef, + { + self.authorize_user(user.as_ref(), session_id) + .await + .map_err(|_| { + anyhow!( + "An unauthorized user tried to update user {}'s bio", + user.as_ref() + ) + })?; + let info = + sqlx::query_as::<_, User>("UPDATE users SET (about) = ? WHERE name=? RETURNING *") + .bind(about.as_ref()) + .bind(user.as_ref()) + .fetch_one(&self.db) + .await? + .into(); + Ok(info) + } + + // /// Updates user profile pic with asset from database + // pub async fn update_user_profile_pic( + // &self, + // user: S, + // profile_pic_name: T, + // session_id: i64, + // ) -> anyhow::Result + // where + // S: AsRef, + // T: AsRef, + // { + // self.authorize_user(user.as_ref(), session_id) + // .await + // .map_err(|_| { + // anyhow!( + // "An unauthorized user tried to update user {}'s profile pic", + // user.as_ref() + // ) + // })?; + // let info = sqlx::query_as::<_, User>( + // "UPDATE users SET (profile_pic) = ? WHERE name=? RETURNING *", + // ) + // .bind(profile_pic_name.as_ref()) + // .bind(user.as_ref()) + // .fetch_one(&self.db) + // .await? + // .into(); + // Ok(info) + // } + + // /// Replaces user profile pic with new photo, saving it to the database as an asset + // pub async fn replace_user_profile_pic( + // &self, + // user: S, + // profile_pic_data: D, + // session_id: i64, + // ) -> anyhow::Result<(Asset, UserInfo)> + // where + // S: AsRef, + // D: Into>, + // { + // self.authorize_user(user.as_ref(), session_id) + // .await + // .map_err(|_| { + // anyhow!( + // "An unauthorized user tried to replace user {}'s profile pic", + // user.as_ref() + // ) + // })?; + + // let data = profile_pic_data.into(); + + // let user_name = self.get_user_info(user.as_ref()).await?.name; + // let ext = infer::get(&data) + // .ok_or(anyhow!("Couldn't get the profile pic filetype"))? + // .extension(); + // let name = format!("{user_name}-profile.{ext}"); + + // let profile_pic = self.add_asset(name, data).await?; + + // let updated_info: UserInfo = sqlx::query_as::<_, User>( + // "UPDATE users SET (profile_pic) = ? WHERE name=? RETURNING *", + // ) + // .bind(&profile_pic.slug) + // .bind(user.as_ref()) + // .fetch_one(&self.db) + // .await? + // .into(); + + // Ok((profile_pic, updated_info)) + // } + + /// Check if a given session id is valid, and if it is, get the user it's valid for + pub async fn check_session(&self, session_id: I) -> anyhow::Result + where + I: Into, + { + let _ = sqlx::query("DELETE * FROM SESSIONS WHERE date( + &self, + user: S, + password: String, + expires: T, + ) -> anyhow::Result + where + S: AsRef, + T: AsRef, + { + self.verify_password(user.as_ref(), password).await?; + + // ChaCha20Rng panics if it can't get secure entropy, which prolly won't happen but still + // want to catch if it does + let id = std::panic::catch_unwind(|| ChaCha20Rng::from_entropy().next_u64()) + .map_err(|_| anyhow!("Couldn't get secure entropy to generate session id"))? + as i64; + + sqlx::query("INSERT INTO sessions VALUES (?,?, DATE('now', ?))") + .bind(id) + .bind(user.as_ref()) + .bind(expires.as_ref()) + .execute(&self.memory) + .await?; + Ok(id) + } + + // /// Get a session for the server, returning a session id. + // pub async fn get_server_session(&self) -> anyhow::Result { + // // ChaCha20Rng panics if it can't get secure entropy, which prolly won't happen but still + // // want to catch if it does + // let id = std::panic::catch_unwind(|| ChaCha20Rng::from_entropy().next_u64()) + // .map_err(|_| anyhow!("Couldn't get secure entropy to generate session id"))? + // as i64; + + // let user: String = random::().to_string(); + + // sqlx::query("INSERT INTO sessions VALUES (?,?)") + // .bind(id) + // .bind(user) + // .execute(&self.memory) + // .await?; + // Ok(id) + // } + + /// End the user session, call this when logging out the user + pub async fn end_session(&self, id: I) -> anyhow::Result<()> + where + I: Into, + { + sqlx::query("DELETE FROM sessions WHERE id=?") + .bind(id.into()) + .execute(&self.memory) + .await?; + Ok(()) + } + + /// Check if a session is valid for a given user + pub(super) async fn authorize_user(&self, user: S, session_id: I) -> anyhow::Result<()> + where + S: AsRef, + I: Into + Copy, + { + let session_user = self.check_session(session_id).await?; + // this could be a match or if/else statement but i think it's a funny one-liner + (user.as_ref() == session_user).then_some(()).ok_or(anyhow!( + "Session {} is not valid for user {}", + session_id.into(), + user.as_ref() + )) + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::util::tests::*; + + #[sqlx::test] + async fn user_info(pool: SqlitePool) -> anyhow::Result<()> { + let (db, session_id) = get_init_db(pool).await?; + + // Getting info + + let expected_info: UserInfo = UserInfo { + name: "evie".to_string(), + about: "".to_string(), + home: None, + }; + assert_eq!(db.get_user_info("evie").await?, expected_info); + + // Updating info + + let user = db.get_user_info("evie").await?; + + let new_name = "august"; + let new_bio = "loves george"; + + db.update_user_name(&user.name, new_name, session_id) + .await?; + db.update_user_about(new_name, new_bio, session_id).await?; + + // PNG magic numbers + let profile_pic_data = &[0x89, 0x50, 0x4e, 0x47]; + + let profile_pic = db.add_asset("profile.png", profile_pic_data).await?; + + let updated_info = db + .update_user_profile_pic(new_name, &profile_pic.slug, session_id) + .await?; + assert_eq!(updated_info.profile_pic, Some(profile_pic.slug)); + + db.replace_user_profile_pic(new_name, profile_pic_data, session_id) + .await?; + + Ok(()) + } + + #[sqlx::test] + async fn sessions(pool: SqlitePool) -> anyhow::Result<()> { + let (db, _) = get_init_db(pool).await?; + + let username = "evie"; + let password = "hunter2".to_string(); + let id = db.new_session(username, password, "+5 minutes").await?; + + let session_result = db.check_session(id).await; + assert!(session_result.is_ok()); + + let logout = db.end_session(id).await; + assert!(logout.is_ok()); + + Ok(()) + } + + #[sqlx::test] + async fn update_password(pool: SqlitePool) -> anyhow::Result<()> { + let (db, session_id) = get_init_db(pool).await?; + + let update = db + .update_password( + "evie", + "hunter2".to_string(), + "password".to_string(), + session_id, + ) + .await; + + assert!(update.is_ok()); + + Ok(()) + } +} diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..1a53836 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,54 @@ +use anyhow::{anyhow, Context}; +use argon2::{password_hash::SaltString, Argon2}; +use rand::rngs::OsRng; + +use argon2::{PasswordHash, PasswordHasher, PasswordVerifier}; +use sqlx::SqlitePool; +use tokio::task; + +pub(super) async fn verify_hashed_value(value: String, hash: String) -> anyhow::Result<()> { + task::spawn_blocking(move || { + let hash = PasswordHash::new(&hash)?; + Argon2::default() + .verify_password(value.as_bytes(), &hash) + .map_err(|_| anyhow!("Couldn't verify hashed value")) + }) + .await? +} + +pub(super) async fn hash_value(value: String) -> anyhow::Result { + task::spawn_blocking(move || { + let salt = SaltString::generate(&mut OsRng); + let argon2 = Argon2::default(); + Ok(argon2.hash_password(value.as_bytes(), &salt)?.to_string()) + }) + .await? +} + +#[cfg(test)] +pub(super) mod tests { + use crate::*; + use posts::Post; + use sqlx::{ConnectOptions, SqlitePool}; + + pub(crate) async fn get_init_db(pool: SqlitePool) -> anyhow::Result<(BlogDb, i64)> { + let user = "evie"; + let password = "hunter2".to_string(); + let db_url = pool.connect_options().to_url_lossy().to_string(); + let db = BlogDb::new(user, password.clone(), db_url).await?; + let session_id = db.new_session(user, password, "+1 year").await?; + Ok((db, session_id)) + } + + pub(crate) async fn init_and_add_post(pool: SqlitePool) -> anyhow::Result<(BlogDb, i64, Post)> { + let (db, session_id) = get_init_db(pool).await?; + let post = db.add_post("hello", "", "").await?; + let resulting_post = db.get_post(post.id).await?; + + assert_eq!(resulting_post.title, "hello"); + assert_eq!(resulting_post.tags, ""); + assert_eq!(resulting_post.content, ""); + + Ok((db, session_id, post)) + } +}