Compare commits
22 Commits
9e6d407b69
...
master
Author | SHA1 | Date | |
---|---|---|---|
9e16684ac9 | |||
ae257f949d | |||
fd7d85edbb | |||
5502013b76 | |||
cb7526dd45 | |||
ce3400e353 | |||
80124af836 | |||
e7c7d9f270 | |||
ea825c89eb | |||
57a09461f1 | |||
4fbbd30c4b | |||
1bfbfc5e44 | |||
55ffa538e8 | |||
cb9bac9d8a | |||
cb69c4d55a | |||
ce10c73eae | |||
38af08eb14 | |||
0ab0b59f4c | |||
a61474f38a | |||
393b66a9f5 | |||
4fc050e590 | |||
bb70cc65e0 |
337
Cargo.lock
generated
337
Cargo.lock
generated
@ -82,17 +82,6 @@ dependencies = [
|
|||||||
"windows-sys 0.59.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "async-trait"
|
|
||||||
version = "0.1.88"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@ -101,13 +90,13 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "axum"
|
name = "axum"
|
||||||
version = "0.7.9"
|
version = "0.8.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f"
|
checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
|
||||||
"axum-core",
|
"axum-core",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"form_urlencoded",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
@ -135,13 +124,12 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "axum-core"
|
name = "axum-core"
|
||||||
version = "0.4.5"
|
version = "0.5.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199"
|
checksum = "68464cd0412f486726fb3373129ef5d2993f90c34bc2bc1c1e9943b2f4fc7ca6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-util",
|
"futures-core",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
"http-body-util",
|
"http-body-util",
|
||||||
@ -154,30 +142,6 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "axum-extra"
|
|
||||||
version = "0.9.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c794b30c904f0a1c2fb7740f7df7f7972dfaa14ef6f57cb6178dc63e5dca2f04"
|
|
||||||
dependencies = [
|
|
||||||
"axum",
|
|
||||||
"axum-core",
|
|
||||||
"bytes",
|
|
||||||
"cookie",
|
|
||||||
"fastrand",
|
|
||||||
"futures-util",
|
|
||||||
"http",
|
|
||||||
"http-body",
|
|
||||||
"http-body-util",
|
|
||||||
"mime",
|
|
||||||
"multer",
|
|
||||||
"pin-project-lite",
|
|
||||||
"serde",
|
|
||||||
"tower",
|
|
||||||
"tower-layer",
|
|
||||||
"tower-service",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "backtrace"
|
name = "backtrace"
|
||||||
version = "0.3.74"
|
version = "0.3.74"
|
||||||
@ -193,27 +157,12 @@ dependencies = [
|
|||||||
"windows-targets",
|
"windows-targets",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "base64"
|
|
||||||
version = "0.22.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.9.0"
|
version = "2.9.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "block-buffer"
|
|
||||||
version = "0.10.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
|
|
||||||
dependencies = [
|
|
||||||
"generic-array",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bumpalo"
|
name = "bumpalo"
|
||||||
version = "3.17.0"
|
version = "3.17.0"
|
||||||
@ -228,9 +177,9 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.2.18"
|
version = "1.2.21"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "525046617d8376e3db1deffb079e91cef90a89fc3ca5c185bbf8c9ecdd15cd5c"
|
checksum = "8691782945451c1c383942c4874dbe63814f61cb57ef773cda2972682b7bb3c0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"shlex",
|
"shlex",
|
||||||
]
|
]
|
||||||
@ -243,9 +192,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chrono"
|
name = "chrono"
|
||||||
version = "0.4.40"
|
version = "0.4.41"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
|
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"android-tzdata",
|
"android-tzdata",
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
@ -257,9 +206,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "4.5.35"
|
version = "4.5.37"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d8aa86934b44c19c50f87cc2790e19f54f7a67aedb64101c2e1a2e5ecfb73944"
|
checksum = "eccb054f56cbd38340b380d4a8e69ef1f02f1af43db2f0cc817a4774d80ae071"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
"clap_derive",
|
"clap_derive",
|
||||||
@ -267,9 +216,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_builder"
|
name = "clap_builder"
|
||||||
version = "4.5.35"
|
version = "4.5.37"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2414dbb2dd0695280da6ea9261e327479e9d37b0630f6b53ba2a11c60c679fd9"
|
checksum = "efd9466fac8543255d3b1fcad4762c5e116ffe808c8a3043d4263cd4fd4862a2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anstream",
|
"anstream",
|
||||||
"anstyle",
|
"anstyle",
|
||||||
@ -307,12 +256,7 @@ version = "0.18.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4ddef33a339a91ea89fb53151bd0a4689cfce27055c291dfa69945475d22c747"
|
checksum = "4ddef33a339a91ea89fb53151bd0a4689cfce27055c291dfa69945475d22c747"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64",
|
|
||||||
"hmac",
|
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
"rand",
|
|
||||||
"sha2",
|
|
||||||
"subtle",
|
|
||||||
"time",
|
"time",
|
||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
@ -323,25 +267,6 @@ version = "0.8.7"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cpufeatures"
|
|
||||||
version = "0.2.17"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
|
|
||||||
dependencies = [
|
|
||||||
"libc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "crypto-common"
|
|
||||||
version = "0.1.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
|
|
||||||
dependencies = [
|
|
||||||
"generic-array",
|
|
||||||
"typenum",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "deranged"
|
name = "deranged"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
@ -351,32 +276,6 @@ dependencies = [
|
|||||||
"powerfmt",
|
"powerfmt",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "digest"
|
|
||||||
version = "0.10.7"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
|
||||||
dependencies = [
|
|
||||||
"block-buffer",
|
|
||||||
"crypto-common",
|
|
||||||
"subtle",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "encoding_rs"
|
|
||||||
version = "0.8.35"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "fastrand"
|
|
||||||
version = "2.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fnv"
|
name = "fnv"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
@ -407,6 +306,17 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-macro"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-task"
|
name = "futures-task"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@ -420,30 +330,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-macro",
|
||||||
"futures-task",
|
"futures-task",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
]
|
"slab",
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "generic-array"
|
|
||||||
version = "0.14.7"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
|
||||||
dependencies = [
|
|
||||||
"typenum",
|
|
||||||
"version_check",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "getrandom"
|
|
||||||
version = "0.2.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"libc",
|
|
||||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -470,15 +361,6 @@ version = "0.5.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "hmac"
|
|
||||||
version = "0.12.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
|
|
||||||
dependencies = [
|
|
||||||
"digest",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "1.3.1"
|
version = "1.3.1"
|
||||||
@ -608,9 +490,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.171"
|
version = "0.2.172"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
|
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
@ -630,9 +512,9 @@ checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "matchit"
|
name = "matchit"
|
||||||
version = "0.7.3"
|
version = "0.8.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
|
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
@ -671,32 +553,16 @@ name = "morethantext"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"axum-extra",
|
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"rand",
|
"http-body-util",
|
||||||
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower",
|
"tower",
|
||||||
|
"tower-cookies",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "multer"
|
|
||||||
version = "3.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "83e87776546dc87511aa5ee218730c92b666d7264ab6ed41f9d215af9cd5224b"
|
|
||||||
dependencies = [
|
|
||||||
"bytes",
|
|
||||||
"encoding_rs",
|
|
||||||
"futures-util",
|
|
||||||
"http",
|
|
||||||
"httparse",
|
|
||||||
"memchr",
|
|
||||||
"mime",
|
|
||||||
"spin",
|
|
||||||
"version_check",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-conv"
|
name = "num-conv"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@ -774,20 +640,11 @@ version = "0.2.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ppv-lite86"
|
|
||||||
version = "0.2.21"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
|
||||||
dependencies = [
|
|
||||||
"zerocopy",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.94"
|
version = "1.0.95"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
|
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
@ -807,41 +664,11 @@ version = "5.2.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
|
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rand"
|
|
||||||
version = "0.8.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
|
||||||
dependencies = [
|
|
||||||
"libc",
|
|
||||||
"rand_chacha",
|
|
||||||
"rand_core",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rand_chacha"
|
|
||||||
version = "0.3.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
|
||||||
dependencies = [
|
|
||||||
"ppv-lite86",
|
|
||||||
"rand_core",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rand_core"
|
|
||||||
version = "0.6.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
|
||||||
dependencies = [
|
|
||||||
"getrandom 0.2.15",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.11"
|
version = "0.5.12"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d2f103c6d277498fbceb16e84d317e2a400f160f46904d5f5410848c829511a3"
|
checksum = "928fca9cf2aa042393a8325b9ead81d2f0df4cb12e1e24cef072922ccd99c5af"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
@ -924,17 +751,6 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "sha2"
|
|
||||||
version = "0.10.8"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"cpufeatures",
|
|
||||||
"digest",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shlex"
|
name = "shlex"
|
||||||
version = "1.3.0"
|
version = "1.3.0"
|
||||||
@ -943,13 +759,22 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signal-hook-registry"
|
name = "signal-hook-registry"
|
||||||
version = "1.4.2"
|
version = "1.4.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
|
checksum = "9203b8055f63a2a00e2f593bb0510367fe707d7ff1e5c872de2f537b339e5410"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "slab"
|
||||||
|
version = "0.4.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smallvec"
|
name = "smallvec"
|
||||||
version = "1.15.0"
|
version = "1.15.0"
|
||||||
@ -966,29 +791,17 @@ dependencies = [
|
|||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "spin"
|
|
||||||
version = "0.9.8"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.11.1"
|
version = "0.11.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "subtle"
|
|
||||||
version = "2.6.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.100"
|
version = "2.0.101"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0"
|
checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -1034,9 +847,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.44.2"
|
version = "1.45.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e6b88822cbe49de4185e3a4cbf8321dd487cf5fe0c5c65695fef6346371e9c48"
|
checksum = "2513ca694ef9ede0fb23fe71a4ee4107cb102b9dc1930f6d0fd77aae068ae165"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"backtrace",
|
"backtrace",
|
||||||
"bytes",
|
"bytes",
|
||||||
@ -1077,6 +890,22 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower-cookies"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "151b5a3e3c45df17466454bb74e9ecedecc955269bdedbf4d150dfa393b55a36"
|
||||||
|
dependencies = [
|
||||||
|
"axum-core",
|
||||||
|
"cookie",
|
||||||
|
"futures-util",
|
||||||
|
"http",
|
||||||
|
"parking_lot",
|
||||||
|
"pin-project-lite",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-layer"
|
name = "tower-layer"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
@ -1109,12 +938,6 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "typenum"
|
|
||||||
version = "1.18.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
@ -1133,7 +956,7 @@ version = "1.16.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9"
|
checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getrandom 0.3.2",
|
"getrandom",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1364,23 +1187,3 @@ checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "zerocopy"
|
|
||||||
version = "0.8.24"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2586fea28e186957ef732a5f8b3be2da217d65c5969d4b1e17f973ebbe876879"
|
|
||||||
dependencies = [
|
|
||||||
"zerocopy-derive",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "zerocopy-derive"
|
|
||||||
version = "0.8.24"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a996a8f63c5c4448cd959ac1bab0aaa3306ccfd060472f85943ee0750f0169be"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
17
Cargo.toml
17
Cargo.toml
@ -6,13 +6,14 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = "0.7.4"
|
axum = ">=0.8.0"
|
||||||
axum-extra = { version = "0.9.2", features = ["cookie-signed"] }
|
chrono = { version = ">=0.4.40", features = ["now"] }
|
||||||
chrono = { version = "0.4.40", features = ["now"] }
|
clap = { version = ">=4.5.1", features = ["derive"] }
|
||||||
clap = { version = "4.5.1", features = ["derive"] }
|
serde_json = ">=1.0.140"
|
||||||
rand = "0.8.5"
|
tokio = { version = ">=1.36.0", features = ["full"] }
|
||||||
tokio = { version = "1.36.0", features = ["full"] }
|
tower-cookies = ">=0.11.0"
|
||||||
uuid = { version = "1.8.0", features = ["v4"] }
|
uuid = { version = ">=1.8.0", features = ["v4"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tower = { version = "0.5.2", features = ["util"] }
|
http-body-util = ">=0.1.3"
|
||||||
|
tower = { version = ">=0.5.2", features = ["util"] }
|
||||||
|
412
src/client.rs
412
src/client.rs
@ -1,8 +1,4 @@
|
|||||||
use crate::{
|
use crate::queue::{Message, MsgType, Queue};
|
||||||
field::Field,
|
|
||||||
queue::{Message, MsgType, Queue},
|
|
||||||
utils::GenID,
|
|
||||||
};
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
sync::{
|
sync::{
|
||||||
@ -13,370 +9,170 @@ use std::{
|
|||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
const RESPONS_TO: [MsgType; 2] = [MsgType::Document, MsgType::SessionValidated];
|
const RESPONS_TO: [MsgType; 4] = [
|
||||||
|
MsgType::ActionOk,
|
||||||
pub struct Request {
|
MsgType::Document,
|
||||||
pub session: Option<Field>,
|
MsgType::Error,
|
||||||
}
|
MsgType::SessionValidated,
|
||||||
|
];
|
||||||
impl Request {
|
|
||||||
pub fn new(session: Option<Field>) -> Self {
|
|
||||||
Self { session: session }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub mod requests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
pub fn get_root_document() -> Request {
|
|
||||||
Request::new(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_root_with_session(sess_id: &Uuid) -> Request {
|
|
||||||
Request::new(Some(sess_id.clone().into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_root_document_eith_session<F>(id: F) -> Request
|
|
||||||
where
|
|
||||||
F: Into<Field>,
|
|
||||||
{
|
|
||||||
Request::new(Some(id.into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn new_request_no_session() {
|
|
||||||
let sess: Option<Field> = None;
|
|
||||||
let req = Request::new(sess);
|
|
||||||
assert!(req.session.is_none(), "should not have a session")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn new_request_with_session() {
|
|
||||||
let id = Uuid::new_v4();
|
|
||||||
let req = Request::new(Some(id.into()));
|
|
||||||
match req.session {
|
|
||||||
Some(result) => assert_eq!(result.to_uuid().unwrap(), id),
|
|
||||||
None => unreachable!("should contain a session"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Reply {
|
|
||||||
sess_id: Uuid,
|
|
||||||
content: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Reply {
|
|
||||||
fn new(sess_id: Uuid, content: String) -> Self {
|
|
||||||
Self {
|
|
||||||
sess_id: sess_id,
|
|
||||||
content: content,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_session(&self) -> Uuid {
|
|
||||||
self.sess_id.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_content(&self) -> String {
|
|
||||||
self.content.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod replies {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
pub fn create_reply() -> Reply {
|
|
||||||
Reply {
|
|
||||||
sess_id: Uuid::new_v4(),
|
|
||||||
content: "some text".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn create_new_reply() {
|
|
||||||
let sess_id = Uuid::new_v4();
|
|
||||||
let txt = Uuid::new_v4().to_string();
|
|
||||||
let reply = Reply::new(sess_id, txt.clone());
|
|
||||||
assert_eq!(reply.get_session(), sess_id);
|
|
||||||
assert_eq!(reply.get_content(), txt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ClientRegistry {
|
pub struct ClientChannel {
|
||||||
registry: Arc<Mutex<HashMap<Uuid, Sender<Reply>>>>,
|
queue: Queue,
|
||||||
|
registry: Arc<Mutex<HashMap<Uuid, Sender<Message>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientRegistry {
|
impl ClientChannel {
|
||||||
pub fn new() -> Self {
|
fn new(queue: Queue) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
queue: queue,
|
||||||
registry: Arc::new(Mutex::new(HashMap::new())),
|
registry: Arc::new(Mutex::new(HashMap::new())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_id<'a>(
|
pub fn send(&self, mut msg: Message) -> Receiver<Message> {
|
||||||
gen: &mut impl Iterator<Item = Uuid>,
|
|
||||||
data: &HashMap<Uuid, Sender<Reply>>,
|
|
||||||
) -> Uuid {
|
|
||||||
let mut id = gen.next().unwrap();
|
|
||||||
while data.contains_key(&id) {
|
|
||||||
id = gen.next().unwrap();
|
|
||||||
}
|
|
||||||
id.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add(&mut self, tx: Sender<Reply>) -> Uuid {
|
|
||||||
let mut reg = self.registry.lock().unwrap();
|
let mut reg = self.registry.lock().unwrap();
|
||||||
let mut gen_id = GenID::new();
|
while reg.contains_key(&msg.get_id()) {
|
||||||
let id = ClientRegistry::get_id(&mut gen_id, ®);
|
msg.reset_id();
|
||||||
reg.insert(id.clone(), tx);
|
|
||||||
id
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send(&mut self, id: &Uuid, msg: Reply) {
|
|
||||||
let mut reg = self.registry.lock().unwrap();
|
|
||||||
let tx = reg.remove(id).unwrap();
|
|
||||||
tx.send(msg).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod clientregistries {
|
|
||||||
use super::*;
|
|
||||||
use crate::client::replies::create_reply;
|
|
||||||
use std::{
|
|
||||||
sync::mpsc::{channel, Receiver},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
|
|
||||||
static TIMEOUT: Duration = Duration::from_millis(500);
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn create_client_registry() {
|
|
||||||
let reg = ClientRegistry::new();
|
|
||||||
let data = reg.registry.lock().unwrap();
|
|
||||||
assert!(data.is_empty(), "needs to create an empty hashmap");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn send_from_client() {
|
|
||||||
let mut reg = ClientRegistry::new();
|
|
||||||
let count = 10;
|
|
||||||
let mut rxs: HashMap<Uuid, Receiver<Reply>> = HashMap::new();
|
|
||||||
for _ in 0..count {
|
|
||||||
let (tx, rx) = channel::<Reply>();
|
|
||||||
let id = reg.add(tx);
|
|
||||||
rxs.insert(id, rx);
|
|
||||||
}
|
|
||||||
assert_eq!(rxs.len(), count, "should have been {} receivers", count);
|
|
||||||
for (id, rx) in rxs.iter() {
|
|
||||||
let msg = create_reply();
|
|
||||||
reg.send(id, msg);
|
|
||||||
rx.recv_timeout(TIMEOUT).unwrap();
|
|
||||||
}
|
|
||||||
let data = reg.registry.lock().unwrap();
|
|
||||||
assert!(data.is_empty(), "should remove sender after sending");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn prevent_duplicates() {
|
|
||||||
let mut reg = ClientRegistry::new();
|
|
||||||
let (tx, _rx) = channel::<Reply>();
|
|
||||||
let existing = reg.add(tx);
|
|
||||||
let expected = Uuid::new_v4();
|
|
||||||
let ids = [existing.clone(), expected.clone()];
|
|
||||||
let data = reg.registry.lock().unwrap();
|
|
||||||
let result = ClientRegistry::get_id(&mut ids.into_iter(), &data);
|
|
||||||
assert_eq!(result, expected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ClientLink {
|
|
||||||
tx: Sender<Message>,
|
|
||||||
registry: ClientRegistry,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ClientLink {
|
|
||||||
fn new(tx: Sender<Message>, registry: ClientRegistry) -> Self {
|
|
||||||
Self {
|
|
||||||
tx: tx,
|
|
||||||
registry: registry,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn send(&mut self, req: Request) -> Receiver<Reply> {
|
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let mut msg: Message = req.into();
|
reg.insert(msg.get_id(), tx);
|
||||||
let id = self.registry.add(tx);
|
self.queue.send(msg).unwrap();
|
||||||
msg.add_data("tx_id", id);
|
|
||||||
self.tx.send(msg).unwrap();
|
|
||||||
rx
|
rx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn reply(&self, msg: Message) {
|
||||||
|
let mut reg = self.registry.lock().unwrap();
|
||||||
|
match reg.remove(&msg.get_id()) {
|
||||||
|
Some(tx) => tx.send(msg).unwrap(),
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod clientlinks {
|
mod client_channels {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::client::replies::create_reply;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
static TIMEOUT: Duration = Duration::from_millis(500);
|
static TIMEOUT: Duration = Duration::from_millis(500);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_client_link() {
|
fn fowards_message() {
|
||||||
|
let msg_type = MsgType::Document;
|
||||||
|
let reply_type = MsgType::Time;
|
||||||
|
let queue = Queue::new();
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let mut registry = ClientRegistry::new();
|
queue.add(tx, [msg_type.clone()].to_vec());
|
||||||
let mut link = ClientLink::new(tx, registry.clone());
|
let chan = ClientChannel::new(queue);
|
||||||
let req = Request::new(None);
|
let msg = Message::new(msg_type.clone());
|
||||||
let rx_client = link.send(req);
|
let client_rx = chan.send(msg.clone());
|
||||||
let msg = rx.recv_timeout(TIMEOUT).unwrap();
|
let reply = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
match msg.get_msg_type() {
|
assert_eq!(reply.get_id(), msg.get_id());
|
||||||
MsgType::ClientRequest => {}
|
assert_eq!(reply.get_msg_type().clone(), msg_type);
|
||||||
_ => unreachable!("should have been a client request"),
|
let client_reply = reply.reply(MsgType::Time);
|
||||||
|
chan.reply(client_reply);
|
||||||
|
let client_msg = client_rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_eq!(client_msg.get_id(), msg.get_id());
|
||||||
|
assert_eq!(client_msg.get_msg_type().clone(), reply_type);
|
||||||
}
|
}
|
||||||
match msg.get_data("tx_id") {
|
|
||||||
Some(result) => {
|
#[test]
|
||||||
let id = result.to_uuid().unwrap();
|
fn no_duplicate_ids() {
|
||||||
registry.send(&id, create_reply());
|
let (tx, rx) = channel();
|
||||||
rx_client.recv().unwrap();
|
let queue = Queue::new();
|
||||||
}
|
queue.add(tx, [MsgType::Time].to_vec());
|
||||||
None => unreachable!("should have had a seender id"),
|
let chan = ClientChannel::new(queue);
|
||||||
|
let msg1 = Message::new(MsgType::Time);
|
||||||
|
let msg2 = msg1.reply(MsgType::Time);
|
||||||
|
let rx1 = chan.send(msg1);
|
||||||
|
let rx2 = chan.send(msg2);
|
||||||
|
let queue1 = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
let queue2 = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_ne!(queue1.get_id(), queue2.get_id());
|
||||||
|
chan.reply(queue1.reply(MsgType::Document));
|
||||||
|
chan.reply(queue2.reply(MsgType::Document));
|
||||||
|
let reply1 = rx1.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
let reply2 = rx2.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_eq!(reply1.get_id(), queue1.get_id());
|
||||||
|
assert_eq!(reply2.get_id(), queue2.get_id());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ignore_unrequested() {
|
||||||
|
let queue = Queue::new();
|
||||||
|
let chan = ClientChannel::new(queue);
|
||||||
|
chan.reply(Message::new(MsgType::Document));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
queue: Queue,
|
channel: ClientChannel,
|
||||||
registry: ClientRegistry,
|
|
||||||
return_to: HashMap<Uuid, Message>,
|
|
||||||
rx: Receiver<Message>,
|
rx: Receiver<Message>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
fn new(rx: Receiver<Message>, queue: Queue) -> Self {
|
fn new(chan: ClientChannel, rx: Receiver<Message>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
queue: queue,
|
channel: chan,
|
||||||
registry: ClientRegistry::new(),
|
|
||||||
return_to: HashMap::new(),
|
|
||||||
rx: rx,
|
rx: rx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(queue: Queue) -> ClientLink {
|
pub fn start(queue: Queue) -> ClientChannel {
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
queue.add(tx.clone(), RESPONS_TO.to_vec());
|
queue.add(tx.clone(), RESPONS_TO.to_vec());
|
||||||
let mut client = Client::new(rx, queue);
|
let chan = ClientChannel::new(queue.clone());
|
||||||
let link = ClientLink::new(tx, client.get_registry());
|
let client = Client::new(chan.clone(), rx);
|
||||||
spawn(move || {
|
spawn(move || {
|
||||||
client.listen();
|
client.listen();
|
||||||
});
|
});
|
||||||
link
|
chan
|
||||||
}
|
}
|
||||||
|
|
||||||
fn listen(&mut self) {
|
fn listen(&self) {
|
||||||
loop {
|
loop {
|
||||||
let msg = self.rx.recv().unwrap();
|
let msg = self.rx.recv().unwrap();
|
||||||
match msg.get_msg_type() {
|
self.channel.reply(msg);
|
||||||
MsgType::ClientRequest => self.client_request(msg),
|
|
||||||
MsgType::Document => self.document(msg),
|
|
||||||
MsgType::SessionValidated => self.session(msg),
|
|
||||||
_ => unreachable!("Received message it did not understand"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn get_registry(&self) -> ClientRegistry {
|
|
||||||
self.registry.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn client_request(&mut self, msg: Message) {
|
|
||||||
self.return_to.insert(msg.get_id(), msg.clone());
|
|
||||||
let mut reply = msg.reply(MsgType::SessionValidate);
|
|
||||||
match msg.get_data("sess_id") {
|
|
||||||
Some(sess_id) => reply.add_data("sess_id", sess_id.clone()),
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
self.queue.send(reply).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn session(&mut self, msg: Message) {
|
|
||||||
let initial_msg = self.return_to.get_mut(&msg.get_id()).unwrap();
|
|
||||||
let mut reply = msg.reply(MsgType::DocumentRequest);
|
|
||||||
match msg.get_data("sess_id") {
|
|
||||||
Some(sess_id) => {
|
|
||||||
initial_msg.add_data("sess_id", sess_id.clone());
|
|
||||||
reply.add_data("sess_id", sess_id.clone());
|
|
||||||
}
|
|
||||||
None => unreachable!("validated should always have an id"),
|
|
||||||
}
|
|
||||||
self.queue.send(reply).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn document(&mut self, msg: Message) {
|
|
||||||
let initial_msg = self.return_to.remove(&msg.get_id()).unwrap();
|
|
||||||
let tx_id = initial_msg.get_data("tx_id").unwrap().to_uuid().unwrap();
|
|
||||||
let reply = Reply::new(
|
|
||||||
initial_msg.get_data("sess_id").unwrap().to_uuid().unwrap(),
|
|
||||||
msg.get_data("doc").unwrap().to_string(),
|
|
||||||
);
|
|
||||||
self.registry.send(&tx_id, reply);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod clients {
|
mod clients {
|
||||||
use super::*;
|
use super::*;
|
||||||
use requests::get_root_with_session;
|
use crate::session::sessions::create_validated_reply;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
static TIMEOUT: Duration = Duration::from_millis(500);
|
static TIMEOUT: Duration = Duration::from_millis(500);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn start_client() {
|
fn session_validated() {
|
||||||
let sess_id1 = Uuid::new_v4();
|
|
||||||
let sess_id2 = Uuid::new_v4();
|
|
||||||
let doc = Uuid::new_v4().to_string();
|
|
||||||
let (tx, rx) = channel();
|
|
||||||
let queue = Queue::new();
|
let queue = Queue::new();
|
||||||
queue.add(
|
let (queue_tx, queue_rx) = channel();
|
||||||
tx,
|
queue.add(queue_tx, [MsgType::SessionValidate].to_vec());
|
||||||
[MsgType::SessionValidate, MsgType::DocumentRequest].to_vec(),
|
let chan = Client::start(queue.clone());
|
||||||
);
|
let chan_rx = chan.send(Message::new(MsgType::SessionValidate));
|
||||||
let mut link = Client::start(queue.clone());
|
let msg = queue_rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
let req = get_root_with_session(&sess_id1);
|
let expected = create_validated_reply(msg);
|
||||||
let reply_rx = link.send(req);
|
queue.send(expected.clone()).unwrap();
|
||||||
let send1 = rx.recv_timeout(TIMEOUT).unwrap();
|
let result = chan_rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
match send1.get_msg_type() {
|
assert_eq!(result.get_id(), expected.get_id());
|
||||||
MsgType::SessionValidate => {}
|
assert_eq!(result.get_msg_type(), expected.get_msg_type());
|
||||||
_ => unreachable!("should request session validation"),
|
|
||||||
}
|
}
|
||||||
assert_eq!(
|
|
||||||
send1.get_data("sess_id").unwrap().to_uuid().unwrap(),
|
#[test]
|
||||||
sess_id1
|
fn document_return() {
|
||||||
);
|
let queue = Queue::new();
|
||||||
assert!(send1.get_data("tx_id").is_none());
|
let (queue_tx, queue_rx) = channel();
|
||||||
let mut response = send1.reply_with_data(MsgType::SessionValidated);
|
queue.add(queue_tx, [MsgType::DocumentRequest].to_vec());
|
||||||
response.add_data("sess_id", sess_id2);
|
let chan = Client::start(queue.clone());
|
||||||
queue.send(response).unwrap();
|
let chan_rx = chan.send(Message::new(MsgType::DocumentRequest));
|
||||||
let send2 = rx.recv_timeout(TIMEOUT).unwrap();
|
let msg = queue_rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
assert_eq!(send2.get_id(), send1.get_id());
|
let expected = msg.reply(MsgType::Document);
|
||||||
match send2.get_msg_type() {
|
queue.send(expected.clone()).unwrap();
|
||||||
MsgType::DocumentRequest => {}
|
let result = chan_rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
_ => unreachable!("should request session validation"),
|
assert_eq!(result.get_id(), expected.get_id());
|
||||||
}
|
assert_eq!(result.get_msg_type(), expected.get_msg_type());
|
||||||
assert_eq!(
|
|
||||||
send2.get_data("sess_id").unwrap().to_uuid().unwrap(),
|
|
||||||
sess_id2
|
|
||||||
);
|
|
||||||
let mut document = send2.reply(MsgType::Document);
|
|
||||||
document.add_data("doc", doc.clone());
|
|
||||||
queue.send(document).unwrap();
|
|
||||||
let reply = reply_rx.recv_timeout(TIMEOUT).unwrap();
|
|
||||||
assert_eq!(reply.get_session(), sess_id2);
|
|
||||||
assert_eq!(reply.get_content(), doc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
274
src/document.rs
274
src/document.rs
@ -1,5 +1,10 @@
|
|||||||
use crate::queue::{Message, MsgType, Queue};
|
use crate::{
|
||||||
|
queue::{Message, MsgType, Queue},
|
||||||
|
ActionType, ErrorType,
|
||||||
|
};
|
||||||
|
use serde_json::Value;
|
||||||
use std::{
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
sync::mpsc::{channel, Receiver},
|
sync::mpsc::{channel, Receiver},
|
||||||
thread::spawn,
|
thread::spawn,
|
||||||
};
|
};
|
||||||
@ -7,13 +12,17 @@ use std::{
|
|||||||
const RESPONDS_TO: [MsgType; 1] = [MsgType::DocumentRequest];
|
const RESPONDS_TO: [MsgType; 1] = [MsgType::DocumentRequest];
|
||||||
|
|
||||||
pub struct Document {
|
pub struct Document {
|
||||||
|
data: HashMap<String, String>,
|
||||||
queue: Queue,
|
queue: Queue,
|
||||||
rx: Receiver<Message>,
|
rx: Receiver<Message>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Document {
|
impl Document {
|
||||||
fn new(queue: Queue, rx: Receiver<Message>) -> Self {
|
fn new(queue: Queue, rx: Receiver<Message>) -> Self {
|
||||||
|
let mut data = HashMap::new();
|
||||||
|
data.insert("root".to_string(), "Something goes here.".to_string());
|
||||||
Self {
|
Self {
|
||||||
|
data: data,
|
||||||
queue: queue,
|
queue: queue,
|
||||||
rx: rx,
|
rx: rx,
|
||||||
}
|
}
|
||||||
@ -31,36 +40,102 @@ impl Document {
|
|||||||
fn listen(&mut self) {
|
fn listen(&mut self) {
|
||||||
loop {
|
loop {
|
||||||
let msg = self.rx.recv().unwrap();
|
let msg = self.rx.recv().unwrap();
|
||||||
let mut reply = msg.reply(MsgType::Document);
|
match msg.get_data("action") {
|
||||||
reply.add_data(
|
Some(action_field) => {
|
||||||
"sess_id",
|
let action = action_field.to_action().unwrap();
|
||||||
msg.get_data("sess_id").unwrap().to_uuid().unwrap(),
|
match action {
|
||||||
);
|
ActionType::Add | ActionType::Update => self.add(action, msg),
|
||||||
reply.add_data("doc", "Something goes hwew");
|
_ => self.get(msg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => self.get(msg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add(&mut self, action: ActionType, msg: Message) {
|
||||||
|
let name = msg.get_data("name").unwrap().to_string();
|
||||||
|
match self.data.get(&name) {
|
||||||
|
Some(_) => match action {
|
||||||
|
ActionType::Add => {
|
||||||
|
self.queue
|
||||||
|
.send(msg.reply_with_error(ErrorType::DocumentAlreadyExists))
|
||||||
|
.unwrap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ActionType::Update => {}
|
||||||
|
_ => unreachable!("listen should prevent anything else"),
|
||||||
|
},
|
||||||
|
None => match action {
|
||||||
|
ActionType::Add => {}
|
||||||
|
ActionType::Update => {
|
||||||
|
self.queue
|
||||||
|
.send(msg.reply_with_error(ErrorType::DocumentNotFound))
|
||||||
|
.unwrap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_ => unreachable!("listen should prevent anything else"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
let doc: Value = match serde_json::from_str(&msg.get_data("doc").unwrap().to_string()) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(_) => {
|
||||||
|
self.queue
|
||||||
|
.send(msg.reply_with_error(ErrorType::DocumentInvalidRequest))
|
||||||
|
.unwrap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let reply = match doc["template"].as_str() {
|
||||||
|
Some(content) => {
|
||||||
|
self.data.insert(name, content.to_string());
|
||||||
|
msg.reply(MsgType::ActionOk)
|
||||||
|
}
|
||||||
|
None => msg.reply_with_error(ErrorType::DocumentInvalidRequest),
|
||||||
|
};
|
||||||
self.queue.send(reply).unwrap();
|
self.queue.send(reply).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get(&self, msg: Message) {
|
||||||
|
let name = match msg.get_data("name") {
|
||||||
|
Some(doc) => doc.to_string(),
|
||||||
|
None => "root".to_string(),
|
||||||
|
};
|
||||||
|
let reply = match self.data.get(&name) {
|
||||||
|
Some(data) => {
|
||||||
|
let mut holder = msg.reply(MsgType::Document);
|
||||||
|
holder.add_data("doc", data.clone());
|
||||||
|
holder
|
||||||
|
}
|
||||||
|
None => msg.reply_with_error(ErrorType::DocumentNotFound),
|
||||||
|
};
|
||||||
|
self.queue.send(reply).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod documents {
|
pub mod documents {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use serde_json::json;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
const TIMEOUT: Duration = Duration::from_millis(500);
|
const TIMEOUT: Duration = Duration::from_millis(500);
|
||||||
|
|
||||||
fn setup_document(listen_for: Vec<MsgType>) -> (Queue, Receiver<Message>) {
|
fn setup_document() -> (Queue, Receiver<Message>) {
|
||||||
let queue = Queue::new();
|
let queue = Queue::new();
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
queue.add(tx, listen_for);
|
queue.add(
|
||||||
|
tx,
|
||||||
|
[MsgType::ActionOk, MsgType::Document, MsgType::Error].to_vec(),
|
||||||
|
);
|
||||||
Document::start(queue.clone());
|
Document::start(queue.clone());
|
||||||
(queue, rx)
|
(queue, rx)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn start_service() {
|
fn start_service() {
|
||||||
let (queue, rx) = setup_document([MsgType::Document].to_vec());
|
let (queue, rx) = setup_document();
|
||||||
let id = Uuid::new_v4();
|
let id = Uuid::new_v4();
|
||||||
let mut msg = Message::new(MsgType::DocumentRequest);
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
msg.add_data("sess_id", id.clone());
|
msg.add_data("sess_id", id.clone());
|
||||||
@ -71,7 +146,182 @@ mod documents {
|
|||||||
MsgType::Document => {}
|
MsgType::Document => {}
|
||||||
_ => unreachable!("got {:?} should have gotten document", msg.get_msg_type()),
|
_ => unreachable!("got {:?} should have gotten document", msg.get_msg_type()),
|
||||||
}
|
}
|
||||||
assert_eq!(reply.get_data("sess_id").unwrap().to_uuid().unwrap(), id);
|
|
||||||
assert!(reply.get_data("doc").is_some());
|
assert!(reply.get_data("doc").is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_existing_document() {
|
||||||
|
let (queue, rx) = setup_document();
|
||||||
|
let name = format!("name-{}", Uuid::new_v4());
|
||||||
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg.add_data("name", name.clone());
|
||||||
|
queue.send(msg.clone()).unwrap();
|
||||||
|
let reply = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_eq!(reply.get_id(), msg.get_id());
|
||||||
|
match reply.get_msg_type() {
|
||||||
|
MsgType::Error => {}
|
||||||
|
_ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()),
|
||||||
|
}
|
||||||
|
match reply.get_data("error_type") {
|
||||||
|
Some(err) => match err.to_error_type().unwrap() {
|
||||||
|
ErrorType::DocumentNotFound => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been document not found'", err),
|
||||||
|
},
|
||||||
|
None => unreachable!("should contain error type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn root_always_exists() {
|
||||||
|
let (queue, rx) = setup_document();
|
||||||
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg.add_data("name", "root");
|
||||||
|
queue.send(msg).unwrap();
|
||||||
|
let reply = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
match reply.get_msg_type() {
|
||||||
|
MsgType::Document => {}
|
||||||
|
_ => unreachable!(
|
||||||
|
"Got '{:?}': should have been a document",
|
||||||
|
reply.get_msg_type()
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_new_document() {
|
||||||
|
let (queue, rx) = setup_document();
|
||||||
|
let name = format!("name-{}", Uuid::new_v4());
|
||||||
|
let content = format!("content-{}", Uuid::new_v4());
|
||||||
|
let input = json!({
|
||||||
|
"template": content.clone()
|
||||||
|
});
|
||||||
|
let mut msg1 = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg1.add_data("name", name.clone());
|
||||||
|
msg1.add_data("action", ActionType::Add);
|
||||||
|
msg1.add_data("doc", input.to_string());
|
||||||
|
queue.send(msg1.clone()).unwrap();
|
||||||
|
let reply1 = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_eq!(reply1.get_id(), msg1.get_id());
|
||||||
|
match reply1.get_msg_type() {
|
||||||
|
MsgType::ActionOk => {}
|
||||||
|
_ => unreachable!(
|
||||||
|
"got {:?}: should have received action ok",
|
||||||
|
reply1.get_msg_type()
|
||||||
|
),
|
||||||
|
}
|
||||||
|
let mut msg2 = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg2.add_data("name", name.clone());
|
||||||
|
msg2.add_data("action", ActionType::Get);
|
||||||
|
queue.send(msg2.clone()).unwrap();
|
||||||
|
let reply2 = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_eq!(reply2.get_id(), msg2.get_id());
|
||||||
|
match reply2.get_msg_type() {
|
||||||
|
MsgType::Document => {}
|
||||||
|
_ => unreachable!(
|
||||||
|
"got {:?}: should have received document",
|
||||||
|
reply2.get_msg_type()
|
||||||
|
),
|
||||||
|
}
|
||||||
|
assert_eq!(reply2.get_data("doc").unwrap().to_string(), content);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_does_not_overwrite_existing() {
|
||||||
|
let (queue, rx) = setup_document();
|
||||||
|
let mut holder = Message::new(MsgType::DocumentRequest);
|
||||||
|
holder.add_data("name", "root");
|
||||||
|
holder.add_data("action", ActionType::Get);
|
||||||
|
queue.send(holder.clone()).unwrap();
|
||||||
|
let binding = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
let expected = binding.get_data("doc").unwrap();
|
||||||
|
let input = json!({
|
||||||
|
"template": format!("content-{}", Uuid::new_v4())
|
||||||
|
});
|
||||||
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg.add_data("name", "root");
|
||||||
|
msg.add_data("action", ActionType::Add);
|
||||||
|
msg.add_data("doc", input.to_string());
|
||||||
|
queue.send(msg.clone()).unwrap();
|
||||||
|
let reply = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_eq!(reply.get_id(), msg.get_id());
|
||||||
|
match reply.get_msg_type() {
|
||||||
|
MsgType::Error => {}
|
||||||
|
_ => unreachable!(
|
||||||
|
"got '{:?}': should have received document",
|
||||||
|
reply.get_msg_type()
|
||||||
|
),
|
||||||
|
}
|
||||||
|
match reply.get_data("error_type") {
|
||||||
|
Some(err) => match err.to_error_type().unwrap() {
|
||||||
|
ErrorType::DocumentAlreadyExists => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been document not found'", err),
|
||||||
|
},
|
||||||
|
None => unreachable!("should contain error type"),
|
||||||
|
}
|
||||||
|
queue.send(holder).unwrap();
|
||||||
|
let binding = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
let result = binding.get_data("doc").unwrap();
|
||||||
|
assert_eq!(result.to_string(), expected.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_json() {
|
||||||
|
let inputs = ["Invalid json request.", "{}"];
|
||||||
|
let (queue, rx) = setup_document();
|
||||||
|
for input in inputs.into_iter() {
|
||||||
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg.add_data("action", ActionType::Add);
|
||||||
|
msg.add_data("name", "doc");
|
||||||
|
msg.add_data("doc", input);
|
||||||
|
queue.send(msg.clone()).unwrap();
|
||||||
|
let reply = match rx.recv_timeout(TIMEOUT) {
|
||||||
|
Ok(data) => data,
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "got '{}' with the following json: '{}'", err, input);
|
||||||
|
Message::new(MsgType::Error)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assert_eq!(reply.get_id(), msg.get_id());
|
||||||
|
match reply.get_msg_type() {
|
||||||
|
MsgType::Error => {}
|
||||||
|
_ => unreachable!(
|
||||||
|
"got '{:?}': should have received document",
|
||||||
|
reply.get_msg_type()
|
||||||
|
),
|
||||||
|
}
|
||||||
|
match reply.get_data("error_type") {
|
||||||
|
Some(err) => match err.to_error_type().unwrap() {
|
||||||
|
ErrorType::DocumentInvalidRequest => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been bad request'", err),
|
||||||
|
},
|
||||||
|
None => unreachable!("should contain error type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn patch_nonexistant_page() {
|
||||||
|
let (queue, rx) = setup_document();
|
||||||
|
let input = json!({
|
||||||
|
"template": "Sothing here"
|
||||||
|
});
|
||||||
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg.add_data("action", ActionType::Update);
|
||||||
|
msg.add_data("name", "something");
|
||||||
|
msg.add_data("doc", input.to_string());
|
||||||
|
queue.send(msg.clone()).unwrap();
|
||||||
|
let reply = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_eq!(reply.get_id(), msg.get_id());
|
||||||
|
match reply.get_msg_type() {
|
||||||
|
MsgType::Error => {}
|
||||||
|
_ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()),
|
||||||
|
}
|
||||||
|
match reply.get_data("error_type") {
|
||||||
|
Some(err) => match err.to_error_type().unwrap() {
|
||||||
|
ErrorType::DocumentNotFound => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been document not found'", err),
|
||||||
|
},
|
||||||
|
None => unreachable!("should contain error type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
106
src/field.rs
106
src/field.rs
@ -1,15 +1,34 @@
|
|||||||
|
use crate::{ActionType, ErrorType};
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
enum FieldType {
|
||||||
|
Action,
|
||||||
|
DateTime,
|
||||||
|
Error,
|
||||||
|
StaticString,
|
||||||
|
Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Field {
|
pub enum Field {
|
||||||
|
Action(ActionType),
|
||||||
|
DateTime(DateTime<Utc>),
|
||||||
|
ErrorType(ErrorType),
|
||||||
Static(String),
|
Static(String),
|
||||||
Uuid(Uuid),
|
Uuid(Uuid),
|
||||||
DateTime(DateTime<Utc>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Field {
|
impl Field {
|
||||||
|
pub fn to_action(&self) -> Result<ActionType, String> {
|
||||||
|
match self {
|
||||||
|
Field::Action(data) => Ok(data.clone()),
|
||||||
|
_ => Err("not an action".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_uuid(&self) -> Result<Uuid, String> {
|
pub fn to_uuid(&self) -> Result<Uuid, String> {
|
||||||
match self {
|
match self {
|
||||||
Field::Uuid(data) => Ok(data.clone()),
|
Field::Uuid(data) => Ok(data.clone()),
|
||||||
@ -23,6 +42,13 @@ impl Field {
|
|||||||
_ => Err("not a datetime".to_string()),
|
_ => Err("not a datetime".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_error_type(&self) -> Result<ErrorType, String> {
|
||||||
|
match self {
|
||||||
|
Field::ErrorType(data) => Ok(data.clone()),
|
||||||
|
_ => Err("not an error type".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for Field {
|
impl From<String> for Field {
|
||||||
@ -45,6 +71,12 @@ impl From<&str> for Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<ActionType> for Field {
|
||||||
|
fn from(value: ActionType) -> Self {
|
||||||
|
Field::Action(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Uuid> for Field {
|
impl From<Uuid> for Field {
|
||||||
fn from(value: Uuid) -> Self {
|
fn from(value: Uuid) -> Self {
|
||||||
Field::Uuid(value)
|
Field::Uuid(value)
|
||||||
@ -57,12 +89,19 @@ impl From<DateTime<Utc>> for Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<ErrorType> for Field {
|
||||||
|
fn from(value: ErrorType) -> Self {
|
||||||
|
Field::ErrorType(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Field {
|
impl fmt::Display for Field {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Field::Uuid(data) => write!(f, "{}", data),
|
|
||||||
Field::Static(data) => write!(f, "{}", data),
|
|
||||||
Field::DateTime(data) => write!(f, "{}", data),
|
Field::DateTime(data) => write!(f, "{}", data),
|
||||||
|
Field::Static(data) => write!(f, "{}", data),
|
||||||
|
Field::Uuid(data) => write!(f, "{}", data),
|
||||||
|
_ => write!(f, ""),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,4 +235,65 @@ mod fields {
|
|||||||
let field: Field = txt.into();
|
let field: Field = txt.into();
|
||||||
assert!(field.to_datetime().is_err(), "should not return a value");
|
assert!(field.to_datetime().is_err(), "should not return a value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_error_to_field() {
|
||||||
|
let err = ErrorType::DocumentNotFound;
|
||||||
|
let field: Field = err.into();
|
||||||
|
match field {
|
||||||
|
Field::ErrorType(data) => match data {
|
||||||
|
ErrorType::DocumentNotFound => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been Document not found", data),
|
||||||
|
},
|
||||||
|
_ => unreachable!("should have been an error type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_field_to_error_type_error() {
|
||||||
|
let field: Field = Uuid::new_v4().into();
|
||||||
|
assert!(field.to_error_type().is_err(), "should generate an error");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_field_to_error_type() {
|
||||||
|
let err = ErrorType::DocumentNotFound;
|
||||||
|
let field: Field = err.into();
|
||||||
|
let result = field.to_error_type().unwrap();
|
||||||
|
match result {
|
||||||
|
ErrorType::DocumentNotFound => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been document not found", result),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_action_to_field() {
|
||||||
|
let actions = [ActionType::Add, ActionType::Get, ActionType::Update];
|
||||||
|
for action in actions.into_iter() {
|
||||||
|
let result: Field = action.clone().into();
|
||||||
|
match result {
|
||||||
|
Field::Action(data) => assert_eq!(format!("{:?}", data), format!("{:?}", action)),
|
||||||
|
_ => unreachable!("should have been an action"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_field_to_action() {
|
||||||
|
let actions = [ActionType::Add, ActionType::Get, ActionType::Update];
|
||||||
|
for action in actions.into_iter() {
|
||||||
|
let field: Field = action.clone().into();
|
||||||
|
let result = field.to_action().unwrap();
|
||||||
|
assert_eq!(format!("{:?}", result), format!("{:?}", action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_uuid_to_action() {
|
||||||
|
let field: Field = Uuid::new_v4().into();
|
||||||
|
match field.to_action() {
|
||||||
|
Ok(_) => unreachable!("should have returned an error"),
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
182
src/lib.rs
182
src/lib.rs
@ -4,18 +4,111 @@ mod document;
|
|||||||
mod field;
|
mod field;
|
||||||
mod queue;
|
mod queue;
|
||||||
mod session;
|
mod session;
|
||||||
mod utils;
|
|
||||||
|
|
||||||
use client::{Client, ClientLink, Reply, Request};
|
use client::{Client, ClientChannel};
|
||||||
use clock::Clock;
|
use clock::Clock;
|
||||||
use document::Document;
|
use document::Document;
|
||||||
use field::Field;
|
use field::Field;
|
||||||
use queue::Queue;
|
use queue::{Message, MsgType, Queue};
|
||||||
use session::Session;
|
use session::Session;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum ActionType {
|
||||||
|
Get,
|
||||||
|
Add,
|
||||||
|
Update,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum ErrorType {
|
||||||
|
DocumentAlreadyExists,
|
||||||
|
DocumentInvalidRequest,
|
||||||
|
DocumentNotFound,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MTTReply {
|
||||||
|
document: String,
|
||||||
|
error_type: Option<ErrorType>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MTTReply {
|
||||||
|
fn new(msg: Message) -> Self {
|
||||||
|
Self {
|
||||||
|
document: match msg.get_data("doc") {
|
||||||
|
Some(doc) => doc.to_string(),
|
||||||
|
None => "".to_string(),
|
||||||
|
},
|
||||||
|
error_type: match msg.get_data("error_type") {
|
||||||
|
Some(err) => Some(err.to_error_type().unwrap()),
|
||||||
|
None => None,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_document(&self) -> String {
|
||||||
|
self.document.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_error(&self) -> Option<ErrorType> {
|
||||||
|
self.error_type.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod mtt_replies {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_reply_with_no_error() {
|
||||||
|
let mut msg = Message::new(MsgType::Document);
|
||||||
|
let content = format!("content-{}", Uuid::new_v4());
|
||||||
|
msg.add_data("doc", content.to_string());
|
||||||
|
let reply = MTTReply::new(msg);
|
||||||
|
assert!(reply.get_error().is_none());
|
||||||
|
assert_eq!(reply.get_document(), content);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_reply_with_error() {
|
||||||
|
let mut msg = Message::new(MsgType::Error);
|
||||||
|
msg.add_data("error_type", ErrorType::DocumentNotFound);
|
||||||
|
let reply = MTTReply::new(msg);
|
||||||
|
match reply.get_error() {
|
||||||
|
Some(err) => match err {
|
||||||
|
ErrorType::DocumentNotFound => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been document not found", err),
|
||||||
|
},
|
||||||
|
None => unreachable!("should return an error type"),
|
||||||
|
}
|
||||||
|
assert_eq!(reply.get_document(), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_error() {
|
||||||
|
let msg = Message::new(MsgType::Document);
|
||||||
|
let reply = MTTReply::new(msg);
|
||||||
|
assert!(reply.get_error().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn some_error() {
|
||||||
|
let mut msg = Message::new(MsgType::Error);
|
||||||
|
msg.add_data("error_type", ErrorType::DocumentNotFound);
|
||||||
|
let reply = MTTReply::new(msg);
|
||||||
|
match reply.get_error() {
|
||||||
|
Some(err) => match err {
|
||||||
|
ErrorType::DocumentNotFound => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been document not found", err),
|
||||||
|
},
|
||||||
|
None => unreachable!("should return an error type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MoreThanText {
|
pub struct MoreThanText {
|
||||||
client_link: ClientLink,
|
client_channel: ClientChannel,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MoreThanText {
|
impl MoreThanText {
|
||||||
@ -25,20 +118,83 @@ impl MoreThanText {
|
|||||||
Document::start(queue.clone());
|
Document::start(queue.clone());
|
||||||
Session::start(queue.clone());
|
Session::start(queue.clone());
|
||||||
Self {
|
Self {
|
||||||
client_link: Client::start(queue.clone()),
|
client_channel: Client::start(queue.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request<F>(&mut self, session: Option<F>) -> Reply
|
pub fn validate_session<F>(&mut self, session: Option<F>) -> Uuid
|
||||||
where
|
where
|
||||||
F: Into<Field>,
|
F: Into<Field>,
|
||||||
{
|
{
|
||||||
let sess = match session {
|
let mut msg = Message::new(MsgType::SessionValidate);
|
||||||
Some(id) => Some(id.into()),
|
match session {
|
||||||
None => None,
|
Some(id) => msg.add_data("sess_id", id.into()),
|
||||||
};
|
None => {}
|
||||||
let req = Request::new(sess);
|
}
|
||||||
let rx = self.client_link.send(req);
|
let rx = self.client_channel.send(msg);
|
||||||
rx.recv().unwrap()
|
let reply = rx.recv().unwrap();
|
||||||
|
reply.get_data("sess_id").unwrap().to_uuid().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_document<S>(
|
||||||
|
&self,
|
||||||
|
sess_id: Uuid,
|
||||||
|
action: ActionType,
|
||||||
|
doc_name: S,
|
||||||
|
data: String,
|
||||||
|
) -> MTTReply
|
||||||
|
where
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg.add_data("sess_id", sess_id);
|
||||||
|
msg.add_data("action", action);
|
||||||
|
msg.add_data("name", doc_name.into());
|
||||||
|
msg.add_data("doc", data);
|
||||||
|
let rx = self.client_channel.send(msg);
|
||||||
|
let reply = rx.recv().unwrap();
|
||||||
|
MTTReply::new(reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod mtt {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn session_id_is_unique() {
|
||||||
|
let mut mtt = MoreThanText::new();
|
||||||
|
let input: Option<String> = None;
|
||||||
|
let mut ids: Vec<Uuid> = Vec::new();
|
||||||
|
for _ in 0..10 {
|
||||||
|
let id = mtt.validate_session(input.clone());
|
||||||
|
assert!(!ids.contains(&id));
|
||||||
|
ids.push(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reuse_existing_session() {
|
||||||
|
let mut mtt = MoreThanText::new();
|
||||||
|
let initial: Option<String> = None;
|
||||||
|
let id = mtt.validate_session(initial);
|
||||||
|
let output = mtt.validate_session(Some(id.clone()));
|
||||||
|
assert_eq!(output, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_root_document_with_str() {
|
||||||
|
let mut mtt = MoreThanText::new();
|
||||||
|
let id = mtt.validate_session(Some(Uuid::new_v4()));
|
||||||
|
let output = mtt.get_document(id, ActionType::Get, "root", "".to_string());
|
||||||
|
assert!(output.get_error().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_root_document_with_string() {
|
||||||
|
let mut mtt = MoreThanText::new();
|
||||||
|
let id = mtt.validate_session(Some(Uuid::new_v4()));
|
||||||
|
let output = mtt.get_document(id, ActionType::Get, "root".to_string(), "".to_string());
|
||||||
|
assert!(output.get_error().is_none());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
300
src/main.rs
300
src/main.rs
@ -1,8 +1,16 @@
|
|||||||
use axum::{extract::State, response::IntoResponse, routing::get, Router};
|
use axum::{
|
||||||
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
extract::{Extension, FromRequestParts, Path, State},
|
||||||
|
http::{request::Parts, Method, StatusCode},
|
||||||
|
response::IntoResponse,
|
||||||
|
routing::{get, post},
|
||||||
|
RequestPartsExt, Router,
|
||||||
|
};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use morethantext::MoreThanText;
|
use morethantext::{ActionType, ErrorType, MoreThanText};
|
||||||
|
use std::{collections::HashMap, convert::Infallible};
|
||||||
use tokio::{spawn, sync::mpsc::channel};
|
use tokio::{spawn, sync::mpsc::channel};
|
||||||
|
use tower_cookies::{Cookie, CookieManagerLayer, Cookies};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
const LOCALHOST: &str = "127.0.0.1";
|
const LOCALHOST: &str = "127.0.0.1";
|
||||||
const SESSION_KEY: &str = "sessionid";
|
const SESSION_KEY: &str = "sessionid";
|
||||||
@ -34,23 +42,78 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn create_app(state: MoreThanText) -> Router {
|
async fn create_app(state: MoreThanText) -> Router {
|
||||||
Router::new().route("/", get(mtt_conn)).with_state(state)
|
Router::new()
|
||||||
|
.route("/", get(mtt_conn))
|
||||||
|
.route("/{document}", get(mtt_conn))
|
||||||
|
.route("/api/{document}", post(mtt_conn).patch(mtt_conn))
|
||||||
|
.layer(CookieManagerLayer::new())
|
||||||
|
.layer(Extension(state.clone()))
|
||||||
|
.with_state(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn mtt_conn(jar: CookieJar, state: State<MoreThanText>) -> impl IntoResponse {
|
#[derive(Clone)]
|
||||||
let sid = match jar.get(SESSION_KEY) {
|
struct SessionID(Uuid);
|
||||||
|
|
||||||
|
impl<S> FromRequestParts<S> for SessionID
|
||||||
|
where
|
||||||
|
S: Send + Sync,
|
||||||
|
{
|
||||||
|
type Rejection = Infallible;
|
||||||
|
|
||||||
|
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||||
|
let Extension(cookies) = parts.extract::<Extension<Cookies>>().await.unwrap();
|
||||||
|
let Extension(mut state) = parts.extract::<Extension<MoreThanText>>().await.unwrap();
|
||||||
|
let req_id = match cookies.get(SESSION_KEY) {
|
||||||
Some(cookie) => Some(cookie.value().to_string()),
|
Some(cookie) => Some(cookie.value().to_string()),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
let sess_info = sid.clone();
|
let requested = req_id.clone();
|
||||||
let (tx, mut rx) = channel(5);
|
let (tx, mut rx) = channel(1);
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
tx.send(state.clone().request(sess_info)).await.unwrap();
|
tx.send(state.validate_session(requested)).await.unwrap();
|
||||||
|
});
|
||||||
|
let id = rx.recv().await.unwrap();
|
||||||
|
if !req_id.is_some_and(|x| x == id.to_string()) {
|
||||||
|
cookies.add(Cookie::new(SESSION_KEY, id.to_string()));
|
||||||
|
}
|
||||||
|
Ok(SessionID(id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn mtt_conn(
|
||||||
|
sess_id: SessionID,
|
||||||
|
method: Method,
|
||||||
|
path: Path<HashMap<String, String>>,
|
||||||
|
state: State<MoreThanText>,
|
||||||
|
body: String,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let (tx, mut rx) = channel(1);
|
||||||
|
let action = match method {
|
||||||
|
Method::GET => ActionType::Get,
|
||||||
|
Method::POST => ActionType::Add,
|
||||||
|
Method::PATCH => ActionType::Update,
|
||||||
|
_ => unreachable!("reouter should prevent this"),
|
||||||
|
};
|
||||||
|
let doc = match path.get("document") {
|
||||||
|
Some(result) => result.clone(),
|
||||||
|
None => "root".to_string(),
|
||||||
|
};
|
||||||
|
spawn(async move {
|
||||||
|
tx.send(state.get_document(sess_id.0, action, doc, body))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
});
|
});
|
||||||
let reply = rx.recv().await.unwrap();
|
let reply = rx.recv().await.unwrap();
|
||||||
let cookie = Cookie::build((SESSION_KEY, reply.get_session().to_string()));
|
let status = match reply.get_error() {
|
||||||
let cookies = jar.add(cookie);
|
Some(err) => match err {
|
||||||
(cookies, reply.get_content())
|
ErrorType::DocumentAlreadyExists => StatusCode::CONFLICT,
|
||||||
|
ErrorType::DocumentInvalidRequest => StatusCode::BAD_REQUEST,
|
||||||
|
ErrorType::DocumentNotFound => StatusCode::NOT_FOUND,
|
||||||
|
// _ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
},
|
||||||
|
None => StatusCode::OK,
|
||||||
|
};
|
||||||
|
(status, reply.get_document())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -58,8 +121,13 @@ mod servers {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
http::{Request, StatusCode},
|
http::{
|
||||||
|
header::{COOKIE, SET_COOKIE},
|
||||||
|
Method, Request,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
use http_body_util::BodyExt;
|
||||||
|
use serde_json::json;
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@ -70,7 +138,7 @@ mod servers {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
let sessid = format!("{:?}", response.headers().get("set-cookie").unwrap());
|
let sessid = format!("{:?}", response.headers().get(SET_COOKIE).unwrap());
|
||||||
assert!(sessid.contains(SESSION_KEY), "did not set session id");
|
assert!(sessid.contains(SESSION_KEY), "did not set session id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +152,7 @@ mod servers {
|
|||||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let sessid = format!("{:?}", response.headers().get("set-cookie").unwrap());
|
let sessid = format!("{:?}", response.headers().get(SET_COOKIE).unwrap());
|
||||||
assert!(
|
assert!(
|
||||||
!holder.contains(&sessid),
|
!holder.contains(&sessid),
|
||||||
"found duplicate entry: {:?}",
|
"found duplicate entry: {:?}",
|
||||||
@ -95,17 +163,213 @@ mod servers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn receive_file_not_found() {
|
async fn cookie_only_issued_once() {
|
||||||
let app = create_app(MoreThanText::new()).await;
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let initial = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(initial.status(), StatusCode::OK);
|
||||||
|
let sessid = initial.headers().get(SET_COOKIE).unwrap();
|
||||||
|
let request = Request::builder()
|
||||||
|
.uri("/")
|
||||||
|
.header(COOKIE, sessid.clone())
|
||||||
|
.body(Body::empty())
|
||||||
|
.unwrap();
|
||||||
|
let response = app.clone().oneshot(request).await.unwrap();
|
||||||
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
match response.headers().get(SET_COOKIE) {
|
||||||
|
Some(info) => assert!(false, "first pass: {:?}, second pass: {:?}", sessid, info),
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn receive_file_not_found() {
|
||||||
|
let uri = "/something";
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let response = app
|
||||||
|
.oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
"'{}' should not exist",
|
||||||
|
uri
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn add_new_page() {
|
||||||
|
let base = "/something".to_string();
|
||||||
|
let api = format!("/api{}", &base);
|
||||||
|
let content = format!("content-{}", Uuid::new_v4());
|
||||||
|
let document = json!({
|
||||||
|
"template": content.clone()
|
||||||
|
});
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri(&api)
|
||||||
|
.body(document.to_string())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::OK,
|
||||||
|
"failed to post ro {:?}",
|
||||||
|
api
|
||||||
|
);
|
||||||
|
let response = app
|
||||||
|
.oneshot(Request::builder().uri(&base).body(Body::empty()).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::OK,
|
||||||
|
"failed to get ro {:?}",
|
||||||
|
base
|
||||||
|
);
|
||||||
|
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||||
|
assert_eq!(body, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn cannot_add_duplicate_document_names() {
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let document = json!({
|
||||||
|
"template": "something completely different."
|
||||||
|
});
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri("/api/root")
|
||||||
|
.body(document.to_string())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::CONFLICT,
|
||||||
|
"do not allow post to existing documents"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn invalid_json() {
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri("/api/something")
|
||||||
|
.body("Some invalid json.".to_string())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"do not allow post to existing documents"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn post_with_missing_document() {
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri("/api/something")
|
||||||
|
.body("{}".to_string())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"do not allow post to existing documents"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn patch_root() {
|
||||||
|
let content = format!("content-{}", Uuid::new_v4());
|
||||||
|
let document = json!({
|
||||||
|
"template": content.clone()
|
||||||
|
});
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(Method::PATCH)
|
||||||
|
.uri("/api/root".to_string())
|
||||||
|
.body(document.to_string())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::OK,
|
||||||
|
"failed to patch /api/root",
|
||||||
|
);
|
||||||
let response = app
|
let response = app
|
||||||
.oneshot(
|
.oneshot(
|
||||||
Request::builder()
|
Request::builder()
|
||||||
.uri("/isomething")
|
.uri("/".to_string())
|
||||||
.body(Body::empty())
|
.body(Body::empty())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::OK,
|
||||||
|
"failed to get to home page",
|
||||||
|
);
|
||||||
|
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||||
|
assert_eq!(body, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn patch_missing_page() {
|
||||||
|
let content = format!("content-{}", Uuid::new_v4());
|
||||||
|
let document = json!({
|
||||||
|
"template": content.clone()
|
||||||
|
});
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(Method::PATCH)
|
||||||
|
.uri("/api/something".to_string())
|
||||||
|
.body(document.to_string())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
"failed to patch /api/somethingt",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
70
src/queue.rs
70
src/queue.rs
@ -1,4 +1,4 @@
|
|||||||
use crate::{client::Request, field::Field};
|
use crate::{field::Field, ErrorType};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
sync::{mpsc::Sender, Arc, RwLock},
|
sync::{mpsc::Sender, Arc, RwLock},
|
||||||
@ -7,9 +7,10 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
pub enum MsgType {
|
pub enum MsgType {
|
||||||
ClientRequest,
|
ActionOk,
|
||||||
Document,
|
Document,
|
||||||
DocumentRequest,
|
DocumentRequest,
|
||||||
|
Error,
|
||||||
SessionValidate,
|
SessionValidate,
|
||||||
SessionValidated,
|
SessionValidated,
|
||||||
Time,
|
Time,
|
||||||
@ -47,6 +48,12 @@ impl Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reply_with_error(&self, error: ErrorType) -> Self {
|
||||||
|
let mut reply = self.reply(MsgType::Error);
|
||||||
|
reply.add_data("error_type", error);
|
||||||
|
reply
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_msg_type(&self) -> &MsgType {
|
pub fn get_msg_type(&self) -> &MsgType {
|
||||||
&self.msg_type
|
&self.msg_type
|
||||||
}
|
}
|
||||||
@ -70,23 +77,15 @@ impl Message {
|
|||||||
pub fn get_id(&self) -> Uuid {
|
pub fn get_id(&self) -> Uuid {
|
||||||
self.id.clone()
|
self.id.clone()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Request> for Message {
|
pub fn reset_id(&mut self) {
|
||||||
fn from(value: Request) -> Self {
|
self.id = Uuid::new_v4();
|
||||||
let mut msg = Message::new(MsgType::ClientRequest);
|
|
||||||
match value.session {
|
|
||||||
Some(id) => msg.add_data("sess_id", id),
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
msg
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod messages {
|
mod messages {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::client::requests::{get_root_document, get_root_document_eith_session};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn new_message() {
|
fn new_message() {
|
||||||
@ -115,11 +114,11 @@ mod messages {
|
|||||||
let mut msg = Message::new(MsgType::SessionValidate);
|
let mut msg = Message::new(MsgType::SessionValidate);
|
||||||
msg.id = id.clone();
|
msg.id = id.clone();
|
||||||
msg.add_data("test", "test");
|
msg.add_data("test", "test");
|
||||||
let data = MsgType::ClientRequest;
|
let data = MsgType::SessionValidate;
|
||||||
let result = msg.reply(data);
|
let result = msg.reply(data);
|
||||||
assert_eq!(result.id, id);
|
assert_eq!(result.id, id);
|
||||||
match result.msg_type {
|
match result.msg_type {
|
||||||
MsgType::ClientRequest => {}
|
MsgType::SessionValidate => {}
|
||||||
_ => unreachable!("should have been a registration request"),
|
_ => unreachable!("should have been a registration request"),
|
||||||
}
|
}
|
||||||
assert!(result.data.is_empty());
|
assert!(result.data.is_empty());
|
||||||
@ -182,23 +181,38 @@ mod messages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_request_no_session() {
|
fn reset_msg_id() {
|
||||||
let req = get_root_document();
|
let mut msg = Message::new(MsgType::Time);
|
||||||
let msg: Message = req.into();
|
let id = msg.get_id();
|
||||||
assert!(
|
msg.reset_id();
|
||||||
msg.get_data("sess_id").is_none(),
|
assert_ne!(msg.get_id(), id);
|
||||||
"should not have a session id"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_request_with_session() {
|
fn error_reply() {
|
||||||
let id = Uuid::new_v4();
|
let msg = Message::new(MsgType::Time);
|
||||||
let req = get_root_document_eith_session(id.clone());
|
let errors = [
|
||||||
let msg: Message = req.into();
|
ErrorType::DocumentAlreadyExists,
|
||||||
match msg.get_data("sess_id") {
|
ErrorType::DocumentInvalidRequest,
|
||||||
Some(result) => assert_eq!(result.to_uuid().unwrap(), id),
|
];
|
||||||
None => unreachable!("should return an id"),
|
for error in errors.into_iter() {
|
||||||
|
let reply = msg.reply_with_error(error.clone());
|
||||||
|
assert_eq!(reply.get_id(), msg.get_id());
|
||||||
|
assert_eq!(
|
||||||
|
format!("{:?}", reply.get_msg_type()),
|
||||||
|
format!("{:?}", MsgType::Error)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
format!(
|
||||||
|
"{:?}",
|
||||||
|
reply
|
||||||
|
.get_data("error_type")
|
||||||
|
.unwrap()
|
||||||
|
.to_error_type()
|
||||||
|
.unwrap()
|
||||||
|
),
|
||||||
|
format!("{:?}", error)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,13 +156,19 @@ impl Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod sessions {
|
pub mod sessions {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::queue::{Message, MsgType};
|
use crate::queue::{Message, MsgType};
|
||||||
use std::{sync::mpsc::channel, time::Duration};
|
use std::{sync::mpsc::channel, time::Duration};
|
||||||
|
|
||||||
static TIMEOUT: Duration = Duration::from_millis(500);
|
static TIMEOUT: Duration = Duration::from_millis(500);
|
||||||
|
|
||||||
|
pub fn create_validated_reply(msg: Message) -> Message {
|
||||||
|
let mut reply = msg.reply(MsgType::SessionValidated);
|
||||||
|
reply.add_data("sess_id", Uuid::new_v4());
|
||||||
|
reply
|
||||||
|
}
|
||||||
|
|
||||||
fn setup_session(listen_for: Vec<MsgType>) -> (Queue, Receiver<Message>) {
|
fn setup_session(listen_for: Vec<MsgType>) -> (Queue, Receiver<Message>) {
|
||||||
let queue = Queue::new();
|
let queue = Queue::new();
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
|
17
src/utils.rs
17
src/utils.rs
@ -1,17 +0,0 @@
|
|||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub struct GenID;
|
|
||||||
|
|
||||||
impl GenID {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
GenID {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for GenID {
|
|
||||||
type Item = Uuid;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
Some(Uuid::new_v4())
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user