1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

Database interactions/redis (#524)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Christopher Gubbin 2022-02-15 00:35:00 +00:00 committed by GitHub
parent 7ea3f7f54a
commit 5c1e25fe52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 158 additions and 353 deletions

368
Cargo.lock generated
View File

@ -36,7 +36,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1be241f88f3b1e7e9a3fbe3b5a8a0f6915b5a1d7ee0d9a248d3376d01068cc60"
dependencies = [
"actix-rt 1.1.1",
"actix_derive",
"actix_derive 0.5.0",
"bitflags",
"bytes 0.5.6",
"crossbeam-channel 0.4.4",
@ -45,7 +45,7 @@ dependencies = [
"futures-util",
"log",
"once_cell",
"parking_lot 0.11.2",
"parking_lot",
"pin-project 0.4.29",
"smallvec",
"tokio 0.2.25",
@ -61,6 +61,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3720d0064a0ce5c0de7bd93bdb0a6caebab2a9b5668746145d7b3b0c5da02914"
dependencies = [
"actix-rt 2.6.0",
"actix_derive 0.6.0",
"bitflags",
"bytes 1.1.0",
"crossbeam-channel 0.5.2",
@ -70,7 +71,7 @@ dependencies = [
"futures-util",
"log",
"once_cell",
"parking_lot 0.11.2",
"parking_lot",
"pin-project-lite 0.2.8",
"smallvec",
"tokio 1.16.1",
@ -248,7 +249,6 @@ version = "2.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2be6b66b62a794a8e6d366ac9415bb7d475ffd1e9f4671f38c1d8a8a5df950b3"
dependencies = [
"actix 0.10.0",
"actix-codec 0.3.0",
"actix-connect",
"actix-rt 1.1.1",
@ -423,40 +423,14 @@ dependencies = [
[[package]]
name = "actix-redis"
version = "0.9.2"
version = "0.10.0-beta.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac08092eec10bd695a8bcd64d4dbc29fa4f3495cbd669be41d3355dd51213882"
dependencies = [
"actix 0.10.0",
"actix-http 2.2.2",
"actix-rt 1.1.1",
"actix-service 1.0.6",
"actix-session 0.4.1",
"actix-utils 2.0.0",
"actix-web 3.3.3",
"backoff",
"derive_more",
"futures-util",
"log",
"rand 0.7.3",
"redis-async 0.6.6",
"serde 1.0.136",
"serde_json",
"time 0.2.27",
"tokio 0.2.25",
"tokio-util 0.3.1",
]
[[package]]
name = "actix-redis"
version = "0.10.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54f9df32d7d0176d2acca0bf4b62d94e0a7ac341d5f8124f7f5903f4d57ff462"
checksum = "184451f4945b4b9bf47417014658781ffad2d415bf5a886809cba9303a70b420"
dependencies = [
"actix 0.12.0",
"actix-rt 2.6.0",
"actix-service 2.0.2",
"actix-session 0.5.0-beta.7",
"actix-session",
"actix-tls 3.0.2",
"actix-web 4.0.0-rc.3",
"backoff",
@ -465,7 +439,7 @@ dependencies = [
"log",
"rand 0.8.4",
"redis",
"redis-async 0.8.1",
"redis-async",
"serde 1.0.136",
"serde_json",
"time 0.3.7",
@ -587,24 +561,9 @@ dependencies = [
[[package]]
name = "actix-session"
version = "0.4.1"
version = "0.5.0-beta.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "559b815f2b3ad84f8a17256069d7df16c3ee8069635c86758729521d62ca891d"
dependencies = [
"actix-service 1.0.6",
"actix-web 3.3.3",
"derive_more",
"futures-util",
"serde 1.0.136",
"serde_json",
"time 0.2.27",
]
[[package]]
name = "actix-session"
version = "0.5.0-beta.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8984fa6256873b5075143ef662898ed6699bcd0ca094d8a6e40048c12af8d591"
checksum = "c5562e924654d706f9fe3fe5a1644bb8ee311b58a1aa5d1451007efa35890975"
dependencies = [
"actix-service 2.0.2",
"actix-utils 3.0.0",
@ -665,7 +624,7 @@ dependencies = [
"lazy_static",
"log",
"num_cpus",
"parking_lot 0.11.2",
"parking_lot",
"threadpool",
]
@ -873,16 +832,28 @@ dependencies = [
"syn",
]
[[package]]
name = "actix_derive"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d44b8fee1ced9671ba043476deddef739dd0959bf77030b26b738cc591737a7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "actix_redis"
version = "1.0.0"
dependencies = [
"actix 0.10.0",
"actix-redis 0.9.2",
"actix-web 3.3.3",
"env_logger 0.8.4",
"futures",
"redis-async 0.6.6",
"actix 0.12.0",
"actix-redis",
"actix-web 4.0.0-rc.3",
"env_logger 0.9.0",
"futures-util",
"log",
"redis-async",
"serde 1.0.136",
]
@ -901,15 +872,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aead"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fc95d1bdb8e6666b2b217308eeeb09f2d6728d104be3e31916cc74d15420331"
dependencies = [
"generic-array 0.14.5",
]
[[package]]
name = "aead"
version = "0.4.3"
@ -919,17 +881,6 @@ dependencies = [
"generic-array 0.14.5",
]
[[package]]
name = "aes"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "884391ef1066acaa41e766ba8f596341b96e93ce34f9a43e7d24bf0a0eaf0561"
dependencies = [
"aes-soft",
"aesni",
"cipher 0.2.5",
]
[[package]]
name = "aes"
version = "0.7.5"
@ -937,59 +888,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8"
dependencies = [
"cfg-if 1.0.0",
"cipher 0.3.0",
"cipher",
"cpufeatures",
"opaque-debug 0.3.0",
]
[[package]]
name = "aes-gcm"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5278b5fabbb9bd46e24aa69b2fdea62c99088e0a950a9be40e3e0101298f88da"
dependencies = [
"aead 0.3.2",
"aes 0.6.0",
"cipher 0.2.5",
"ctr 0.6.0",
"ghash 0.3.1",
"subtle",
]
[[package]]
name = "aes-gcm"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df5f85a83a7d8b0442b6aa7b504b8212c1733da07b98aae43d4bc21b2cb3cdf6"
dependencies = [
"aead 0.4.3",
"aes 0.7.5",
"cipher 0.3.0",
"ctr 0.8.0",
"ghash 0.4.4",
"aead",
"aes",
"cipher",
"ctr",
"ghash",
"subtle",
]
[[package]]
name = "aes-soft"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be14c7498ea50828a38d0e24a765ed2effe92a705885b57d029cd67d45744072"
dependencies = [
"cipher 0.2.5",
"opaque-debug 0.3.0",
]
[[package]]
name = "aesni"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea2e11f5e94c2f7d386164cc2aa1f97823fed6f259e486940a71c174dd01b0ce"
dependencies = [
"cipher 0.2.5",
"opaque-debug 0.3.0",
]
[[package]]
name = "ahash"
version = "0.7.6"
@ -1455,7 +1372,7 @@ name = "basics"
version = "1.0.0"
dependencies = [
"actix-files 0.6.0-beta.16",
"actix-session 0.5.0-beta.7",
"actix-session",
"actix-web 4.0.0-rc.3",
"async-stream",
"env_logger 0.9.0",
@ -1701,7 +1618,7 @@ checksum = "e3be2866a3a3174b9d31a11d9f9ef219ab84a444d107981c51bf79d7be221adc"
dependencies = [
"async-trait",
"lazy_static",
"parking_lot 0.11.2",
"parking_lot",
"regex",
"rhai",
"ritelinked",
@ -1773,15 +1690,6 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fff857943da45f546682664a79488be82e69e43c1a7a2307679ab9afb3a66d2e"
[[package]]
name = "cipher"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f8e7987cbd042a63249497f41aed09f8e65add917ea6566effbc56578d6801"
dependencies = [
"generic-array 0.14.5",
]
[[package]]
name = "cipher"
version = "0.3.0"
@ -1908,13 +1816,7 @@ version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03a5d7b21829bc7b4bf4754a978a241ae54ea55a40f92bb20216e54096f4b951"
dependencies = [
"aes-gcm 0.8.0",
"base64 0.13.0",
"hkdf 0.10.0",
"hmac 0.10.1",
"percent-encoding",
"rand 0.8.4",
"sha2 0.9.9",
"time 0.2.27",
"version_check 0.9.4",
]
@ -1925,9 +1827,9 @@ version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94d4706de1b0fa5b132270cddffa8585166037822e260a944fe161acd137ca05"
dependencies = [
"aes-gcm 0.9.4",
"aes-gcm",
"base64 0.13.0",
"hkdf 0.12.2",
"hkdf",
"hmac 0.12.0",
"percent-encoding",
"rand 0.8.4",
@ -1951,7 +1853,7 @@ dependencies = [
name = "cookie-session"
version = "1.0.0"
dependencies = [
"actix-session 0.5.0-beta.7",
"actix-session",
"actix-web 4.0.0-rc.3",
"env_logger 0.9.0",
"log",
@ -2004,12 +1906,6 @@ dependencies = [
"libc",
]
[[package]]
name = "cpuid-bool"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb25d077389e53838a8158c8e99174c5a9d902dee4904320db714f3c653ffba"
[[package]]
name = "crc"
version = "2.1.0"
@ -2094,16 +1990,6 @@ dependencies = [
"generic-array 0.14.5",
]
[[package]]
name = "crypto-mac"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bff07008ec701e8028e2ceb8f83f0e4274ee62bd2dbdc4fefff2e9a91824081a"
dependencies = [
"generic-array 0.14.5",
"subtle",
]
[[package]]
name = "crypto-mac"
version = "0.11.1"
@ -2114,22 +2000,13 @@ dependencies = [
"subtle",
]
[[package]]
name = "ctr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb4a30d54f7443bf3d6191dcd486aca19e67cb3c49fa7a06a319966346707e7f"
dependencies = [
"cipher 0.2.5",
]
[[package]]
name = "ctr"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "049bb91fb4aaf0e3c7efa6cd5ef877dbbbd15b39dad06d9948de4ec8a75761ea"
dependencies = [
"cipher 0.3.0",
"cipher",
]
[[package]]
@ -2666,7 +2543,7 @@ checksum = "62007592ac46aa7c2b6416f7deb9a8a8f63a01e0f1d6e1787d5630170db2b63e"
dependencies = [
"futures-core",
"lock_api",
"parking_lot 0.11.2",
"parking_lot",
]
[[package]]
@ -2766,16 +2643,6 @@ dependencies = [
"wasi 0.10.2+wasi-snapshot-preview1",
]
[[package]]
name = "ghash"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97304e4cd182c3846f7575ced3890c53012ce534ad9114046b0a9e00bb30a375"
dependencies = [
"opaque-debug 0.3.0",
"polyval 0.4.5",
]
[[package]]
name = "ghash"
version = "0.4.4"
@ -2783,7 +2650,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1583cc1656d7839fd3732b80cf4f38850336cdb9b8ded1cd399ca62958de3c99"
dependencies = [
"opaque-debug 0.3.0",
"polyval 0.5.3",
"polyval",
]
[[package]]
@ -2936,16 +2803,6 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hkdf"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51ab2f639c231793c5f6114bdb9bbe50a7dbbfcd7c7c6bd8475dec2d991e964f"
dependencies = [
"digest 0.9.0",
"hmac 0.10.1",
]
[[package]]
name = "hkdf"
version = "0.12.2"
@ -2955,23 +2812,13 @@ dependencies = [
"hmac 0.12.0",
]
[[package]]
name = "hmac"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1441c6b1e930e2817404b5046f1f989899143a12bf92de603b69f4e0aee1e15"
dependencies = [
"crypto-mac 0.10.1",
"digest 0.9.0",
]
[[package]]
name = "hmac"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b"
dependencies = [
"crypto-mac 0.11.1",
"crypto-mac",
"digest 0.9.0",
]
@ -4219,17 +4066,7 @@ checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
dependencies = [
"instant",
"lock_api",
"parking_lot_core 0.8.5",
]
[[package]]
name = "parking_lot"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58"
dependencies = [
"lock_api",
"parking_lot_core 0.9.1",
"parking_lot_core",
]
[[package]]
@ -4246,19 +4083,6 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "parking_lot_core"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28141e0cc4143da2443301914478dc976a61ffdb3f043058310c70df2fed8954"
dependencies = [
"cfg-if 1.0.0",
"libc",
"redox_syscall",
"smallvec",
"windows-sys",
]
[[package]]
name = "parse-zoneinfo"
version = "0.3.0"
@ -4280,7 +4104,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d95f5254224e617595d2cc3cc73ff0a5eaf2637519e25f03388154e9378b6ffa"
dependencies = [
"crypto-mac 0.11.1",
"crypto-mac",
]
[[package]]
@ -4473,17 +4297,6 @@ version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe"
[[package]]
name = "polyval"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eebcc4aa140b9abd2bc40d9c3f7ccec842679cd79045ac3a7ac698c1a064b7cd"
dependencies = [
"cpuid-bool",
"opaque-debug 0.3.0",
"universal-hash",
]
[[package]]
name = "polyval"
version = "0.5.3"
@ -4688,7 +4501,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "545c5bc2b880973c9c10e4067418407a0ccaa3091781d1671d46eb35107cb26f"
dependencies = [
"log",
"parking_lot 0.11.2",
"parking_lot",
"scheduled-thread-pool",
]
@ -4825,21 +4638,6 @@ dependencies = [
"url",
]
[[package]]
name = "redis-async"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffc6ce852b893ec227e1671953c153ac92a27a6f458b9623309dcbfa18edb057"
dependencies = [
"bytes 0.5.6",
"futures-channel",
"futures-sink",
"futures-util",
"log",
"tokio 0.2.25",
"tokio-util 0.3.1",
]
[[package]]
name = "redis-async"
version = "0.8.1"
@ -4859,8 +4657,8 @@ dependencies = [
name = "redis_session"
version = "1.0.0"
dependencies = [
"actix-redis 0.10.0-beta.5",
"actix-session 0.5.0-beta.7",
"actix-redis",
"actix-session",
"actix-test",
"actix-web 4.0.0-rc.3",
"env_logger 0.9.0",
@ -5321,7 +5119,7 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f74fd1204073fa02d5d5d68bec8021be4c38690b61264b2fdb48083d0e7d7"
dependencies = [
"parking_lot 0.11.2",
"parking_lot",
]
[[package]]
@ -5492,13 +5290,10 @@ dependencies = [
name = "server-sent-events"
version = "1.0.0"
dependencies = [
"actix-web 4.0.0-rc.3",
"env_logger 0.9.0",
"futures-util",
"log",
"parking_lot 0.12.0",
"tokio 1.16.1",
"tokio-stream",
"actix-web 3.3.3",
"env_logger 0.8.4",
"futures",
"tokio 0.2.25",
]
[[package]]
@ -5772,7 +5567,7 @@ dependencies = [
"log",
"memchr",
"once_cell",
"parking_lot 0.11.2",
"parking_lot",
"percent-encoding",
"rustls 0.19.1",
"serde 1.0.136",
@ -5916,7 +5711,7 @@ checksum = "923f0f39b6267d37d23ce71ae7235602134b250ace715dd2c90421998ddac0c6"
dependencies = [
"lazy_static",
"new_debug_unreachable",
"parking_lot 0.11.2",
"parking_lot",
"phf_shared 0.8.0",
"precomputed-hash",
"serde 1.0.136",
@ -6235,7 +6030,7 @@ name = "todo"
version = "1.0.0"
dependencies = [
"actix-files 0.6.0-beta.16",
"actix-session 0.5.0-beta.7",
"actix-session",
"actix-web 4.0.0-rc.3",
"dotenv",
"env_logger 0.9.0",
@ -6281,7 +6076,7 @@ dependencies = [
"mio 0.7.14",
"num_cpus",
"once_cell",
"parking_lot 0.11.2",
"parking_lot",
"pin-project-lite 0.2.8",
"signal-hook-registry",
"tokio-macros",
@ -6353,7 +6148,7 @@ dependencies = [
"fallible-iterator",
"futures",
"log",
"parking_lot 0.11.2",
"parking_lot",
"percent-encoding",
"phf 0.10.1",
"pin-project-lite 0.2.8",
@ -6558,7 +6353,7 @@ dependencies = [
"lazy_static",
"log",
"lru-cache",
"parking_lot 0.11.2",
"parking_lot",
"resolv-conf",
"smallvec",
"thiserror",
@ -7242,49 +7037,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3df6e476185f92a12c072be4a189a0210dcdcf512a1891d6dff9edb874deadc6"
dependencies = [
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_msvc"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8e92753b1c443191654ec532f14c199742964a061be25d77d7a96f09db20bf5"
[[package]]
name = "windows_i686_gnu"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a711c68811799e017b6038e0922cb27a5e2f43a2ddb609fe0b6f3eeda9de615"
[[package]]
name = "windows_i686_msvc"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "146c11bb1a02615db74680b32a68e2d61f553cc24c4eb5b4ca10311740e44172"
[[package]]
name = "windows_x86_64_gnu"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c912b12f7454c6620635bbff3450962753834be2a594819bd5e945af18ec64bc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316"
[[package]]
name = "winreg"
version = "0.6.2"

View File

@ -4,10 +4,11 @@ version = "1.0.0"
edition = "2021"
[dependencies]
actix = "0.10"
actix-web = "3"
actix-redis = "0.9"
futures = "0.3"
redis-async = "0.6.3"
actix = "0.12"
actix-web = "4.0.0-rc.3"
actix-redis = "0.10.0-beta.6"
futures-util = { version = "0.3.7", default-features = false, features = ["std"] }
log = "0.4"
redis-async = { version = "0.8", default_features = false, features = ["tokio10"] }
serde = { version = "1", features = ["derive"] }
env_logger = "0.8"
env_logger = "0.9"

View File

@ -1,15 +1,65 @@
This project illustrates how to send multiple cache requests to redis in bulk, asynchronously.
This asyncio approach resembles traditional redis pipelining. Details about how this
is so can be read at https://github.com/benashford/redis-async-rs/issues/19#issuecomment-412208018
# Redis
This project illustrates how to send multiple cache requests to Redis in bulk, asynchronously. This approach resembles traditional Redis pipelining. [See here for more details about this approach.](https://github.com/benashford/redis-async-rs/issues/19#issuecomment-412208018)
## Start Server
To test the demo, POST a json object containing three strings to the /stuff endpoint:
{"one": "first entry",
```sh
cd database_interactions/redis
cargo run
```
## Endpoints
### `POST /stuff`
To test the demo, POST a json object containing three strings to the `/stuff` endpoint:
```json
{
"one": "first entry",
"two": "second entry",
"three": "third entry" }
"three": "third entry"
}
```
These three entries will cache to redis, keyed accordingly.
to delete these, simply issue a DELETE http request to /stuff endpoint
Using [HTTPie]:
```sh
http :8080/stuff one="first entry" two="second entry" three="third entry"
```
Using [cURL]:
```sh
curl localhost:8080/stuff -H 'content-type: application/json' -d '{"one":"first entry","two":"second entry","three":"third entry"}'
```
### `DELETE /stuff`
To delete these, simply issue a DELETE http request to /stuff endpoint
Using [HTTPie]:
```sh
http DELETE :8080/stuff
```
Using [cURL]:
```sh
curl -XDELETE 127.0.0.1:8080/stuff
```
## Verify Redis Contents
At any time, verify the contents of Redis using its CLI:
```sh
echo "MGET mydomain:one mydomain:two mydomain:three" | redis-cli
```
[HTTPie]: https://httpie.org
[cURL]: https://curl.haxx.se

View File

@ -1,7 +1,7 @@
use actix::prelude::*;
use actix_redis::{Command, RedisActor};
use actix_web::{middleware, web, App, Error as AWError, HttpResponse, HttpServer};
use futures::future::join_all;
use actix_web::{error, middleware, web, App, HttpResponse, HttpServer};
use futures_util::future::try_join_all;
use redis_async::{resp::RespValue, resp_array};
use serde::Deserialize;
@ -15,42 +15,40 @@ pub struct CacheInfo {
async fn cache_stuff(
info: web::Json<CacheInfo>,
redis: web::Data<Addr<RedisActor>>,
) -> Result<HttpResponse, AWError> {
) -> actix_web::Result<HttpResponse> {
let info = info.into_inner();
let one = redis.send(Command(resp_array!["SET", "mydomain:one", info.one]));
let two = redis.send(Command(resp_array!["SET", "mydomain:two", info.two]));
let three = redis.send(Command(resp_array!["SET", "mydomain:three", info.three]));
// Creates a future which represents a collection of the results of the futures
// given. The returned future will drive execution for all of its underlying futures,
// collecting the results into a destination `Vec<RespValue>` in the same order as they
// were provided. If any future returns an error then all other futures will be
// canceled and an error will be returned immediately. If all futures complete
// successfully, however, then the returned future will succeed with a `Vec` of
// all the successful results.
let res: Vec<Result<RespValue, AWError>> =
join_all(vec![one, two, three].into_iter())
// Asynchronously collects the results of the futures given. The returned future will drive
// execution for all of its underlying futures, collecting the results into a destination
// `Vec<RespValue>` in the same order as they were provided. If any future returns an error then
// all other futures will be canceled and an error will be returned immediately. If all futures
// complete successfully, however, then the returned future will succeed with a `Vec` of all the
// successful results.
let res = try_join_all([one, two, three])
.await
.map_err(error::ErrorInternalServerError)?
.into_iter()
.map(|item| {
item.map_err(AWError::from)
.and_then(|res| res.map_err(AWError::from))
})
.collect();
.map(|item| item.map_err(error::ErrorInternalServerError))
.collect::<Result<Vec<_>, _>>()?;
// successful operations return "OK", so confirm that all returned as so
if !res
if res
.iter()
.all(|res| matches!(res,Ok(RespValue::SimpleString(x)) if x == "OK"))
.all(|res| matches!(res, RespValue::SimpleString(x) if x == "OK"))
{
Ok(HttpResponse::InternalServerError().finish())
} else {
Ok(HttpResponse::Ok().body("successfully cached values"))
} else {
Ok(HttpResponse::InternalServerError().finish())
}
}
async fn del_stuff(redis: web::Data<Addr<RedisActor>>) -> Result<HttpResponse, AWError> {
async fn del_stuff(
redis: web::Data<Addr<RedisActor>>,
) -> actix_web::Result<HttpResponse> {
let res = redis
.send(Command(resp_array![
"DEL",
@ -58,14 +56,17 @@ async fn del_stuff(redis: web::Data<Addr<RedisActor>>) -> Result<HttpResponse, A
"mydomain:two",
"mydomain:three"
]))
.await?;
.await
.map_err(error::ErrorInternalServerError)?
.map_err(error::ErrorInternalServerError)?;
match res {
Ok(RespValue::Integer(x)) if x == 3 => {
RespValue::Integer(x) if x == 3 => {
Ok(HttpResponse::Ok().body("successfully deleted values"))
}
_ => {
println!("---->{:?}", res);
log::error!("{:?}", res);
Ok(HttpResponse::InternalServerError().finish())
}
}
@ -73,14 +74,15 @@ async fn del_stuff(redis: web::Data<Addr<RedisActor>>) -> Result<HttpResponse, A
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=trace,actix_redis=trace");
env_logger::init();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| {
let redis_addr = RedisActor::start("127.0.0.1:6379");
App::new()
.data(redis_addr)
.app_data(web::Data::new(redis_addr))
.wrap(middleware::Logger::default())
.service(
web::resource("/stuff")
@ -88,7 +90,7 @@ async fn main() -> std::io::Result<()> {
.route(web::delete().to(del_stuff)),
)
})
.bind("0.0.0.0:8080")?
.bind(("127.0.0.1", 8080))?
.run()
.await
}