diff --git a/todored/Cargo.lock b/todored/Cargo.lock index 2877ddb..9a516ae 100644 --- a/todored/Cargo.lock +++ b/todored/Cargo.lock @@ -17,6 +17,32 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + +[[package]] +name = "aho-corasick" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +dependencies = [ + "memchr", +] + +[[package]] +name = "arc-swap" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" + [[package]] name = "async-trait" version = "0.1.72" @@ -43,7 +69,7 @@ dependencies = [ "async-trait", "axum-core", "base64", - "bitflags", + "bitflags 1.3.2", "bytes", "futures-util", "http", @@ -113,6 +139,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630be753d4e58660abd17930c71b647fe46c27ea6b63cc59e1e3851406972e42" + [[package]] name = "block-buffer" version = "0.10.4" @@ -146,6 +178,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "combine" +version = "4.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" +dependencies = [ + "bytes", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", + "tokio-util", +] + [[package]] name = "cpufeatures" version = "0.2.9" @@ -155,6 +201,12 @@ dependencies = [ "libc", ] +[[package]] +name = "crc16" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338089f42c427b86394a5ee60ff321da23a5c89c9d89514c829687b26359fcff" + [[package]] name = "crypto-common" version = "0.1.6" @@ -181,6 +233,40 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "env_logger" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "errno" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "fnv" version = "1.0.7" @@ -196,6 +282,21 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.28" @@ -203,6 +304,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -211,6 +313,34 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" +[[package]] +name = "futures-executor" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" + +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.28" @@ -229,9 +359,13 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-macro", "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", "slab", @@ -264,6 +398,12 @@ version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" +[[package]] +name = "hermit-abi" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" + [[package]] name = "http" version = "0.2.9" @@ -286,6 +426,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" + [[package]] name = "httparse" version = "1.8.0" @@ -298,6 +444,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.27" @@ -331,18 +483,51 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + [[package]] name = "itoa" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +[[package]] +name = "linux-raw-sys" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09fc20d2ca12cb9f044c93e3bd6d32d523e6e2ec3db4f7b2939cd99026ecd3f0" + +[[package]] +name = "lock_api" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.19" @@ -361,6 +546,15 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "micronfig" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf3c1187adebdc5ad8ca94618b966fe8f40e4a3c80fa2f3eaee47891b410156" +dependencies = [ + "lazy_static", +] + [[package]] name = "mime" version = "0.3.17" @@ -387,6 +581,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "object" version = "0.31.1" @@ -402,6 +606,29 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "percent-encoding" version = "2.3.0" @@ -440,12 +667,47 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-version" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e848f61ee4b2010345e65757e427a077213af1cee5d3e6a02e4a151dabca377" +dependencies = [ + "pkg-version-impl", + "proc-macro-hack", +] + +[[package]] +name = "pkg-version-impl" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1564bf5d476bf4a5eac420b88c500454c000dca79cef0a2e4304a1fe34361a3b" +dependencies = [ + "proc-macro-hack", +] + [[package]] name = "ppv-lite86" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "pretty_env_logger" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c" +dependencies = [ + "env_logger", + "log", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.20+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" + [[package]] name = "proc-macro2" version = "1.0.66" @@ -464,6 +726,17 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r2d2" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51de85fb3fb6524929c8a2eb85e6b6d363de4e8c48f9e2c2eac4944abc181c93" +dependencies = [ + "log", + "parking_lot", + "scheduled-thread-pool", +] + [[package]] name = "rand" version = "0.8.5" @@ -494,12 +767,91 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redis" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff5d95dd18a4d76650f0c2607ed8ebdbf63baf9cb934e1c233cd220c694db1d7" +dependencies = [ + "ahash", + "arc-swap", + "async-trait", + "bytes", + "combine", + "crc16", + "futures", + "futures-util", + "itoa", + "percent-encoding", + "pin-project-lite", + "r2d2", + "rand", + "ryu", + "sha1_smol", + "socket2", + "tokio", + "tokio-retry", + "tokio-util", + "url", +] + +[[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "regex" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7b6d6190b7594385f61bd3911cd1be99dfddcfc365a4160cc2ab5bff4aed294" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" + [[package]] name = "rustc-demangle" version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustix" +version = "0.38.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a962918ea88d644592894bc6dc55acc6c0956488adcebbfb6e273506b7fd6e5" +dependencies = [ + "bitflags 2.3.3", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "rustversion" version = "1.0.14" @@ -512,11 +864,40 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" +[[package]] +name = "scheduled-thread-pool" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbc66816425a074528352f5789333ecff06ca41b36b0b0efdfbb29edc391a19" +dependencies = [ + "parking_lot", +] + +[[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.178" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60363bdd39a7be0266a520dab25fdc9241d2f987b08a01e01f0ec6d06a981348" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.178" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28482318d6641454cb273da158647922d1be6b5a2fcc6165cd89ebdd7ed576b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "serde_json" @@ -562,6 +943,12 @@ dependencies = [ "digest", ] +[[package]] +name = "sha1_smol" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" + [[package]] name = "slab" version = "0.4.8" @@ -571,6 +958,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" + [[package]] name = "socket2" version = "0.4.9" @@ -598,6 +991,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.44" @@ -637,8 +1039,16 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" name = "todored" version = "0.1.0" dependencies = [ + "async-trait", "axum", + "log", + "micronfig", + "pkg-version", + "pretty_env_logger", + "redis", "serde", + "tokio", + "tower-http", "uuid", ] @@ -653,11 +1063,35 @@ dependencies = [ "bytes", "libc", "mio", + "num_cpus", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys", ] +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-retry" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f57eb36ecbe0fc510036adff84824dd3c24bb781e21bfa67b69d556aa85214f" +dependencies = [ + "pin-project", + "rand", + "tokio", +] + [[package]] name = "tokio-tungstenite" version = "0.19.0" @@ -670,6 +1104,20 @@ dependencies = [ "tungstenite", ] +[[package]] +name = "tokio-util" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + [[package]] name = "tower" version = "0.4.13" @@ -686,6 +1134,24 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55ae70283aba8d2a8b411c695c437fe25b8b5e44e23e780662002fc72fb47a82" +dependencies = [ + "bitflags 2.3.3", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-range-header", + "pin-project-lite", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.2" @@ -794,7 +1260,8 @@ version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" dependencies = [ - "getrandom", + "serde", + "sha1_smol", ] [[package]] @@ -834,6 +1301,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/todored/Cargo.toml b/todored/Cargo.toml index 42988bb..8c18728 100644 --- a/todored/Cargo.toml +++ b/todored/Cargo.toml @@ -6,6 +6,14 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-trait = "0.1.72" axum = { version = "0.6.19", features = ["ws"] } -serde = "1.0.178" -uuid = { version = "1.4.1", features = ["v4"] } +log = "0.4.19" +micronfig = "0.2.0" +pkg-version = "1.0.0" +pretty_env_logger = "0.5.0" +redis = { version = "0.23.1", features = ["r2d2", "ahash", "cluster", "tokio-comp", "connection-manager"] } +serde = { version = "1.0.178", features = ["derive"] } +tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } +tower-http = { version = "0.4.3", features = ["cors"] } +uuid = { version = "1.4.1", features = ["v5", "serde"] } diff --git a/todored/src/config.rs b/todored/src/config.rs new file mode 100644 index 0000000..575f014 --- /dev/null +++ b/todored/src/config.rs @@ -0,0 +1,4 @@ +use micronfig::required; + +required!(REDIS_CONN, String); +required!(AXUM_HOST, String); // FIXME: Use SocketAddr when possible \ No newline at end of file diff --git a/todored/src/main.rs b/todored/src/main.rs index e7a11a9..9a15ce5 100644 --- a/todored/src/main.rs +++ b/todored/src/main.rs @@ -1,3 +1,42 @@ -fn main() { - println!("Hello, world!"); -} +pub mod task; +pub(crate) mod op; +mod config; +mod routes; +mod utils; + +use std::str::FromStr; +use axum::routing::{get, post}; + + +#[tokio::main] +async fn main() { + pretty_env_logger::init(); + log::debug!("Logging initialized!"); + + let rclient = redis::Client::open(&**config::REDIS_CONN) + .expect("to be able to connect to Redis"); + + let router = axum::Router::new() + .route("/", get(routes::root::version)) + .route("/version", get(routes::root::version)) + .route("/", post(routes::root::healthcheck)) + .route("/healthcheck", post(routes::root::healthcheck)) + .layer(axum::Extension(rclient)) + .layer(tower_http::cors::CorsLayer::new() + .allow_origin( + tower_http::cors::Any + ) + .allow_headers([ + axum::http::header::AUTHORIZATION, + axum::http::header::CONTENT_TYPE, + ]) + .allow_methods( + tower_http::cors::Any + ) + ); + + axum::Server::bind(&std::net::SocketAddr::from_str(&**config::AXUM_HOST).expect("AXUM_HOST to be a valid SocketAddr")) + .serve(router.into_make_service()) + .await + .expect("to be able to run the Axum server"); +} \ No newline at end of file diff --git a/todored/src/op.rs b/todored/src/op.rs new file mode 100644 index 0000000..0c4b4cb --- /dev/null +++ b/todored/src/op.rs @@ -0,0 +1,21 @@ +use serde::{Serialize, Deserialize}; +use uuid::Uuid; +use super::task::Task; + +/// An operation sent from the client to the server. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum ClientOperation { + /// Set the board's title. + Title(String), + /// Create a new [`Task`], or update or delete the task with the given [`Uuid`]. + Task(Option, Option) +} + +/// An operation sent from the server to the clients, and stored on the database. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum ServerOperation { + /// Set the board's title. + Title(String), + /// Create, update, or delete the [`Task`] with the given [`Uuid`]. + Task(Uuid, Option), +} diff --git a/todored/src/routes/mod.rs b/todored/src/routes/mod.rs new file mode 100644 index 0000000..b7ca40a --- /dev/null +++ b/todored/src/routes/mod.rs @@ -0,0 +1 @@ +pub(crate) mod root; \ No newline at end of file diff --git a/todored/src/routes/root.rs b/todored/src/routes/root.rs new file mode 100644 index 0000000..5991dfe --- /dev/null +++ b/todored/src/routes/root.rs @@ -0,0 +1,37 @@ +use axum::{Extension, Json}; +use axum::http::StatusCode; +use crate::utils::{RedisConnectOr500, RedisUnwrapOr500, Result}; + + +const MAJOR: u32 = pkg_version::pkg_version_major!(); +const MINOR: u32 = pkg_version::pkg_version_minor!(); +const PATCH: u32 = pkg_version::pkg_version_patch!(); + +fn compose_version() -> String { + format!("{}.{}.{}", MAJOR, MINOR, PATCH) +} + + +pub async fn version( +) -> Json { + Json(compose_version()) +} + +pub async fn healthcheck( + Extension(rclient): Extension +) -> Result> { + + let mut rconn = rclient.get_connection_or_500().await?; + + log::trace!("Sending PING..."); + let response = redis::cmd("PING") + .query_async::(&mut rconn).await + .unwrap_or_500_and_log()?; + + log::trace!("Sent PING and received: {:?}", response); + + match response == "PONG" { + false => Err(StatusCode::INTERNAL_SERVER_ERROR), + true => Ok(Json(compose_version())), + } +} diff --git a/todored/src/task.rs b/todored/src/task.rs new file mode 100644 index 0000000..840742f --- /dev/null +++ b/todored/src/task.rs @@ -0,0 +1,65 @@ +use serde::{Serialize, Deserialize}; + +/// A task that can be displayed on the board. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Task { + pub text: String, + pub icon: TaskIcon, + pub importance: TaskImportance, + pub priority: TaskPriority, + pub status: TaskStatus, +} + +/// Possible icons for a [`Task`]. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum TaskIcon { + User, + Image, + Envelope, + Star, + Heart, + Comment, + FaceSmile, + File, + Bell, + Bookmark, + Eye, + Hand, + PaperPlane, + Handshake, + Sun, + Clock, + Circle, + Square, + Building, + Flag, + Moon, +} + +/// The importance of a [`Task`] (how much it matters). +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +pub enum TaskImportance { + Highest, + High, + Normal, + Low, + Lowest, +} + +/// The priority of a [`Task`] (how soon it should be completed). +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +pub enum TaskPriority { + Highest, + High, + Normal, + Low, + Lowest, +} + +/// The status a [`Task`] is currently in. +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +pub enum TaskStatus { + Unfinished, + InProgress, + Complete, +} diff --git a/todored/src/utils.rs b/todored/src/utils.rs new file mode 100644 index 0000000..4a1cc18 --- /dev/null +++ b/todored/src/utils.rs @@ -0,0 +1,35 @@ +use async_trait::async_trait; +use axum::http::StatusCode; + + +pub type Result = std::result::Result; + +pub(crate) trait RedisUnwrapOr500 { + fn unwrap_or_500_and_log(self) -> Result; +} + +impl RedisUnwrapOr500 for redis::RedisResult { + fn unwrap_or_500_and_log(self) -> Result { + self + .map_err(|e| { log::error!("{e:#?}"); e }) + .or(Err(StatusCode::INTERNAL_SERVER_ERROR)) + } +} + +#[async_trait] +pub(crate) trait RedisConnectOr500 { + async fn get_connection_or_500(&self) -> Result; +} + +#[async_trait] +impl RedisConnectOr500 for redis::Client { + async fn get_connection_or_500(&self) -> Result { + log::trace!("Connecting to Redis..."); + + let rconn = self.get_async_connection().await + .unwrap_or_500_and_log()?; + + log::trace!("Connection successful!"); + Ok(rconn) + } +}