From ce66c1618528b74bdcaa889e3dc1698e686596bc Mon Sep 17 00:00:00 2001 From: Katalina Okano Date: Fri, 25 Nov 2022 18:36:33 -0500 Subject: [PATCH] add graphql support --- Cargo.lock | 528 +++++++++++++++++++++++++++++- services/api/Cargo.toml | 5 +- services/api/src/classes.rs | 77 ++--- services/api/src/cors.rs | 2 +- services/api/src/graphql/mod.rs | 65 ++++ services/api/src/graphql/types.rs | 271 +++++++++++++++ services/api/src/main.rs | 18 +- services/api/src/population.rs | 28 +- services/api/src/redispool.rs | 2 +- 9 files changed, 919 insertions(+), 77 deletions(-) create mode 100644 services/api/src/graphql/mod.rs create mode 100644 services/api/src/graphql/types.rs diff --git a/Cargo.lock b/Cargo.lock index d77b369..e96a6ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,16 +37,34 @@ dependencies = [ "subtle", ] +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "api" version = "0.1.0" dependencies = [ + "juniper", + "juniper_rocket", + "once_cell", "rocket", "rocket_db_pools", "serde", "serde_json", ] +[[package]] +name = "ascii" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" + [[package]] name = "async-stream" version = "0.3.3" @@ -132,6 +150,29 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bson" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de0aa578035b938855a710ba58d43cfb4d435f3619f99236fb35922a574d6cb1" +dependencies = [ + "base64", + "chrono", + "hex", + "lazy_static", + "linked-hash-map", + "rand 0.7.3", + "serde", + "serde_json", + "uuid", +] + +[[package]] +name = "bumpalo" +version = "3.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" + [[package]] name = "byteorder" version = "1.4.3" @@ -156,6 +197,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-integer", + "num-traits", + "time 0.1.44", + "wasm-bindgen", + "winapi", +] + [[package]] name = "cipher" version = "0.4.3" @@ -166,6 +222,29 @@ dependencies = [ "inout", ] +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + +[[package]] +name = "combine" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680" +dependencies = [ + "ascii", + "byteorder", + "either", + "memchr", + "unreachable", +] + [[package]] name = "combine" version = "4.6.6" @@ -191,10 +270,10 @@ dependencies = [ "hkdf", "hmac", "percent-encoding", - "rand", + "rand 0.8.5", "sha2", "subtle", - "time", + "time 0.3.17", "version_check", ] @@ -230,7 +309,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", - "rand_core", + "rand_core 0.6.4", "typenum", ] @@ -243,6 +322,50 @@ dependencies = [ "cipher", ] +[[package]] +name = "cxx" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4a41a86530d0fe7f5d9ea779916b7cadd2d4f9add748b99c2c029cbbdfaf453" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06416d667ff3e3ad2df1cd8cd8afae5da26cf9cec4d0825040f88b5ca659a2f0" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "820a9a2af1669deeef27cb271f476ffd196a2c4b6731336011e0ba63e2c7cf71" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a08a6e2fcc370a089ad3b4aaf54db3b1b4cee38ddabce5896b33eb693275f470" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "deadpool" version = "0.9.5" @@ -275,6 +398,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "derive_utils" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "532b4c15dccee12c7044f1fcad956e98410860b22231e44a3b827464797ca7bf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "devise" version = "0.3.1" @@ -418,6 +552,17 @@ version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac" +[[package]] +name = "futures-enum" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3422d14de7903a52e9dbc10ae05a7e14445ec61890100e098754e120b2bd7b1e" +dependencies = [ + "derive_utils", + "quote", + "syn", +] + [[package]] name = "futures-executor" version = "0.3.25" @@ -499,6 +644,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + [[package]] name = "getrandom" version = "0.2.8" @@ -507,7 +663,7 @@ checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -526,6 +682,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "graphql-parser" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1abd4ce5247dfc04a03ccde70f87a048458c9356c7e41d21ad8c407b3dde6f2" +dependencies = [ + "combine 3.8.1", + "thiserror", +] + [[package]] name = "h2" version = "0.3.15" @@ -560,6 +726,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "hkdf" version = "0.12.3" @@ -636,6 +808,30 @@ dependencies = [ "want", ] +[[package]] +name = "iana-time-zone" +version = "0.1.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "idna" version = "0.3.0" @@ -687,6 +883,61 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" +[[package]] +name = "js-sys" +version = "0.3.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "juniper" +version = "0.15.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f478f229a8ab52ff242f3250c8b3b8fe0a59b5b934f9706b7bdbc980991a7b6" +dependencies = [ + "async-trait", + "bson", + "chrono", + "fnv", + "futures", + "futures-enum", + "graphql-parser", + "indexmap", + "juniper_codegen", + "serde", + "smartstring", + "static_assertions", + "url", + "uuid", +] + +[[package]] +name = "juniper_codegen" +version = "0.15.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aee97671061ad50301ba077d054d295e01d31a1868fbd07902db651f987e71db" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "juniper_rocket" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a431e6f03bc31bd74498a837e87ddf635deef3c1a2026e59680dec2552c84c28" +dependencies = [ + "futures", + "juniper", + "rocket", + "serde_json", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -699,6 +950,21 @@ version = "0.2.137" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" +[[package]] +name = "link-cplusplus" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369" +dependencies = [ + "cc", +] + +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "lock_api" version = "0.4.9" @@ -762,7 +1028,7 @@ checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.42.0", ] @@ -814,6 +1080,25 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.14.0" @@ -975,6 +1260,30 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.47" @@ -1017,6 +1326,19 @@ dependencies = [ "scheduled-thread-pool", ] +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc", +] + [[package]] name = "rand" version = "0.8.5" @@ -1024,8 +1346,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "rand_chacha", - "rand_core", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", ] [[package]] @@ -1035,7 +1367,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", ] [[package]] @@ -1044,7 +1385,16 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom", + "getrandom 0.2.8", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", ] [[package]] @@ -1055,7 +1405,7 @@ checksum = "571c252c68d09a2ad3e49edd14e9ee48932f3e0f27b06b4ea4c9b2a706d31103" dependencies = [ "async-trait", "bytes", - "combine", + "combine 4.6.6", "futures-util", "itoa", "percent-encoding", @@ -1072,7 +1422,7 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "513b3649f1a111c17954296e4a3b9eecb108b766c803e2b99f179ebe27005985" dependencies = [ - "combine", + "combine 4.6.6", "itoa", "percent-encoding", "r2d2", @@ -1180,7 +1530,7 @@ dependencies = [ "num_cpus", "parking_lot", "pin-project-lite", - "rand", + "rand 0.8.5", "ref-cast", "rocket_codegen", "rocket_http", @@ -1188,7 +1538,7 @@ dependencies = [ "serde_json", "state", "tempfile", - "time", + "time 0.3.17", "tokio", "tokio-stream", "tokio-util", @@ -1258,7 +1608,7 @@ dependencies = [ "smallvec", "stable-pattern", "state", - "time", + "time 0.3.17", "tokio", "uncased", ] @@ -1306,6 +1656,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898" + [[package]] name = "security-framework" version = "2.7.0" @@ -1351,10 +1707,11 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.88" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e8b3801309262e8184d9687fb697586833e939767aea0dda89f5a8e650e8bd7" +checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db" dependencies = [ + "indexmap", "itoa", "ryu", "serde", @@ -1421,6 +1778,15 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +[[package]] +name = "smartstring" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e714dff2b33f2321fdcd475b71cec79781a692d846f37f415fb395a1d2bcd48e" +dependencies = [ + "static_assertions", +] + [[package]] name = "socket2" version = "0.4.7" @@ -1455,6 +1821,12 @@ dependencies = [ "loom", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "subtle" version = "2.4.1" @@ -1494,6 +1866,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.37" @@ -1523,6 +1904,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "time" version = "0.3.17" @@ -1740,7 +2132,7 @@ dependencies = [ "httparse", "log", "native-tls", - "rand", + "rand 0.8.5", "sha-1", "thiserror", "url", @@ -1793,6 +2185,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "unicode-xid" version = "0.2.4" @@ -1809,6 +2207,15 @@ dependencies = [ "subtle", ] +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +dependencies = [ + "void", +] + [[package]] name = "url" version = "2.3.1" @@ -1826,6 +2233,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" + [[package]] name = "valuable" version = "0.1.0" @@ -1844,6 +2257,12 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + [[package]] name = "want" version = "0.3.0" @@ -1854,12 +2273,78 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" + [[package]] name = "websocket" version = "0.1.0" @@ -1892,6 +2377,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/services/api/Cargo.toml b/services/api/Cargo.toml index 09b498c..5677323 100644 --- a/services/api/Cargo.toml +++ b/services/api/Cargo.toml @@ -9,4 +9,7 @@ edition = "2021" rocket = { version = "0.5.0-rc.2", features = ["json"] } rocket_db_pools = { version = "0.1.0-rc.2", features = [ "deadpool_redis" ] } serde_json = "1.0.88" -serde = "1.0.147" \ No newline at end of file +serde = "1.0.147" +juniper = "0.15.10" +juniper_rocket = "0.8.2" +once_cell = "1.16.0" \ No newline at end of file diff --git a/services/api/src/classes.rs b/services/api/src/classes.rs index f8494d2..a48e7d7 100644 --- a/services/api/src/classes.rs +++ b/services/api/src/classes.rs @@ -1,3 +1,4 @@ +use crate::redispool::RedisPool; use core::time; use rocket_db_pools::deadpool_redis::redis::{pipe, AsyncCommands}; use rocket_db_pools::Connection; @@ -5,8 +6,6 @@ use serde::{Deserialize, Serialize}; use std::ops::Sub; use std::time::SystemTime; -use crate::redispool::RedisPool; - #[derive(Serialize, Deserialize, Debug)] struct ClassCounts { world_id: String, @@ -14,13 +13,13 @@ struct ClassCounts { } #[derive(Serialize, Deserialize, Debug)] -struct Classes { - light_assault: u32, - engineer: u32, - combat_medic: u32, - heavy_assault: u32, - infiltrator: u32, - max: u32, +pub struct Classes { + light_assault: i32, + engineer: i32, + combat_medic: i32, + heavy_assault: i32, + infiltrator: i32, + max: i32, } #[get("/w//classes")] @@ -34,21 +33,36 @@ pub async fn get_classes(world_id: String, mut con: Connection) -> se Err(_) => {} } + let classes = fetch_classes(world_id.clone(), &mut con).await; + + // I hate this but it's fast??? + // The type only allows 12 at a time. + + let response = ClassCounts { world_id, classes }; + + let out = json!(response); + + con.set_ex::(cache_key, out.to_string(), 5) + .await + .unwrap(); + + out +} + +pub async fn fetch_classes(world_id: String, con: &mut Connection) -> Classes { let filter_timestamp = SystemTime::now() .sub(time::Duration::from_secs(60 * 15)) .duration_since(SystemTime::UNIX_EPOCH) .unwrap() .as_secs(); - // I hate this but it's fast??? - // The type only allows 12 at a time. let (light_assault, engineer, combat_medic, heavy_assault, infiltrator, max): ( - u32, - u32, - u32, - u32, - u32, - u32, + i32, + i32, + i32, + i32, + i32, + i32, ) = pipe() .zcount( format!("c:{}/{}", world_id, "light_assault"), @@ -80,27 +94,16 @@ pub async fn get_classes(world_id: String, mut con: Connection) -> se filter_timestamp, "+inf", ) - .query_async(&mut *con) + .query_async(&mut **con) .await .unwrap(); - let response = ClassCounts { - world_id, - classes: Classes { - light_assault, - engineer, - combat_medic, - heavy_assault, - infiltrator, - max, - }, - }; - - let out = json!(response); - - con.set_ex::(cache_key, out.to_string(), 5) - .await - .unwrap(); - - out + Classes { + light_assault, + engineer, + combat_medic, + heavy_assault, + infiltrator, + max, + } } diff --git a/services/api/src/cors.rs b/services/api/src/cors.rs index 527dbae..9367656 100644 --- a/services/api/src/cors.rs +++ b/services/api/src/cors.rs @@ -15,7 +15,7 @@ impl Fairing for CORS { async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) { response.set_header(Header::new("Access-Control-Allow-Origin", "*")); - response.set_header(Header::new("Access-Control-Allow-Methods", "GET")); + response.set_header(Header::new("Access-Control-Allow-Methods", "GET, POST")); response.set_header(Header::new("Access-Control-Allow-Headers", "*")); response.set_header(Header::new("Access-Control-Allow-Credentials", "false")); } diff --git a/services/api/src/graphql/mod.rs b/services/api/src/graphql/mod.rs new file mode 100644 index 0000000..d97c669 --- /dev/null +++ b/services/api/src/graphql/mod.rs @@ -0,0 +1,65 @@ +use crate::redispool::RedisPool; + +use self::types::World; +use juniper::{graphql_object, FieldResult, ID}; +use rocket::response::content::RawHtml; + +pub mod types; + +pub struct Context { + con: RedisPool, +} + +impl juniper::Context for Context {} + +#[get("/graphiql")] +pub fn graphiql() -> RawHtml { + juniper_rocket::graphiql_source("/graphql", None) +} + +#[get("/")] +pub fn playground() -> RawHtml { + juniper_rocket::playground_source("/graphql", None) +} + +#[post("/", data = "")] +pub async fn post_graphql( + query: juniper_rocket::GraphQLRequest, + schema: &rocket::State, + con: &RedisPool, +) -> juniper_rocket::GraphQLResponse { + query.execute(&*schema, &Context { con: con.clone() }).await +} + +#[get("/?")] +pub async fn get_graphql( + query: juniper_rocket::GraphQLRequest, + schema: &rocket::State, + con: &RedisPool, +) -> juniper_rocket::GraphQLResponse { + query.execute(&*schema, &Context { con: con.clone() }).await +} + +pub struct Query; + +#[graphql_object(context = Context)] +impl Query { + fn world(id: ID) -> FieldResult> { + Ok(Some(World { id })) + } +} + +pub type Schema = juniper::RootNode< + 'static, + Query, + juniper::EmptyMutation, + juniper::EmptySubscription, +>; + +pub fn schema() -> Schema { + Schema::new( + Query, + juniper::EmptyMutation::::new(), + juniper::EmptySubscription::::new(), + ) +} diff --git a/services/api/src/graphql/types.rs b/services/api/src/graphql/types.rs new file mode 100644 index 0000000..6b3645d --- /dev/null +++ b/services/api/src/graphql/types.rs @@ -0,0 +1,271 @@ +use juniper::graphql_object; +use once_cell::sync::Lazy; +use rocket_db_pools::deadpool_redis::redis::{cmd, pipe}; +use std::{ + collections::HashMap, + ops::Sub, + time::{Duration, SystemTime}, +}; + +static WORLD_ID_TO_NAME: Lazy> = Lazy::new(|| { + HashMap::from([ + ("1", "Connery"), + ("10", "Miller"), + ("13", "Cobalt"), + ("17", "Emerald"), + ("19", "Jaeger"), + ("40", "SolTech"), + ("1000", "Genudine"), + ("2000", "Ceres"), + ]) +}); + +#[derive(Clone, Debug)] +pub struct World { + pub id: juniper::ID, +} + +#[graphql_object(context = super::Context)] +impl World { + pub fn name(&self) -> String { + WORLD_ID_TO_NAME + .get(&self.id.to_string().as_str()) + .unwrap_or(&"Unknown") + .to_string() + } + + pub async fn population(&self, context: &mut super::Context) -> i32 { + let mut con = (*context).con.get().await.unwrap(); + let id = self.id.to_string(); + + let filter_timestamp = SystemTime::now() + .sub(Duration::from_secs(60 * 15)) + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs(); + + let (vs, nc, tr, ns): (i32, i32, i32, i32) = pipe() + .zcount(format!("wp:{}/{}", id, 1), filter_timestamp, "+inf") + .zcount(format!("wp:{}/{}", id, 2), filter_timestamp, "+inf") + .zcount(format!("wp:{}/{}", id, 3), filter_timestamp, "+inf") + .zcount(format!("wp:{}/{}", id, 4), filter_timestamp, "+inf") + .query_async(&mut con) + .await + .unwrap(); + + tr + vs + nc + ns + } + + pub async fn faction_population(&self) -> FactionPopulation { + FactionPopulation { + world_id: self.id.clone(), + } + } + + pub async fn vehicles(&self) -> Vehicles { + Vehicles { + world_id: self.id.clone(), + } + } +} + +pub struct FactionPopulation { + world_id: juniper::ID, +} + +impl FactionPopulation { + async fn by_faction(&self, context: &super::Context, world_id: String, faction: i32) -> i32 { + let mut con = (*context).con.get().await.unwrap(); + + let filter_timestamp = SystemTime::now() + .sub(Duration::from_secs(60 * 15)) + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs(); + + cmd("ZCOUNT") + .arg(format!("wp:{}/{}", world_id, faction)) + .arg(filter_timestamp) + .arg("+inf") + .query_async(&mut con) + .await + .unwrap() + } +} + +#[graphql_object(context = super::Context)] +#[graphql(description = "The population of each faction on a world")] +impl FactionPopulation { + async fn vs(&self, context: &super::Context) -> i32 { + self.by_faction(context, self.world_id.to_string(), 1).await + } + async fn nc(&self, context: &super::Context) -> i32 { + self.by_faction(context, self.world_id.to_string(), 2).await + } + async fn tr(&self, context: &super::Context) -> i32 { + self.by_faction(context, self.world_id.to_string(), 3).await + } + async fn ns(&self, context: &super::Context) -> i32 { + self.by_faction(context, self.world_id.to_string(), 4).await + } +} + +pub struct Vehicles { + world_id: juniper::ID, +} + +impl Vehicles { + async fn get_vehicle( + &self, + context: &super::Context, + world_id: String, + vehicle_name: &str, + ) -> i32 { + let mut con = (*context).con.get().await.unwrap(); + + let filter_timestamp = SystemTime::now() + .sub(Duration::from_secs(60 * 15)) + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs(); + + cmd("ZCOUNT") + .arg(format!("v:{}/{}", world_id, vehicle_name)) + .arg(filter_timestamp) + .arg("+inf") + .query_async(&mut con) + .await + .unwrap() + } +} + +#[graphql_object(context = super::Context)] +#[graphql(description = "The count of active vehicles on a world")] +impl Vehicles { + // Transporters + async fn flash(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "flash") + .await + } + async fn sunderer(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "sunderer") + .await + } + async fn ant(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "ant") + .await + } + async fn harasser(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "harasser") + .await + } + async fn javelin(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "javelin") + .await + } + + // Tanks + async fn lightning(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "lightning") + .await + } + async fn prowler(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "prowler") + .await + } + async fn vanguard(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "vanguard") + .await + } + async fn magrider(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "magrider") + .await + } + async fn chimera(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "chimera") + .await + } + + // Air + async fn mosquito(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "mosquito") + .await + } + async fn liberator(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "liberator") + .await + } + async fn galaxy(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "galaxy") + .await + } + async fn valkyrie(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "valkyrie") + .await + } + async fn reaver(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "reaver") + .await + } + async fn scythe(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "scythe") + .await + } + async fn dervish(&self, context: &super::Context) -> i32 { + self.get_vehicle(context, self.world_id.to_string(), "dervish") + .await + } +} + +pub struct Classes { + pub world_id: juniper::ID, +} + +impl Classes { + async fn get_class(&self, context: &super::Context, world_id: String, class_name: &str) -> i32 { + let mut con = (*context).con.get().await.unwrap(); + + let filter_timestamp = SystemTime::now() + .sub(Duration::from_secs(60 * 15)) + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs(); + + cmd("ZCOUNT") + .arg(format!("c:{}/{}", world_id, class_name)) + .arg(filter_timestamp) + .arg("+inf") + .query_async(&mut con) + .await + .unwrap() + } +} + +#[graphql_object(context = super::Context)] +#[graphql(description = "The count of active classes on a world")] +impl Classes { + async fn infiltrator(&self, context: &super::Context) -> i32 { + self.get_class(context, self.world_id.to_string(), "infiltrator") + .await + } + async fn light_assault(&self, context: &super::Context) -> i32 { + self.get_class(context, self.world_id.to_string(), "light_assault") + .await + } + async fn combat_medic(&self, context: &super::Context) -> i32 { + self.get_class(context, self.world_id.to_string(), "combat_medic") + .await + } + async fn engineer(&self, context: &super::Context) -> i32 { + self.get_class(context, self.world_id.to_string(), "engineer") + .await + } + async fn heavy_assault(&self, context: &super::Context) -> i32 { + self.get_class(context, self.world_id.to_string(), "heavy_assault") + .await + } + async fn max(&self, context: &super::Context) -> i32 { + self.get_class(context, self.world_id.to_string(), "max") + .await + } +} diff --git a/services/api/src/main.rs b/services/api/src/main.rs index 083dc87..606e6e6 100644 --- a/services/api/src/main.rs +++ b/services/api/src/main.rs @@ -1,5 +1,6 @@ pub mod classes; pub mod cors; +pub mod graphql; pub mod population; pub mod redispool; pub mod vehicles; @@ -10,18 +11,11 @@ use rocket::{error, Build, Rocket}; use rocket_db_pools::deadpool_redis::redis::{cmd, pipe}; use rocket_db_pools::{Connection, Database}; -use serde::{Deserialize, Serialize}; - #[macro_use] extern crate rocket; #[macro_use] extern crate serde_json; -#[derive(Serialize, Deserialize)] -struct IncomingHeaders { - host: String, -} - fn hello_world(host: String, world_id: &str) -> serde_json::Value { json!({ "population": format!("https://{}/w/{}", host, world_id), @@ -99,6 +93,7 @@ fn rocket() -> Rocket { } rocket })) + .manage(graphql::schema()) .mount( "/", routes![ @@ -109,4 +104,13 @@ fn rocket() -> Rocket { classes::get_classes, ], ) + .mount( + "/graphql", + routes![ + graphql::graphiql, + graphql::playground, + graphql::get_graphql, + graphql::post_graphql + ], + ) } diff --git a/services/api/src/population.rs b/services/api/src/population.rs index 1289b2a..b5a8fa7 100644 --- a/services/api/src/population.rs +++ b/services/api/src/population.rs @@ -8,17 +8,17 @@ use crate::redispool::RedisPool; #[derive(Serialize, Deserialize, Debug)] pub struct Factions { - tr: u32, - nc: u32, - vs: u32, - ns: u32, + pub tr: i32, + pub nc: i32, + pub vs: i32, + pub ns: i32, } #[derive(Serialize, Deserialize, Debug)] pub struct WorldPopulation { - world_id: u32, - total: u32, - factions: Factions, + world_id: i32, + pub total: i32, + pub factions: Factions, } #[derive(Serialize, Deserialize, Debug)] @@ -28,28 +28,30 @@ pub struct MultipleWorldPopulation { #[get("/w/")] pub async fn get_world_pop(world_id: String, mut con: Connection) -> serde_json::Value { + json!(fetch_world_pop(world_id, &mut con).await) +} + +pub async fn fetch_world_pop(world_id: String, con: &mut Connection) -> WorldPopulation { let filter_timestamp = SystemTime::now() .sub(time::Duration::from_secs(60 * 15)) .duration_since(SystemTime::UNIX_EPOCH) .unwrap() .as_secs(); - let (vs, nc, tr, ns): (u32, u32, u32, u32) = pipe() + let (vs, nc, tr, ns): (i32, i32, i32, i32) = pipe() .zcount(format!("wp:{}/{}", world_id, 1), filter_timestamp, "+inf") .zcount(format!("wp:{}/{}", world_id, 2), filter_timestamp, "+inf") .zcount(format!("wp:{}/{}", world_id, 3), filter_timestamp, "+inf") .zcount(format!("wp:{}/{}", world_id, 4), filter_timestamp, "+inf") - .query_async(&mut *con) + .query_async(&mut **con) .await .unwrap(); let total = tr + vs + nc; - let response = WorldPopulation { + WorldPopulation { world_id: world_id.parse().unwrap(), total, factions: Factions { tr, nc, vs, ns }, - }; - - json!(response) + } } diff --git a/services/api/src/redispool.rs b/services/api/src/redispool.rs index 65aa6b7..f2de592 100644 --- a/services/api/src/redispool.rs +++ b/services/api/src/redispool.rs @@ -1,5 +1,5 @@ use rocket_db_pools::{deadpool_redis, Database}; -#[derive(Database)] +#[derive(Database, Clone)] #[database("redis")] pub struct RedisPool(deadpool_redis::Pool);