From a3da8f0d66764918a8caf64f5f99a0e411c2ab82 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Fri, 4 Oct 2024 18:11:44 +0800 Subject: [PATCH] refactor: actix-web -> axum --- Cargo.lock | 557 +++++++------------------ Cargo.toml | 5 +- src/api.rs | 161 ++++--- src/commands/fun/autoreply.rs | 2 +- src/commands/fun/owo.rs | 2 +- src/commands/fun/shiggy.rs | 2 +- src/commands/moderation/ban.rs | 2 +- src/commands/moderation/kick.rs | 2 +- src/commands/moderation/timeout.rs | 2 +- src/commands/useful/dig.rs | 2 +- src/commands/useful/lighthouse.rs | 5 +- src/commands/useful/self_timeout.rs | 2 +- src/commands/useful/suppress_embeds.rs | 2 +- src/commands/useful/translate.rs | 6 +- src/commands/utils/ping.rs | 2 +- src/commands/utils/presence.rs | 2 +- src/commands/utils/say.rs | 2 +- src/commands/utils/sysinfo.rs | 2 +- src/commands/utils/template_channel.rs | 11 +- src/commands/utils/version.rs | 2 +- src/handlers/autoreply.rs | 2 +- src/handlers/code_expansion.rs | 2 +- src/handlers/dm.rs | 2 +- src/handlers/error_handling.rs | 2 +- src/handlers/intelligence.rs | 15 +- src/handlers/log.rs | 2 +- src/handlers/mod.rs | 2 +- src/intelligence.rs | 2 +- src/main.rs | 4 +- src/starboard.rs | 2 +- src/template_channel.rs | 2 +- src/utils/actix.rs | 39 -- src/utils/axum.rs | 32 ++ src/utils/error_handling.rs | 8 +- src/utils/mod.rs | 2 +- src/utils/serenity.rs | 2 +- 36 files changed, 335 insertions(+), 558 deletions(-) delete mode 100644 src/utils/actix.rs create mode 100644 src/utils/axum.rs diff --git a/Cargo.lock b/Cargo.lock index a3b7fa0..f7df34f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,189 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "actix-codec" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f7b0a21988c1bf877cf4759ef5ddaac04c1c9fe808c9142ecb78ba97d97a28a" -dependencies = [ - "bitflags", - "bytes", - "futures-core", - "futures-sink", - "memchr", - "pin-project-lite", - "tokio", - "tokio-util", - "tracing", -] - -[[package]] -name = "actix-http" -version = "3.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d48f96fc3003717aeb9856ca3d02a8c7de502667ad76eeacd830b48d2e91fac4" -dependencies = [ - "actix-codec", - "actix-rt", - "actix-service", - "actix-utils", - "ahash", - "base64 0.22.1", - "bitflags", - "brotli", - "bytes", - "bytestring", - "derive_more", - "encoding_rs", - "flate2", - "futures-core", - "h2", - "http 0.2.12", - "httparse", - "httpdate", - "itoa", - "language-tags", - "local-channel", - "mime", - "percent-encoding", - "pin-project-lite", - "rand", - "sha1", - "smallvec", - "tokio", - "tokio-util", - "tracing", - "zstd", -] - -[[package]] -name = "actix-macros" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" -dependencies = [ - "quote", - "syn 2.0.79", -] - -[[package]] -name = "actix-router" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13d324164c51f63867b57e73ba5936ea151b8a41a1d23d1031eeb9f70d0236f8" -dependencies = [ - "bytestring", - "cfg-if", - "http 0.2.12", - "regex", - "regex-lite", - "serde", - "tracing", -] - -[[package]] -name = "actix-rt" -version = "2.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24eda4e2a6e042aa4e55ac438a2ae052d3b5da0ecf83d7411e1a368946925208" -dependencies = [ - "futures-core", - "tokio", -] - -[[package]] -name = "actix-server" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ca2549781d8dd6d75c40cf6b6051260a2cc2f3c62343d761a969a0640646894" -dependencies = [ - "actix-rt", - "actix-service", - "actix-utils", - "futures-core", - "futures-util", - "mio", - "socket2", - "tokio", - "tracing", -] - -[[package]] -name = "actix-service" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b894941f818cfdc7ccc4b9e60fa7e53b5042a2e8567270f9147d5591893373a" -dependencies = [ - "futures-core", - "paste", - "pin-project-lite", -] - -[[package]] -name = "actix-utils" -version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88a1dcdff1466e3c2488e1cb5c36a71822750ad43839937f85d2f4d9f8b705d8" -dependencies = [ - "local-waker", - "pin-project-lite", -] - -[[package]] -name = "actix-web" -version = "4.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9180d76e5cc7ccbc4d60a506f2c727730b154010262df5b910eb17dbe4b8cb38" -dependencies = [ - "actix-codec", - "actix-http", - "actix-macros", - "actix-router", - "actix-rt", - "actix-server", - "actix-service", - "actix-utils", - "actix-web-codegen", - "ahash", - "bytes", - "bytestring", - "cfg-if", - "cookie", - "derive_more", - "encoding_rs", - "futures-core", - "futures-util", - "impl-more", - "itoa", - "language-tags", - "log", - "mime", - "once_cell", - "pin-project-lite", - "regex", - "regex-lite", - "serde", - "serde_json", - "serde_urlencoded", - "smallvec", - "socket2", - "time", - "url", -] - -[[package]] -name = "actix-web-codegen" -version = "4.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f591380e2e68490b5dfaf1dd1aa0ebe78d84ba7067078512b4ea6e4492d622b8" -dependencies = [ - "actix-router", - "proc-macro2", - "quote", - "syn 2.0.79", -] - [[package]] name = "addr2line" version = "0.21.0" @@ -230,19 +47,6 @@ dependencies = [ "syn 2.0.79", ] -[[package]] -name = "ahash" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" -dependencies = [ - "cfg-if", - "getrandom", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "aho-corasick" version = "1.1.3" @@ -299,9 +103,9 @@ dependencies = [ [[package]] name = "async-compression" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fec134f64e2bc57411226dfc4e52dec859ddfc7e711fc5e07b612584f000e4aa" +checksum = "7e614738943d3f68c628ae3dbce7c3daffb196665f82f8c8ea6b65de73c79429" dependencies = [ "brotli", "futures-core", @@ -327,6 +131,61 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "axum" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "504e3947307ac8326a5437504c517c4b56716c9d98fac0028c2acc7ca47d70ae" +dependencies = [ + "async-trait", + "axum-core", + "bytes", + "futures-util", + "http 1.1.0", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper 1.0.1", + "tokio", + "tower", + "tower-layer", + "tower-service", + "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.1.0", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper 1.0.1", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "backtrace" version = "0.3.71" @@ -384,9 +243,9 @@ dependencies = [ [[package]] name = "brotli" -version = "6.0.0" +version = "7.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74f7971dbd9326d58187408ab83117d8ac1bb9c17b085fdacd1cf2f598719b6b" +checksum = "cc97b8f16f944bba54f0433f07e30be199b6dc2bd25937444bbad560bcea29bd" dependencies = [ "alloc-no-stdlib", "alloc-stdlib", @@ -438,12 +297,10 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.22" +version = "1.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9540e661f81799159abee814118cc139a2004b3a3aa3ea37724a1b66530b90e0" +checksum = "812acba72f0a070b003d3697490d2b55b837230ae7c6c6497f05cc2ddbb8d938" dependencies = [ - "jobserver", - "libc", "shlex", ] @@ -509,23 +366,6 @@ dependencies = [ "tokio-util", ] -[[package]] -name = "convert_case" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" - -[[package]] -name = "cookie" -version = "0.16.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e859cd57d0710d9e06c381b550c06e76992472a8c6d527aecd2fc673dcc231fb" -dependencies = [ - "percent-encoding", - "time", - "version_check", -] - [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -628,7 +468,7 @@ checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" dependencies = [ "cfg-if", "crossbeam-utils", - "hashbrown", + "hashbrown 0.14.5", "lock_api", "once_cell", "parking_lot_core", @@ -692,19 +532,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "derive_more" -version = "0.99.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" -dependencies = [ - "convert_case", - "proc-macro2", - "quote", - "rustc_version", - "syn 2.0.79", -] - [[package]] name = "digest" version = "0.10.7" @@ -727,15 +554,6 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" -[[package]] -name = "encoding_rs" -version = "0.8.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" -dependencies = [ - "cfg-if", -] - [[package]] name = "enum-as-inner" version = "0.6.1" @@ -943,6 +761,12 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "hashbrown" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" + [[package]] name = "heck" version = "0.5.0" @@ -1068,9 +892,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.9.4" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" +checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" [[package]] name = "httpdate" @@ -1105,6 +929,7 @@ dependencies = [ "http 1.1.0", "http-body", "httparse", + "httpdate", "itoa", "pin-project-lite", "smallvec", @@ -1198,12 +1023,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "impl-more" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "206ca75c9c03ba3d4ace2460e57b189f39f43de612c2f85836e65c929701bb2d" - [[package]] name = "indenter" version = "0.3.3" @@ -1212,12 +1031,12 @@ checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" [[package]] name = "indexmap" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" +checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.15.0", "serde", ] @@ -1235,9 +1054,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.10.0" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" +checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" [[package]] name = "itoa" @@ -1245,15 +1064,6 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" -[[package]] -name = "jobserver" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" -dependencies = [ - "libc", -] - [[package]] name = "js-sys" version = "0.3.70" @@ -1263,12 +1073,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "language-tags" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388" - [[package]] name = "lazy_static" version = "1.5.0" @@ -1293,23 +1097,6 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" -[[package]] -name = "local-channel" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6cbc85e69b8df4b8bb8b89ec634e7189099cea8927a276b7384ce5488e53ec8" -dependencies = [ - "futures-core", - "futures-sink", - "local-waker", -] - -[[package]] -name = "local-waker" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d873d7c67ce09b42110d801813efbc9364414e356be9935700d368351657487" - [[package]] name = "lock_api" version = "0.4.12" @@ -1350,6 +1137,12 @@ 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 = "memchr" version = "2.7.4" @@ -1398,17 +1191,10 @@ checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ "hermit-abi", "libc", - "log", "wasi", "windows-sys 0.52.0", ] -[[package]] -name = "mutually_exclusive_features" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94e1e6445d314f972ff7395df2de295fe51b71821694f0b0e1e79c4f12c8577" - [[package]] name = "nanoid" version = "0.4.0" @@ -1565,26 +1351,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "pin-project" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.79", -] - [[package]] name = "pin-project-lite" version = "0.2.14" @@ -1597,12 +1363,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "pkg-config" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" - [[package]] name = "poise" version = "0.6.1" @@ -1812,7 +1572,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "rustls 0.23.13", - "rustls-pemfile 2.1.3", + "rustls-pemfile 2.2.0", "rustls-pki-types", "ryu", "sha1_smol", @@ -1850,9 +1610,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "355ae415ccd3a04315d3f8246e86d67689ea74d88d915576e1589a351062a13b" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" dependencies = [ "bitflags", ] @@ -1889,12 +1649,6 @@ dependencies = [ "regex-syntax 0.8.5", ] -[[package]] -name = "regex-lite" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53a49587ad06b26609c52e423de037e7f57f20d53535d66e08c695f347df952a" - [[package]] name = "regex-syntax" version = "0.6.29" @@ -1909,9 +1663,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" -version = "0.12.7" +version = "0.12.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8f4955649ef5c38cc7f9e8aa41761d48fb9677197daea9984dc54f56aad5e63" +checksum = "f713147fbe92361e52392c73b8c9e48c04c6625bce969ef54dc901e58e042a7b" dependencies = [ "async-compression", "base64 0.22.1", @@ -1934,12 +1688,12 @@ dependencies = [ "pin-project-lite", "quinn", "rustls 0.23.13", - "rustls-pemfile 2.1.3", + "rustls-pemfile 2.2.0", "rustls-pki-types", "serde", "serde_json", "serde_urlencoded", - "sync_wrapper", + "sync_wrapper 1.0.1", "tokio", "tokio-rustls 0.26.0", "tokio-util", @@ -1990,15 +1744,6 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" -[[package]] -name = "rustc_version" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" -dependencies = [ - "semver", -] - [[package]] name = "rustls" version = "0.21.12" @@ -2050,11 +1795,10 @@ dependencies = [ [[package]] name = "rustls-pemfile" -version = "2.1.3" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "196fe16b00e106300d3e45ecfcb764fa292a535d7326a29a5875c579c7417425" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" dependencies = [ - "base64 0.22.1", "rustls-pki-types", ] @@ -2123,12 +1867,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "semver" -version = "1.0.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" - [[package]] name = "serde" version = "1.0.210" @@ -2170,6 +1908,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_spanned" version = "0.6.8" @@ -2367,6 +2115,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "sync_wrapper" version = "1.0.1" @@ -2601,6 +2355,44 @@ dependencies = [ "winnow", ] +[[package]] +name = "tower" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper 0.1.2", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-http" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8437150ab6bbc8c5f0f519e3d5ed4aa883a83dd4cdd3d1b21f9482936046cb97" +dependencies = [ + "bitflags", + "bytes", + "http 1.1.0", + "http-body", + "pin-project-lite", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + [[package]] name = "tower-service" version = "0.3.3" @@ -2619,19 +2411,6 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tracing-actix-web" -version = "0.7.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15bc0cd5f72e837e310f4d978a90abf202a7f7d8ef3272246bae381d0086d3bf" -dependencies = [ - "actix-web", - "mutually_exclusive_features", - "pin-project", - "tracing", - "uuid", -] - [[package]] name = "tracing-attributes" version = "0.1.27" @@ -2782,9 +2561,9 @@ dependencies = [ [[package]] name = "unicode-bidi" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" +checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" [[package]] name = "unicode-ident" @@ -2825,24 +2604,16 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" -[[package]] -name = "uuid" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" -dependencies = [ - "getrandom", -] - [[package]] name = "valfisk" version = "0.1.0" dependencies = [ - "actix-web", + "axum", "bytesize", "chrono", "color-eyre", "dotenvy", + "eyre", "hickory-resolver", "humansize", "humantime", @@ -2863,8 +2634,8 @@ dependencies = [ "sysinfo", "tokio", "toml", + "tower-http", "tracing", - "tracing-actix-web", "tracing-error", "tracing-subscriber", ] @@ -3314,31 +3085,3 @@ name = "zeroize" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" - -[[package]] -name = "zstd" -version = "0.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcf2b778a664581e31e389454a7072dab1647606d44f7feea22cd5abb9c9f3f9" -dependencies = [ - "zstd-safe", -] - -[[package]] -name = "zstd-safe" -version = "7.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54a3ab4db68cea366acc5c897c7b4d4d1b8994a9cd6e6f841f8964566a419059" -dependencies = [ - "zstd-sys", -] - -[[package]] -name = "zstd-sys" -version = "2.0.13+zstd.1.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38ff0f21cfee8f97d94cef41359e0c89aa6113028ab0291aa8ca0038995a95aa" -dependencies = [ - "cc", - "pkg-config", -] diff --git a/Cargo.toml b/Cargo.toml index 8c28437..949135d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,11 +8,12 @@ license = "AGPL-3.0-only" publish = false [dependencies] -actix-web = "4.9.0" +axum = "0.7.7" bytesize = "1.3.0" chrono = "0.4.38" color-eyre = "0.6.3" dotenvy = "0.15.7" +eyre = "0.6.12" hickory-resolver = { version = "0.24.1", features = ["dns-over-https-rustls", "webpki-roots"] } humansize = "2.1.3" humantime = "2.1.0" @@ -33,8 +34,8 @@ serde_json = "1.0.128" sysinfo = "0.31.4" tokio = { version = "1.40.0", features = ["full"] } toml = "0.8.19" +tower-http = { version = "0.6.1", features = ["trace"] } tracing = "0.1.40" -tracing-actix-web = "0.7.13" tracing-error = "0.2.0" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } diff --git a/src/api.rs b/src/api.rs index 66e57cb..701c7e5 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,16 +1,25 @@ use poise::serenity_prelude as serenity; -use actix_web::{get, head, middleware, post, web, App, HttpResponse, HttpServer, Responder}; +use axum::{ + extract::{Form, Path, Request, State}, + http::StatusCode, + middleware, + response::{IntoResponse, Json, Response}, + routing::{get, post, Router}, +}; + use serde_json::json; -use tracing_actix_web::TracingLogger; use once_cell::sync::Lazy; use std::collections::HashMap; use std::sync::{Arc, RwLock}; +use tokio::net::TcpListener; -use crate::utils::actix::ActixError; +use tower_http::trace::TraceLayer; use tracing::info; +use crate::utils::axum::AxumResult; + #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct ValfiskPresenceData { pub status: serenity::OnlineStatus, @@ -32,56 +41,60 @@ impl ValfiskPresenceData { pub static PRESENCE_STORE: Lazy>> = Lazy::new(|| RwLock::new(HashMap::new())); -#[tracing::instrument] -#[get("/")] -async fn route_ping() -> Result { - Ok(HttpResponse::Ok().json(json!({ "ok": true }))) +async fn route_ping() -> impl IntoResponse { + (StatusCode::OK, Json(json!({ "ok": true }))) +} + +async fn route_ping_head() -> impl IntoResponse { + StatusCode::OK } -#[tracing::instrument] -#[head("/")] -async fn route_ping_head() -> Result { - Ok(HttpResponse::Ok().finish()) +async fn route_not_found() -> impl IntoResponse { + (StatusCode::NOT_FOUND, Json(json!({ "error": "Not found" }))) } -#[tracing::instrument] -#[get("/presence/{user}")] -async fn route_get_presence(path: web::Path<(u64,)>) -> Result { - let path = path.into_inner(); - if path.0 == 0 { - return Ok(HttpResponse::BadRequest().json(json!({ "error": "User ID cannot be 0!" }))); +async fn route_presence(Path(user_id): Path) -> AxumResult { + if user_id == 0 { + return Ok(( + StatusCode::BAD_REQUEST, + Json(json!({ "error": "User ID cannot be 0" })), + ) + .into_response()); } - let user_id = serenity::UserId::from(path.0); + let user_id = serenity::UserId::from(user_id); let store = PRESENCE_STORE.read().unwrap(); let presence_data = store.get(&user_id).cloned(); drop(store); presence_data.map_or_else( - || Ok(HttpResponse::NotFound().json(json!({ "error": "User not found!" }))), - |presence_data| Ok(HttpResponse::Ok().json(presence_data)), + || { + Ok(( + StatusCode::NOT_FOUND, + Json(json!({ "error": "User not found" })), + ) + .into_response()) + }, + |presence_data| Ok((StatusCode::OK, Json(presence_data)).into_response()), ) } -#[tracing::instrument] -#[head("/presence/{user}")] -async fn route_get_presence_head(path: web::Path<(u64,)>) -> Result { - let path = path.into_inner(); - if path.0 == 0 { - return Ok(HttpResponse::BadRequest().finish()); +async fn route_presence_head(Path(user_id): Path) -> AxumResult { + if user_id == 0 { + return Ok(StatusCode::BAD_REQUEST); } - let user_id = serenity::UserId::from(path.0); + let user_id = serenity::UserId::from(user_id); let store = PRESENCE_STORE.read().unwrap(); let presence_exists = store.contains_key(&user_id); drop(store); if presence_exists { - Ok(HttpResponse::Ok().finish()) + Ok(StatusCode::OK) } else { - Ok(HttpResponse::NotFound().finish()) + Ok(StatusCode::NOT_FOUND) } } @@ -103,17 +116,18 @@ struct KofiData { timestamp: serenity::Timestamp, } -#[tracing::instrument] -#[post("/ko-fi")] async fn route_kofi_webhook( - app_data: web::Data, - form: web::Form, -) -> Result { + State(state): State>, + form: Form, +) -> AxumResult<(StatusCode, impl IntoResponse)> { let data: KofiData = serde_json::from_str(&form.0.data)?; let verification_token = std::env::var("KOFI_VERIFICATION_TOKEN")?; if data.verification_token != verification_token { - return Ok(HttpResponse::Unauthorized().json(json!({ "error": "unauthorized" }))); + return Ok(( + StatusCode::UNAUTHORIZED, + Json(json!({ "error": "Unauthorized" })), + )); } if data.is_public { @@ -137,14 +151,14 @@ async fn route_kofi_webhook( channel .send_message( - &app_data.into_inner().serenity_http, + &state.serenity_http, serenity::CreateMessage::default().embed(embed), ) .await?; } } - Ok(HttpResponse::Ok().json(json!({ "ok": true }))) + Ok((StatusCode::OK, Json(json!({ "ok": true })))) } #[derive(Debug)] @@ -152,46 +166,61 @@ struct AppState { serenity_http: Arc, } +async fn security_middleware(request: Request, next: middleware::Next) -> Response { + let mut response = next.run(request).await; + + let h = response.headers_mut(); + h.insert( + "content-security-policy", + "default-src 'none'".parse().unwrap(), + ); + h.insert("access-control-allow-origin", "*".parse().unwrap()); + h.insert("cross-origin-opener-policy", "same-origin".parse().unwrap()); + h.insert( + "cross-origin-resource-policy", + "same-origin".parse().unwrap(), + ); + h.insert("origin-agent-cluster", "?1".parse().unwrap()); + h.insert("referrer-policy", "no-referrer".parse().unwrap()); + h.insert("x-content-type-options", "nosniff".parse().unwrap()); + h.insert("x-dns-prefetch-control", "off".parse().unwrap()); + h.insert("x-download-options", "noopen".parse().unwrap()); + h.insert("x-frame-options", "DENY".parse().unwrap()); + h.insert("x-permitted-cross-domain-policies", "none".parse().unwrap()); + h.insert("x-xss-protection", "1; mode=block".parse().unwrap()); + + response +} + #[tracing::instrument(skip(serenity_http))] -pub async fn serve(serenity_http: Arc) -> color_eyre::eyre::Result<()> { +pub async fn serve(serenity_http: Arc) -> eyre::Result<()> { #[cfg(debug_assertions)] let default_host = "127.0.0.1"; #[cfg(not(debug_assertions))] let default_host = "0.0.0.0"; + let host = std::env::var("HOST").unwrap_or_else(|_| default_host.to_owned()); let port = std::env::var("PORT").map_or(Ok(8080), |v| v.parse::())?; - let app_state = web::Data::new(AppState { serenity_http }); + let state = Arc::new(AppState { serenity_http }); info!("Started API server {}", format!("http://{host}:{port}")); - HttpServer::new(move || { - let security_middleware = middleware::DefaultHeaders::new() - .add(("access-control-allow-origin", "*")) - .add(("cross-origin-opener-policy", "same-origin")) - .add(("cross-origin-resource-policy", "same-origin")) - .add(("origin-agent-cluster", "?1")) - .add(("referrer-policy", "no-referrer")) - .add(("x-content-type-options", "nosniff")) - .add(("x-dns-prefetch-control", "off")) - .add(("x-download-options", "noopen")) - .add(("x-frame-options", "SAMEORIGIN")) - .add(("x-permitted-cross-domain-policies", "none")) - .add(("x-xss-protection", "1; mode=block")); - - App::new() - .wrap(TracingLogger::default()) - .wrap(security_middleware) - .app_data(app_state.clone()) - .service(route_ping_head) - .service(route_ping) - .service(route_get_presence_head) - .service(route_get_presence) - .service(route_kofi_webhook) - }) - .bind((host, port))? - .run() - .await?; + let listener = TcpListener::bind((host, port)).await?; + + let app = Router::new() + .route("/", get(route_ping).head(route_ping_head)) + .route( + "/presence/:user", + get(route_presence).head(route_presence_head), + ) + .route("/ko-fi", post(route_kofi_webhook)) + .fallback(route_not_found) + .layer(middleware::from_fn(security_middleware)) + .layer(TraceLayer::new_for_http()) + .with_state(state); + + axum::serve(listener, app).await?; Ok(()) } diff --git a/src/commands/fun/autoreply.rs b/src/commands/fun/autoreply.rs index b6091bd..4646793 100644 --- a/src/commands/fun/autoreply.rs +++ b/src/commands/fun/autoreply.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::{eyre, Result}; +use eyre::{eyre, Result}; use poise::{ serenity_prelude::{CreateEmbed, Timestamp}, CreateReply, diff --git a/src/commands/fun/owo.rs b/src/commands/fun/owo.rs index 2437b43..5ddda03 100644 --- a/src/commands/fun/owo.rs +++ b/src/commands/fun/owo.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use crate::Context; diff --git a/src/commands/fun/shiggy.rs b/src/commands/fun/shiggy.rs index e5255ca..a7fb89b 100644 --- a/src/commands/fun/shiggy.rs +++ b/src/commands/fun/shiggy.rs @@ -1,7 +1,7 @@ use poise::{serenity_prelude as serenity, CreateReply}; use crate::{utils::error_handling::ValfiskError, Context}; -use color_eyre::eyre::{Report, Result}; +use eyre::{Report, Result}; #[derive(serde::Deserialize)] struct SafebooruResponse { diff --git a/src/commands/moderation/ban.rs b/src/commands/moderation/ban.rs index 2b81845..066c00a 100644 --- a/src/commands/moderation/ban.rs +++ b/src/commands/moderation/ban.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::serenity_prelude as serenity; use super::LOGS_CHANNEL; diff --git a/src/commands/moderation/kick.rs b/src/commands/moderation/kick.rs index 54dbec2..0827a51 100644 --- a/src/commands/moderation/kick.rs +++ b/src/commands/moderation/kick.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::serenity_prelude as serenity; use super::LOGS_CHANNEL; diff --git a/src/commands/moderation/timeout.rs b/src/commands/moderation/timeout.rs index 6e6136b..e31b8dd 100644 --- a/src/commands/moderation/timeout.rs +++ b/src/commands/moderation/timeout.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::serenity_prelude as serenity; use super::LOGS_CHANNEL; diff --git a/src/commands/useful/dig.rs b/src/commands/useful/dig.rs index b19a8cc..3c26e97 100644 --- a/src/commands/useful/dig.rs +++ b/src/commands/useful/dig.rs @@ -11,7 +11,7 @@ use poise::{ ChoiceParameter, CreateReply, }; -use color_eyre::eyre::Result; +use eyre::Result; use once_cell::sync::Lazy; pub static RESOLVER: Lazy = Lazy::new(|| { diff --git a/src/commands/useful/lighthouse.rs b/src/commands/useful/lighthouse.rs index 590c695..ade5487 100644 --- a/src/commands/useful/lighthouse.rs +++ b/src/commands/useful/lighthouse.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use std::{collections::HashMap, env, time::Duration}; use poise::{serenity_prelude as serenity, CreateReply}; @@ -66,7 +66,8 @@ pub async fn lighthouse( .get(api_url) .timeout(Duration::from_secs(60)) .send() - .await?; + .await? + .error_for_status()?; let data: PagespeedResponse = resp.json().await?; diff --git a/src/commands/useful/self_timeout.rs b/src/commands/useful/self_timeout.rs index 197e7f2..f1b039e 100644 --- a/src/commands/useful/self_timeout.rs +++ b/src/commands/useful/self_timeout.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::{eyre, Result}; +use eyre::{eyre, Result}; use poise::serenity_prelude as serenity; use crate::Context; diff --git a/src/commands/useful/suppress_embeds.rs b/src/commands/useful/suppress_embeds.rs index 80ec38e..97569c2 100644 --- a/src/commands/useful/suppress_embeds.rs +++ b/src/commands/useful/suppress_embeds.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::serenity_prelude as serenity; use crate::Context; diff --git a/src/commands/useful/translate.rs b/src/commands/useful/translate.rs index 1ad6d51..44db6a0 100644 --- a/src/commands/useful/translate.rs +++ b/src/commands/useful/translate.rs @@ -1,7 +1,7 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::{serenity_prelude as serenity, CreateReply}; -use crate::Context; +use crate::{reqwest_client::HTTP, Context}; #[derive(serde::Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -49,7 +49,7 @@ pub async fn translate(ctx: Context<'_>, message: serenity::Message) -> Result<( .append_pair("source", "input") .append_pair("q", &message.content); - let resp = crate::reqwest_client::HTTP.get(api_url).send().await?; + let resp = HTTP.get(api_url).send().await?.error_for_status()?; let data: GoogleTranslateResponse = resp.json().await?; let translation = data diff --git a/src/commands/utils/ping.rs b/src/commands/utils/ping.rs index dcf0fe6..9660b33 100644 --- a/src/commands/utils/ping.rs +++ b/src/commands/utils/ping.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use crate::Context; diff --git a/src/commands/utils/presence.rs b/src/commands/utils/presence.rs index 2bd2364..32e9c6c 100644 --- a/src/commands/utils/presence.rs +++ b/src/commands/utils/presence.rs @@ -1,7 +1,7 @@ use crate::{storage::presence::PresenceChoice, Context}; use poise::{serenity_prelude as serenity, CreateReply}; -use color_eyre::eyre::Result; +use eyre::Result; use tracing::info; /// Modify the Discord presence shown by the bot diff --git a/src/commands/utils/say.rs b/src/commands/utils/say.rs index 6aeee35..721ca51 100644 --- a/src/commands/utils/say.rs +++ b/src/commands/utils/say.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::serenity_prelude as serenity; use crate::Context; diff --git a/src/commands/utils/sysinfo.rs b/src/commands/utils/sysinfo.rs index ac6b781..d0fed13 100644 --- a/src/commands/utils/sysinfo.rs +++ b/src/commands/utils/sysinfo.rs @@ -2,7 +2,7 @@ use poise::{serenity_prelude::CreateEmbed, CreateReply}; use sysinfo::{CpuRefreshKind, MemoryRefreshKind, RefreshKind, System}; use crate::Context; -use color_eyre::eyre::Result; +use eyre::Result; /// Get system information for the bot host #[poise::command(slash_command, guild_only)] diff --git a/src/commands/utils/template_channel.rs b/src/commands/utils/template_channel.rs index 520bb87..5f035eb 100644 --- a/src/commands/utils/template_channel.rs +++ b/src/commands/utils/template_channel.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::{ serenity_prelude::{futures::StreamExt, ChannelId, CreateEmbed}, CreateReply, @@ -24,7 +24,14 @@ pub async fn template_channel( let clear = clear.unwrap_or(true); ctx.defer_ephemeral().await?; - let source = HTTP.get(&url).send().await?.text().await?; + let source = HTTP + .get(&url) + .send() + .await? + .error_for_status()? + .text() + .await?; + let data = TemplateChannelConfig::parse(&source)?; let messages = data.to_messages(); diff --git a/src/commands/utils/version.rs b/src/commands/utils/version.rs index caa44b5..667beab 100644 --- a/src/commands/utils/version.rs +++ b/src/commands/utils/version.rs @@ -1,6 +1,6 @@ use std::env::consts::{ARCH, OS}; -use color_eyre::eyre::Result; +use eyre::Result; use poise::{serenity_prelude::CreateEmbed, CreateReply}; use crate::Context; diff --git a/src/handlers/autoreply.rs b/src/handlers/autoreply.rs index dd89c39..549d5f6 100644 --- a/src/handlers/autoreply.rs +++ b/src/handlers/autoreply.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::serenity_prelude as serenity; use rand::seq::SliceRandom as _; diff --git a/src/handlers/code_expansion.rs b/src/handlers/code_expansion.rs index f5dcfb8..3389586 100644 --- a/src/handlers/code_expansion.rs +++ b/src/handlers/code_expansion.rs @@ -6,7 +6,7 @@ use crate::{ }; use regex::Regex; -use color_eyre::eyre::Result; +use eyre::Result; use once_cell::sync::Lazy; use tracing::debug; diff --git a/src/handlers/dm.rs b/src/handlers/dm.rs index 740a82e..2298113 100644 --- a/src/handlers/dm.rs +++ b/src/handlers/dm.rs @@ -1,6 +1,6 @@ use poise::serenity_prelude as serenity; -use color_eyre::eyre::Result; +use eyre::Result; use humansize::{format_size, FormatSizeOptions}; use once_cell::sync::Lazy; diff --git a/src/handlers/error_handling.rs b/src/handlers/error_handling.rs index 6bfe7fb..102bb21 100644 --- a/src/handlers/error_handling.rs +++ b/src/handlers/error_handling.rs @@ -11,7 +11,7 @@ use crate::{ }; #[tracing::instrument(skip(err))] -pub async fn handle_error(err: FrameworkError<'_, Data, color_eyre::eyre::Report>) { +pub async fn handle_error(err: FrameworkError<'_, Data, eyre::Report>) { match err { FrameworkError::EventHandler { error, diff --git a/src/handlers/intelligence.rs b/src/handlers/intelligence.rs index f82c1af..083a6a5 100644 --- a/src/handlers/intelligence.rs +++ b/src/handlers/intelligence.rs @@ -1,6 +1,6 @@ use poise::serenity_prelude as serenity; -use color_eyre::eyre::Result; +use eyre::Result; use once_cell::sync::Lazy; use std::time::Duration; use tokio::{task, time}; @@ -52,12 +52,15 @@ pub async fn handle(message: &serenity::Message, ctx: &serenity::Context) -> Res let channel = message.channel_id; async move { - let mut interval = time::interval(Duration::from_secs(10)); + let _ = time::timeout(Duration::from_secs(60), async move { + let mut interval = time::interval(Duration::from_secs(10)); - loop { - interval.tick().await; - let _ = http.broadcast_typing(channel).await; - } + loop { + interval.tick().await; + let _ = http.broadcast_typing(channel).await; + } + }) + .await; } }); diff --git a/src/handlers/log.rs b/src/handlers/log.rs index 562922e..4167ce7 100644 --- a/src/handlers/log.rs +++ b/src/handlers/log.rs @@ -1,7 +1,7 @@ use humansize::{format_size, FormatSizeOptions}; use poise::serenity_prelude::{self as serenity}; -use color_eyre::eyre::Result; +use eyre::Result; use once_cell::sync::Lazy; use crate::{storage::log::MessageLog, Data}; diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index c9e4453..8c88001 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::serenity_prelude as serenity; mod autoreply; diff --git a/src/intelligence.rs b/src/intelligence.rs index 1fa2156..5ef331d 100644 --- a/src/intelligence.rs +++ b/src/intelligence.rs @@ -1,6 +1,6 @@ use std::env; -use color_eyre::eyre::{eyre, Result}; +use eyre::{eyre, Result}; use serde::{Deserialize, Serialize}; use crate::reqwest_client::HTTP; diff --git a/src/main.rs b/src/main.rs index af5bfc1..bf384c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use color_eyre::eyre::{Report, Result, WrapErr as _}; +use eyre::{Report, Result, WrapErr as _}; use tracing::{info, warn}; use poise::{serenity_prelude as serenity, Framework, FrameworkContext, FrameworkOptions}; @@ -262,7 +262,7 @@ async fn main() -> Result<()> { let client_http_2 = client.http.clone(); tokio::select! { - result = client.start() => { result.map_err(color_eyre::eyre::Error::from) }, + result = client.start() => { result.map_err(eyre::Error::from) }, result = api::serve(client_http_2) => { result }, _ = tokio::signal::ctrl_c() => { warn!("Interrupted with SIGINT, exiting"); diff --git a/src/starboard.rs b/src/starboard.rs index 1436c4a..93039c2 100644 --- a/src/starboard.rs +++ b/src/starboard.rs @@ -2,7 +2,7 @@ use std::env; use poise::serenity_prelude as serenity; -use color_eyre::eyre::{OptionExt, Result}; +use eyre::{OptionExt, Result}; use tracing::debug; fn channel_from_env(key: &str) -> Option { diff --git a/src/template_channel.rs b/src/template_channel.rs index 79a4aa9..99325f4 100644 --- a/src/template_channel.rs +++ b/src/template_channel.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use indexmap::IndexMap; use poise::serenity_prelude::{CreateEmbed, CreateMessage}; diff --git a/src/utils/actix.rs b/src/utils/actix.rs deleted file mode 100644 index 703e868..0000000 --- a/src/utils/actix.rs +++ /dev/null @@ -1,39 +0,0 @@ -use actix_web::{body::BoxBody, http::StatusCode, HttpResponse}; -use serde_json::json; -use tracing::error; - -pub struct ActixError(color_eyre::eyre::Error); - -impl std::fmt::Debug for ActixError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } -} - -impl std::fmt::Display for ActixError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } -} - -impl actix_web::ResponseError for ActixError { - fn error_response(&self) -> HttpResponse { - error!("{}", self.0); - - HttpResponse::InternalServerError() - .json(json!({ "error": "An internal server error occurred!" })) - } - - fn status_code(&self) -> StatusCode { - StatusCode::INTERNAL_SERVER_ERROR - } -} - -impl From for ActixError -where - E: Into, -{ - fn from(err: E) -> Self { - Self(err.into()) - } -} diff --git a/src/utils/axum.rs b/src/utils/axum.rs new file mode 100644 index 0000000..f9471e6 --- /dev/null +++ b/src/utils/axum.rs @@ -0,0 +1,32 @@ +use axum::{ + http::StatusCode, + response::{IntoResponse, Response}, + Json, +}; +use serde_json::json; +use tracing::error; + +pub struct AxumError(eyre::Report); + +impl IntoResponse for AxumError { + fn into_response(self) -> Response { + error!("{}", self.0); + + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({ "error": "An internal server error occurred!" })), + ) + .into_response() + } +} + +impl From for AxumError +where + E: Into, +{ + fn from(err: E) -> Self { + Self(err.into()) + } +} + +pub type AxumResult = Result; diff --git a/src/utils/error_handling.rs b/src/utils/error_handling.rs index 47d81a2..83dc75c 100644 --- a/src/utils/error_handling.rs +++ b/src/utils/error_handling.rs @@ -10,10 +10,10 @@ use poise::{ use crate::Context; use tracing::error; -/// A wrapper type that encapsulates errors ([`color_eyre::eyre::Error`]) or panic strings ([`Option`]). +/// A wrapper type that encapsulates errors ([`eyre::Error`]) or panic strings ([`Option`]). pub enum ErrorOrPanic<'a> { - /// A reference to an error, [`color_eyre::eyre::Error`] - Error(&'a color_eyre::eyre::Error), + /// A reference to an error, [`eyre::Error`] + Error(&'a eyre::Error), /// A reference to a panic string, [`Option`] Panic(&'a Option), } @@ -48,7 +48,7 @@ pub struct ValfiskError<'a> { impl ValfiskError<'_> { /// Create a new [`ValfiskError`] from an error and Poise context. #[must_use] - pub fn error<'a>(error: &'a color_eyre::eyre::Error, ctx: &'a Context) -> ValfiskError<'a> { + pub fn error<'a>(error: &'a eyre::Error, ctx: &'a Context) -> ValfiskError<'a> { ValfiskError { error_or_panic: ErrorOrPanic::Error(error), ctx, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 0cdda08..b32e36e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,4 +1,4 @@ -pub mod actix; +pub mod axum; pub mod error_handling; pub mod serenity; diff --git a/src/utils/serenity.rs b/src/utils/serenity.rs index f5639e7..f8821f8 100644 --- a/src/utils/serenity.rs +++ b/src/utils/serenity.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use eyre::Result; use poise::serenity_prelude as serenity; #[tracing::instrument(skip(ctx))]