diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 958561e..023371d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,12 +8,6 @@ on: jobs: build: runs-on: "ubuntu-latest" - services: - redis: - image: redis:7 - ports: - - 6379:6379 - steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 873aa8d..529f2e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.spin/ tally/target/* scoreboard/target/* highscore/target/* diff --git a/Makefile b/Makefile index fe5112b..260118c 100644 --- a/Makefile +++ b/Makefile @@ -6,14 +6,6 @@ build: serve: ./serve.sh -.PHONY: start-redis -start-redis: - @docker start fw-redis &>/dev/null || docker run -p 6379:6379 --name fw-redis redis:7 & - -.PHONY: stop-redis -stop-redis: - @docker stop fw-redis &>/dev/null - .PHONY: test-server test-server: - ./tests/test-server.sh \ No newline at end of file + ./tests/test-server.sh diff --git a/README.md b/README.md index 718928c..b3f2964 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ Finicky Whiskers is comprised of a handful of microservices. Click on each item below to see more details around a particular microservice. -- [morsel_event](./morsel_event/README.md) - [redirect](./redirect/README.md) - [scoreboard](./scoreboard/README.md) - [session](./session/README.md) @@ -37,15 +36,6 @@ make build-session ## To Run -Finicky Whiskers depends on a Redis instance to run. The default connection -string is `redis://localhost:6379`. - -If you have Docker installed, you can start a redis container like so: - -```console -make start-redis -``` - The following command will serve the Finicky Whiskers site locally: ```console @@ -54,12 +44,6 @@ make serve This will run the game at [http://127.0.0.1:3000](http://127.0.0.1:3000) -When finished, the following command will stop the redis container: - -```console -make stop-redis -``` - ## To Test The following command will serve the site and then run the integration test diff --git a/components/highscore.wasm b/components/highscore.wasm index 7b2c721..aa9466f 100644 Binary files a/components/highscore.wasm and b/components/highscore.wasm differ diff --git a/components/morsel_event.wasm b/components/morsel_event.wasm deleted file mode 100644 index 7815df1..0000000 Binary files a/components/morsel_event.wasm and /dev/null differ diff --git a/components/redirect.wasm b/components/redirect.wasm index e4d41ad..6aaf78c 100644 Binary files a/components/redirect.wasm and b/components/redirect.wasm differ diff --git a/components/reset.wasm b/components/reset.wasm new file mode 100755 index 0000000..2379cfd Binary files /dev/null and b/components/reset.wasm differ diff --git a/components/scoreboard.wasm b/components/scoreboard.wasm index 98980ff..4d3f612 100644 Binary files a/components/scoreboard.wasm and b/components/scoreboard.wasm differ diff --git a/components/tally.wasm b/components/tally.wasm index 2357cf8..a207e02 100755 Binary files a/components/tally.wasm and b/components/tally.wasm differ diff --git a/highscore/Cargo.lock b/highscore/Cargo.lock index 4e3641e..0670c17 100644 --- a/highscore/Cargo.lock +++ b/highscore/Cargo.lock @@ -114,7 +114,7 @@ dependencies = [ "serde", "serde_json", "spin-sdk", - "wit-bindgen-rust 0.1.0", + "wit-bindgen-rust", ] [[package]] @@ -266,6 +266,16 @@ dependencies = [ "getrandom", ] +[[package]] +name = "routefinder" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94f8f99b10dedd317514253dda1fa7c14e344aac96e1f78149a64879ce282aca" +dependencies = [ + "smartcow", + "smartstring", +] + [[package]] name = "rusty_ulid" version = "1.0.0" @@ -315,10 +325,30 @@ dependencies = [ "serde", ] +[[package]] +name = "smartcow" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "656fcb1c1fca8c4655372134ce87d8afdf5ec5949ebabe8d314be0141d8b5da2" +dependencies = [ + "smartstring", +] + +[[package]] +name = "smartstring" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29" +dependencies = [ + "autocfg", + "static_assertions", + "version_check", +] + [[package]] name = "spin-macro" version = "0.1.0" -source = "git+https://github.com/fermyon/spin?tag=v0.6.0#12a503794ff475b38d28bda9cb0683013bf93480" +source = "git+https://github.com/fermyon/spin?tag=v1.2.0#c4fbd08eb75ce9b2729bf7c450520076e692c6af" dependencies = [ "anyhow", "bytes", @@ -326,24 +356,29 @@ dependencies = [ "proc-macro2", "quote", "syn", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust-wasm 0.2.0", - "wit-bindgen-rust 0.2.0", ] [[package]] name = "spin-sdk" -version = "0.6.0" -source = "git+https://github.com/fermyon/spin?tag=v0.6.0#12a503794ff475b38d28bda9cb0683013bf93480" +version = "1.2.0" +source = "git+https://github.com/fermyon/spin?tag=v1.2.0#c4fbd08eb75ce9b2729bf7c450520076e692c6af" dependencies = [ "anyhow", "bytes", "form_urlencoded", "http", + "routefinder", "spin-macro", - "wit-bindgen-rust 0.2.0", + "thiserror", + "wit-bindgen-rust", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "syn" version = "1.0.99" @@ -355,6 +390,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -494,31 +549,13 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "wit-bindgen-gen-core" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=e9c7c0a3405845cecd3fe06f3c20ab413302fc73#e9c7c0a3405845cecd3fe06f3c20ab413302fc73" -dependencies = [ - "anyhow", - "wit-parser 0.1.0", -] - [[package]] name = "wit-bindgen-gen-core" version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "anyhow", - "wit-parser 0.2.0", -] - -[[package]] -name = "wit-bindgen-gen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=e9c7c0a3405845cecd3fe06f3c20ab413302fc73#e9c7c0a3405845cecd3fe06f3c20ab413302fc73" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.1.0", + "wit-parser", ] [[package]] @@ -527,17 +564,7 @@ version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "heck", - "wit-bindgen-gen-core 0.2.0", -] - -[[package]] -name = "wit-bindgen-gen-rust-wasm" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=e9c7c0a3405845cecd3fe06f3c20ab413302fc73#e9c7c0a3405845cecd3fe06f3c20ab413302fc73" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.1.0", - "wit-bindgen-gen-rust 0.1.0", + "wit-bindgen-gen-core", ] [[package]] @@ -546,18 +573,8 @@ version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "heck", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust 0.2.0", -] - -[[package]] -name = "wit-bindgen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=e9c7c0a3405845cecd3fe06f3c20ab413302fc73#e9c7c0a3405845cecd3fe06f3c20ab413302fc73" -dependencies = [ - "async-trait", - "bitflags", - "wit-bindgen-rust-impl 0.1.0", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust", ] [[package]] @@ -567,18 +584,7 @@ source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460 dependencies = [ "async-trait", "bitflags", - "wit-bindgen-rust-impl 0.2.0", -] - -[[package]] -name = "wit-bindgen-rust-impl" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=e9c7c0a3405845cecd3fe06f3c20ab413302fc73#e9c7c0a3405845cecd3fe06f3c20ab413302fc73" -dependencies = [ - "proc-macro2", - "syn", - "wit-bindgen-gen-core 0.1.0", - "wit-bindgen-gen-rust-wasm 0.1.0", + "wit-bindgen-rust-impl", ] [[package]] @@ -588,20 +594,8 @@ source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460 dependencies = [ "proc-macro2", "syn", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust-wasm 0.2.0", -] - -[[package]] -name = "wit-parser" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=e9c7c0a3405845cecd3fe06f3c20ab413302fc73#e9c7c0a3405845cecd3fe06f3c20ab413302fc73" -dependencies = [ - "anyhow", - "id-arena", - "pulldown-cmark", - "unicode-normalization", - "unicode-xid", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust-wasm", ] [[package]] diff --git a/highscore/Cargo.toml b/highscore/Cargo.toml index 65ed789..e355661 100644 --- a/highscore/Cargo.toml +++ b/highscore/Cargo.toml @@ -6,7 +6,7 @@ version = "0.1.0" edition = "2021" [lib] -crate-type = [ "cdylib" ] +crate-type = ["cdylib"] [dependencies] # Useful crate to handle errors. @@ -16,13 +16,13 @@ bytes = "1" # General-purpose crate with common HTTP types. http = "0.2" # The Spin SDK. -spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v0.6.0" } +spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v1.2.0" } # Crate that generates Rust Wasm bindings from a WebAssembly interface. -wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "e9c7c0a3405845cecd3fe06f3c20ab413302fc73" } +wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" } # ULID support rusty_ulid = "1.0.0" # Serde serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -[workspace] \ No newline at end of file +[workspace] diff --git a/highscore/src/lib.rs b/highscore/src/lib.rs index b4341d7..a53dc4b 100644 --- a/highscore/src/lib.rs +++ b/highscore/src/lib.rs @@ -6,18 +6,16 @@ use rusty_ulid::Ulid; use serde::{Deserialize, Serialize}; use spin_sdk::{ http::{Request, Response}, - http_component, redis, + http_component, + key_value::Store, }; #[http_component] fn highscore(req: Request) -> Result { - let redis_address = std::env::var("REDIS_ADDRESS")?; - + let store = Store::open_default()?; let res_body: String = match *req.method() { - Method::GET => { - serde_json::to_string_pretty(&get_highscore(&redis_address).unwrap()).unwrap() - } - Method::POST => check_highscore(req, redis_address).unwrap_or_else(|_| "".to_string()), + Method::GET => serde_json::to_string_pretty(&get_highscore(&store).unwrap()).unwrap(), + Method::POST => check_highscore(req, &store).unwrap_or_else(|_| "".to_string()), _ => "".to_string(), }; @@ -32,7 +30,7 @@ fn highscore(req: Request) -> Result { .body(Some(res_body.into()))?) } -fn check_highscore(req: Request, config: String) -> Result { +fn check_highscore(req: Request, store: &Store) -> Result { println!("Incoming body: {:?}", req.body()); // Parsing incoming request to HighScore @@ -42,7 +40,7 @@ fn check_highscore(req: Request, config: String) -> Result { }; // Fetching the highscores from store (JsonBin) - let mut high_score_table = match get_highscore(&config) { + let mut high_score_table = match get_highscore(&store) { Ok(high_score_table) => high_score_table, Err(e) => panic!("Tried to get high score: {}", Error::msg(e.to_string())), }; @@ -94,12 +92,12 @@ fn check_highscore(req: Request, config: String) -> Result { // If it has a username, let's store the result if !incoming_score.username.is_empty() { - redis::set( - &config, - "fw-highscore-list", - &serde_json::to_vec_pretty(&high_score_table)?, - ) - .map_err(|_| anyhow!("Error executing Redis set command"))?; + store + .set( + "fw-highscore-list", + &serde_json::to_vec_pretty(&high_score_table)?, + ) + .map_err(|_| anyhow!("Error storing in key/value"))?; } } // Setting up response @@ -114,8 +112,8 @@ fn check_highscore(req: Request, config: String) -> Result { Ok(res_body) } -fn get_highscore(config: &str) -> Result> { - let payload = redis::get(config, "fw-highscore-list"); +fn get_highscore(store: &Store) -> Result> { + let payload = store.get("fw-highscore-list"); let highscore_list: Vec = match payload { Ok(value) => { @@ -125,9 +123,7 @@ fn get_highscore(config: &str) -> Result> { _ => Vec::new(), } } - _ => { - panic!("Error getting data from Redis") - } + _ => Vec::new(), }; Ok(highscore_list) diff --git a/morsel_event/.gitignore b/morsel_event/.gitignore deleted file mode 100644 index 2f7896d..0000000 --- a/morsel_event/.gitignore +++ /dev/null @@ -1 +0,0 @@ -target/ diff --git a/morsel_event/Cargo.lock b/morsel_event/Cargo.lock deleted file mode 100644 index cc3a28f..0000000 --- a/morsel_event/Cargo.lock +++ /dev/null @@ -1,538 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "anyhow" -version = "1.0.56" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27" - -[[package]] -name = "async-trait" -version = "0.1.53" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed6aa3524a2dfcf9fe180c51eae2b58738348d819517ceadf95789c51fff7600" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "bytes" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "chrono" -version = "0.4.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" -dependencies = [ - "libc", - "num-integer", - "num-traits", - "time", - "winapi", -] - -[[package]] -name = "doc-comment" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" - -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - -[[package]] -name = "form_urlencoded" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" -dependencies = [ - "percent-encoding", -] - -[[package]] -name = "getrandom" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - -[[package]] -name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - -[[package]] -name = "http" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31f4c6746584866f0feabcc69893c5b51beef3831656a968ed7ae254cdc4fd03" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "id-arena" -version = "2.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" - -[[package]] -name = "itoa" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" - -[[package]] -name = "libc" -version = "0.2.122" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec647867e2bf0772e28c8bcde4f0d19a9216916e890543b5a03ed8ef27b8f259" - -[[package]] -name = "memchr" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" - -[[package]] -name = "morsel_event" -version = "0.1.0" -dependencies = [ - "anyhow", - "bytes", - "chrono", - "http", - "rusty_ulid", - "serde", - "serde_json", - "spin-sdk", - "wit-bindgen-rust 0.1.0", -] - -[[package]] -name = "num-integer" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" -dependencies = [ - "autocfg", - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" -dependencies = [ - "autocfg", -] - -[[package]] -name = "percent-encoding" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" - -[[package]] -name = "ppv-lite86" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" - -[[package]] -name = "proc-macro2" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1" -dependencies = [ - "unicode-xid", -] - -[[package]] -name = "pulldown-cmark" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8" -dependencies = [ - "bitflags", - "memchr", - "unicase", -] - -[[package]] -name = "quote" -version = "1.0.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "632d02bff7f874a36f33ea8bb416cd484b90cc66c1194b1a1110d067a7013f58" -dependencies = [ - "proc-macro2", -] - -[[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.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" -dependencies = [ - "getrandom", -] - -[[package]] -name = "rusty_ulid" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf6169f464e79b379a190df5d09d59fe0b581b1b0f788ce9de63953c31897aa0" -dependencies = [ - "chrono", - "doc-comment", - "rand", - "serde", -] - -[[package]] -name = "ryu" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" - -[[package]] -name = "serde" -version = "1.0.136" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.136" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "spin-macro" -version = "0.1.0" -source = "git+https://github.com/fermyon/spin?tag=v0.6.0#12a503794ff475b38d28bda9cb0683013bf93480" -dependencies = [ - "anyhow", - "bytes", - "http", - "proc-macro2", - "quote", - "syn", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust-wasm 0.2.0", - "wit-bindgen-rust 0.2.0", -] - -[[package]] -name = "spin-sdk" -version = "0.6.0" -source = "git+https://github.com/fermyon/spin?tag=v0.6.0#12a503794ff475b38d28bda9cb0683013bf93480" -dependencies = [ - "anyhow", - "bytes", - "form_urlencoded", - "http", - "spin-macro", - "wit-bindgen-rust 0.2.0", -] - -[[package]] -name = "syn" -version = "1.0.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - -[[package]] -name = "time" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" -dependencies = [ - "libc", - "winapi", -] - -[[package]] -name = "tinyvec" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c1c1d5a42b6245520c249549ec267180beaffcc0615401ac8e31853d4b6d8d2" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" - -[[package]] -name = "unicase" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" -dependencies = [ - "version_check", -] - -[[package]] -name = "unicode-normalization" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9" -dependencies = [ - "tinyvec", -] - -[[package]] -name = "unicode-segmentation" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" - -[[package]] -name = "unicode-xid" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" - -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - -[[package]] -name = "wasi" -version = "0.10.2+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "wit-bindgen-gen-core" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "anyhow", - "wit-parser 0.1.0", -] - -[[package]] -name = "wit-bindgen-gen-core" -version = "0.2.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" -dependencies = [ - "anyhow", - "wit-parser 0.2.0", -] - -[[package]] -name = "wit-bindgen-gen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.1.0", -] - -[[package]] -name = "wit-bindgen-gen-rust" -version = "0.2.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.2.0", -] - -[[package]] -name = "wit-bindgen-gen-rust-wasm" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.1.0", - "wit-bindgen-gen-rust 0.1.0", -] - -[[package]] -name = "wit-bindgen-gen-rust-wasm" -version = "0.2.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust 0.2.0", -] - -[[package]] -name = "wit-bindgen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "async-trait", - "bitflags", - "wit-bindgen-rust-impl 0.1.0", -] - -[[package]] -name = "wit-bindgen-rust" -version = "0.2.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" -dependencies = [ - "async-trait", - "bitflags", - "wit-bindgen-rust-impl 0.2.0", -] - -[[package]] -name = "wit-bindgen-rust-impl" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "proc-macro2", - "syn", - "wit-bindgen-gen-core 0.1.0", - "wit-bindgen-gen-rust-wasm 0.1.0", -] - -[[package]] -name = "wit-bindgen-rust-impl" -version = "0.2.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" -dependencies = [ - "proc-macro2", - "syn", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust-wasm 0.2.0", -] - -[[package]] -name = "wit-parser" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "anyhow", - "id-arena", - "pulldown-cmark", - "unicode-normalization", - "unicode-xid", -] - -[[package]] -name = "wit-parser" -version = "0.2.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" -dependencies = [ - "anyhow", - "id-arena", - "pulldown-cmark", - "unicode-normalization", - "unicode-xid", -] diff --git a/morsel_event/Cargo.toml b/morsel_event/Cargo.toml deleted file mode 100644 index 7568752..0000000 --- a/morsel_event/Cargo.toml +++ /dev/null @@ -1,26 +0,0 @@ -[package] -name = "morsel_event" -version = "0.1.0" -edition = "2021" - -[lib] -crate-type = ["cdylib"] - -[dependencies] -# Useful crate to handle errors. -anyhow = "1" -# Crate to simplify working with bytes. -bytes = "1" -# Date library -chrono = "0.4" -# General-purpose crate with common HTTP types. -http = "0.2" -# ULID support -rusty_ulid = "1.0.0" -# The Spin SDK. -spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v0.6.0" } -# Serde -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -# Crate that generates Rust Wasm bindings from a WebAssembly interface. -wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "dde4694aaa6acf9370206527a798ac4ba6a8c5b8" } diff --git a/morsel_event/Makefile b/morsel_event/Makefile deleted file mode 100644 index 6e502af..0000000 --- a/morsel_event/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -ENABLE_WASM_OPT ?= true - -.PHONY: build -build: - cargo build --target wasm32-wasi --release -ifeq ($(ENABLE_WASM_OPT),true) - wasm-opt target/wasm32-wasi/release/morsel_event.wasm -o ../components/morsel_event.wasm -Os -endif - -.PHONY: serve -serve: build -serve: - spin up -f spin.toml \ No newline at end of file diff --git a/morsel_event/README.md b/morsel_event/README.md deleted file mode 100644 index 86d0e6c..0000000 --- a/morsel_event/README.md +++ /dev/null @@ -1 +0,0 @@ -Responsible for listening for morsel events from Tally and doings stuff with those events. \ No newline at end of file diff --git a/morsel_event/spin.toml b/morsel_event/spin.toml deleted file mode 100644 index 2d8e18c..0000000 --- a/morsel_event/spin.toml +++ /dev/null @@ -1,12 +0,0 @@ -spin_manifest_version = "1" -name = "finicky-whiskers-redis" -trigger = { type = "redis", address = "redis://localhost:6379" } -version = "0.1.0" - -[[component]] -id = "morsel-event" -source = "target/wasm32-wasi/release/morsel_event.wasm" -environment = { REDIS_ADDRESS = "redis://localhost:6379" } -[component.trigger] -channel = "fw-tally" - diff --git a/morsel_event/src/lib.rs b/morsel_event/src/lib.rs deleted file mode 100644 index b911313..0000000 --- a/morsel_event/src/lib.rs +++ /dev/null @@ -1,67 +0,0 @@ -use anyhow::Result; -use bytes::Bytes; -use serde::{Deserialize, Serialize}; -use spin_sdk::{redis, redis_component}; -use tally::Tally; - -mod tally; - -const REDIS_ADDRESS_ENV: &str = "REDIS_ADDRESS"; - -#[redis_component] -fn on_message(msg: Bytes) -> anyhow::Result<()> { - let address = std::env::var(REDIS_ADDRESS_ENV)?; - - let tally_mon: Tally = serde_json::from_slice(&msg)?; - - if !tally_mon.correct { - return Ok(()); - } - - let id: rusty_ulid::Ulid = tally_mon.ulid.parse()?; - - let mut scorecard = match redis::get(&address, &id.to_string()) { - Err(_) => Scorecard::new(id), - Ok(data) => serde_json::from_slice(&data).unwrap_or_else(|_| Scorecard::new(id)), - }; - - match tally_mon.food.as_str() { - "chicken" => scorecard.chicken += 1, - "fish" => scorecard.fish += 1, - "beef" => scorecard.beef += 1, - "veg" => scorecard.veg += 1, - _ => {} - }; - - scorecard.total += 1; - - if let Ok(talled_mon) = serde_json::to_vec(&scorecard) { - redis::set(&address, &id.to_string(), &talled_mon) - .map_err(|_| anyhow::anyhow!("Error saving to Redis"))?; - } - - Ok(()) -} - -#[derive(Deserialize, Serialize)] -struct Scorecard { - pub ulid: rusty_ulid::Ulid, - pub beef: i32, - pub fish: i32, - pub chicken: i32, - pub veg: i32, - pub total: i32, -} - -impl Scorecard { - fn new(ulid: rusty_ulid::Ulid) -> Self { - Scorecard { - ulid, - beef: 0, - fish: 0, - chicken: 0, - veg: 0, - total: 0, - } - } -} diff --git a/morsel_event/src/tally.rs b/morsel_event/src/tally.rs deleted file mode 100644 index c42e467..0000000 --- a/morsel_event/src/tally.rs +++ /dev/null @@ -1,8 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Deserialize, Serialize, Debug)] -pub struct Tally { - pub ulid: String, - pub food: String, - pub correct: bool, -} diff --git a/nomad/README.md b/nomad/README.md index 0b8514c..8b8a347 100644 --- a/nomad/README.md +++ b/nomad/README.md @@ -1,10 +1,5 @@ # Nomad job for finicky-whiskers -The job will create two allocations executing spin http triggers and one -allocation for the morsel event queue backed by redis. - -The redis instance runs on static port 6379. - ``` nomad run finicky-whiskers.nomad ``` diff --git a/nomad/finicky-whiskers.nomad b/nomad/finicky-whiskers.nomad index 14fbb6d..46e1cb8 100644 --- a/nomad/finicky-whiskers.nomad +++ b/nomad/finicky-whiskers.nomad @@ -49,57 +49,7 @@ job "finicky-whiskers" { command = "bash" args = [ "-c", - "cd local/repo/session && make && cd .. && spin up --log-dir ${NOMAD_ALLOC_DIR}/logs --file spin.toml --listen ${NOMAD_ADDR_http} --env REDIS_ADDRESS=redis://${NOMAD_IP_http}:6379" - ] - } - } - - } - - group "finicky-whiskers-backend" { - network { - port "db" { - static = 6379 - } - } - - task "redis" { - driver = "docker" - - service { - name = "finicky-whiskers-redis" - port = "db" - - check { - name = "alive" - type = "tcp" - interval = "10s" - timeout = "2s" - } - } - config { - image = "redis:7" - ports = ["db"] - } - } - - task "morsel" { - driver = "raw_exec" - - artifact { - source = "git::https://github.com/fermyon/finicky-whiskers" - destination = "local/repo" - } - - env { - RUST_LOG = "spin=debug" - } - - config { - command = "bash" - args = [ - "-c", - "cd local/repo && perl -i -pe 's/localhost:6379/${NOMAD_ADDR_db}/' spin-morsel.toml && spin up --log-dir ${NOMAD_ALLOC_DIR}/logs --file spin-morsel.toml --env REDIS_ADDRESS=redis://${NOMAD_ADDR_db}" + "cd local/repo/session && make && cd .. && spin up --log-dir ${NOMAD_ALLOC_DIR}/logs --file spin.toml --listen ${NOMAD_ADDR_http}" ] } } diff --git a/reset/.gitignore b/reset/.gitignore new file mode 100644 index 0000000..b565010 --- /dev/null +++ b/reset/.gitignore @@ -0,0 +1,2 @@ +main.wasm +.spin/ diff --git a/reset/go.mod b/reset/go.mod new file mode 100644 index 0000000..289b3cc --- /dev/null +++ b/reset/go.mod @@ -0,0 +1,7 @@ +module github.com/reset + +go 1.17 + +require github.com/fermyon/spin/sdk/go v1.2.0 + +require github.com/julienschmidt/httprouter v1.3.0 // indirect diff --git a/reset/go.sum b/reset/go.sum new file mode 100644 index 0000000..734037a --- /dev/null +++ b/reset/go.sum @@ -0,0 +1,4 @@ +github.com/fermyon/spin/sdk/go v1.2.0 h1:eVTzxFHLgp1fB/N+PZ7hI369RTSWg8wNdtuLueF4Eqk= +github.com/fermyon/spin/sdk/go v1.2.0/go.mod h1:yb8lGesopgj/GwPzLPATxcOeqWZT/HjrzEFfwbztAXE= +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= diff --git a/reset/main.go b/reset/main.go new file mode 100644 index 0000000..d611ae0 --- /dev/null +++ b/reset/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "net/http" + "strings" + + spinhttp "github.com/fermyon/spin/sdk/go/http" + "github.com/fermyon/spin/sdk/go/key_value" +) + +const keyPrefix = "fw-" + +func init() { + spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) { + store, err := key_value.Open("default") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer key_value.Close(store) + + keys, err := key_value.GetKeys(store) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + for _, key := range keys { + if strings.HasPrefix(key, keyPrefix) { + if err := key_value.Delete(store, key); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + } + fmt.Fprintln(w, "Finicky Whiskers is reset.") + }) +} + +func main() {} diff --git a/scoreboard/Cargo.lock b/scoreboard/Cargo.lock index 615bae9..dab2659 100644 --- a/scoreboard/Cargo.lock +++ b/scoreboard/Cargo.lock @@ -222,6 +222,16 @@ dependencies = [ "getrandom", ] +[[package]] +name = "routefinder" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94f8f99b10dedd317514253dda1fa7c14e344aac96e1f78149a64879ce282aca" +dependencies = [ + "smartcow", + "smartstring", +] + [[package]] name = "rusty_ulid" version = "1.0.0" @@ -252,7 +262,7 @@ dependencies = [ "serde", "serde_json", "spin-sdk", - "wit-bindgen-rust 0.1.0", + "wit-bindgen-rust", ] [[package]] @@ -286,10 +296,30 @@ dependencies = [ "serde", ] +[[package]] +name = "smartcow" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "656fcb1c1fca8c4655372134ce87d8afdf5ec5949ebabe8d314be0141d8b5da2" +dependencies = [ + "smartstring", +] + +[[package]] +name = "smartstring" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29" +dependencies = [ + "autocfg", + "static_assertions", + "version_check", +] + [[package]] name = "spin-macro" version = "0.1.0" -source = "git+https://github.com/fermyon/spin?tag=v0.6.0#12a503794ff475b38d28bda9cb0683013bf93480" +source = "git+https://github.com/fermyon/spin?tag=v1.2.0#c4fbd08eb75ce9b2729bf7c450520076e692c6af" dependencies = [ "anyhow", "bytes", @@ -297,24 +327,29 @@ dependencies = [ "proc-macro2", "quote", "syn", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust-wasm 0.2.0", - "wit-bindgen-rust 0.2.0", ] [[package]] name = "spin-sdk" -version = "0.6.0" -source = "git+https://github.com/fermyon/spin?tag=v0.6.0#12a503794ff475b38d28bda9cb0683013bf93480" +version = "1.2.0" +source = "git+https://github.com/fermyon/spin?tag=v1.2.0#c4fbd08eb75ce9b2729bf7c450520076e692c6af" dependencies = [ "anyhow", "bytes", "form_urlencoded", "http", + "routefinder", "spin-macro", - "wit-bindgen-rust 0.2.0", + "thiserror", + "wit-bindgen-rust", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "syn" version = "1.0.91" @@ -326,6 +361,26 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "thiserror" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.1.43" @@ -415,31 +470,13 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "wit-bindgen-gen-core" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "anyhow", - "wit-parser 0.1.0", -] - [[package]] name = "wit-bindgen-gen-core" version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "anyhow", - "wit-parser 0.2.0", -] - -[[package]] -name = "wit-bindgen-gen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.1.0", + "wit-parser", ] [[package]] @@ -448,17 +485,7 @@ version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "heck", - "wit-bindgen-gen-core 0.2.0", -] - -[[package]] -name = "wit-bindgen-gen-rust-wasm" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.1.0", - "wit-bindgen-gen-rust 0.1.0", + "wit-bindgen-gen-core", ] [[package]] @@ -467,18 +494,8 @@ version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "heck", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust 0.2.0", -] - -[[package]] -name = "wit-bindgen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "async-trait", - "bitflags", - "wit-bindgen-rust-impl 0.1.0", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust", ] [[package]] @@ -488,18 +505,7 @@ source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460 dependencies = [ "async-trait", "bitflags", - "wit-bindgen-rust-impl 0.2.0", -] - -[[package]] -name = "wit-bindgen-rust-impl" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "proc-macro2", - "syn", - "wit-bindgen-gen-core 0.1.0", - "wit-bindgen-gen-rust-wasm 0.1.0", + "wit-bindgen-rust-impl", ] [[package]] @@ -509,20 +515,8 @@ source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460 dependencies = [ "proc-macro2", "syn", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust-wasm 0.2.0", -] - -[[package]] -name = "wit-parser" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "anyhow", - "id-arena", - "pulldown-cmark", - "unicode-normalization", - "unicode-xid", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust-wasm", ] [[package]] diff --git a/scoreboard/Cargo.toml b/scoreboard/Cargo.toml index cc542ca..2ec6b67 100644 --- a/scoreboard/Cargo.toml +++ b/scoreboard/Cargo.toml @@ -18,9 +18,9 @@ http = "0.2" # ULID support rusty_ulid = "1.0.0" # The Spin SDK. -spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v0.6.0" } +spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v1.2.0" } # Serde serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" # Crate that generates Rust Wasm bindings from a WebAssembly interface. -wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "dde4694aaa6acf9370206527a798ac4ba6a8c5b8" } +wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" } diff --git a/scoreboard/src/lib.rs b/scoreboard/src/lib.rs index a927814..97cdbd7 100644 --- a/scoreboard/src/lib.rs +++ b/scoreboard/src/lib.rs @@ -4,12 +4,11 @@ use rusty_ulid::Ulid; use serde::{Deserialize, Serialize}; use spin_sdk::{ http::{Request, Response}, - http_component, redis, + http_component, + key_value::Store, }; use std::collections::HashMap; - -// Env var that has the Redis address -const REDIS_ADDRESS_ENV: &str = "REDIS_ADDRESS"; +use std::str; #[http_component] fn scoreboard(req: Request) -> Result { @@ -65,11 +64,11 @@ fn get_ulid(url: &Uri) -> Result { } fn get_scores(ulid: &Ulid) -> Result { - let address = std::env::var(REDIS_ADDRESS_ENV)?; - //let channel = std::env::var(REDIS_CHANNEL_ENV)?; + let store = Store::open_default()?; - let raw_scorecard = redis::get(&address, &ulid.to_string()) - .map_err(|_| anyhow::anyhow!("Error fetching from Redis"))?; + let raw_scorecard = store + .get(format!("fw-{}", &ulid.to_string())) + .map_err(|_| anyhow::anyhow!("Error fetching from key/value"))?; let score: Scorecard = serde_json::from_slice(raw_scorecard.as_slice())?; Ok(score) } @@ -83,4 +82,3 @@ fn simple_query_parser(q: &str) -> HashMap { }); dict } - diff --git a/serve.sh b/serve.sh index af9c702..1085949 100755 --- a/serve.sh +++ b/serve.sh @@ -13,7 +13,4 @@ trap 'kill $(jobs -p)' EXIT # Start HTTP handlers spin up --log-dir "${SPIN_LOG_DIR}" --file spin.toml --listen "${SPIN_ADDRESS}" & -# Start Redis handler -spin up --log-dir "${SPIN_LOG_DIR}" --file spin-morsel.toml & - wait diff --git a/spin-morsel.toml b/spin-morsel.toml deleted file mode 100644 index a23674d..0000000 --- a/spin-morsel.toml +++ /dev/null @@ -1,11 +0,0 @@ -spin_manifest_version = "1" -name = "finicky-whiskers-redis" -trigger = { type = "redis", address = "redis://localhost:6379" } -version = "0.1.0" - -[[component]] -id = "morsel-event" -source = "components/morsel_event.wasm" -environment = { REDIS_ADDRESS = "redis://localhost:6379" } -[component.trigger] -channel = "fw-tally" diff --git a/spin.toml b/spin.toml index 06a7704..8428aba 100644 --- a/spin.toml +++ b/spin.toml @@ -33,7 +33,7 @@ watch = ["**/*.go", "go.mod"] [[component]] id = "tally" source = "components/tally.wasm" -environment = {REDIS_ADDRESS = "redis://localhost:6379/", REDIS_CHANNEL = "fw-tally"} +key_value_stores = ["default"] [component.trigger] route = "/tally" [component.build] @@ -57,7 +57,7 @@ watch = ["**/*.rs"] [[component]] id = "scoreboard" source = "components/scoreboard.wasm" -environment = {REDIS_ADDRESS = "redis://localhost:6379/"} +key_value_stores = ["default"] [component.trigger] route = "/score" [component.build] @@ -65,14 +65,26 @@ workdir = "scoreboard" command = "make" watch = ["src/**/*.rs", "Cargo.toml"] -# Stores highscores in redis +# Stores highscores in key/value [[component]] id = "highscore" source = "components/highscore.wasm" -environment = {REDIS_ADDRESS = "redis://localhost:6379/"} +key_value_stores = ["default"] [component.trigger] route = "/highscore" [component.build] workdir = "highscore" command = "make" watch = ["src/**/*.rs", "Cargo.toml"] + +[[component]] +id = "reset" +source = "components/reset.wasm" +allowed_http_hosts = [] +key_value_stores = ["default"] +[component.trigger] +route = "/reset" +[component.build] +command = "tinygo build -target=wasi -gc=leaking -no-debug -o ../components/reset.wasm main.go" +workdir = "reset" +watch = ["**/*.go", "go.mod"] diff --git a/tally/Cargo.lock b/tally/Cargo.lock index cdae0be..85ef1e5 100644 --- a/tally/Cargo.lock +++ b/tally/Cargo.lock @@ -222,6 +222,16 @@ dependencies = [ "getrandom", ] +[[package]] +name = "routefinder" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94f8f99b10dedd317514253dda1fa7c14e344aac96e1f78149a64879ce282aca" +dependencies = [ + "smartcow", + "smartstring", +] + [[package]] name = "rusty_ulid" version = "1.0.0" @@ -271,10 +281,30 @@ dependencies = [ "serde", ] +[[package]] +name = "smartcow" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "656fcb1c1fca8c4655372134ce87d8afdf5ec5949ebabe8d314be0141d8b5da2" +dependencies = [ + "smartstring", +] + +[[package]] +name = "smartstring" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29" +dependencies = [ + "autocfg", + "static_assertions", + "version_check", +] + [[package]] name = "spin-macro" version = "0.1.0" -source = "git+https://github.com/fermyon/spin?tag=v0.6.0#12a503794ff475b38d28bda9cb0683013bf93480" +source = "git+https://github.com/fermyon/spin?tag=v1.2.0#c4fbd08eb75ce9b2729bf7c450520076e692c6af" dependencies = [ "anyhow", "bytes", @@ -282,24 +312,29 @@ dependencies = [ "proc-macro2", "quote", "syn", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust-wasm 0.2.0", - "wit-bindgen-rust 0.2.0", ] [[package]] name = "spin-sdk" -version = "0.6.0" -source = "git+https://github.com/fermyon/spin?tag=v0.6.0#12a503794ff475b38d28bda9cb0683013bf93480" +version = "1.2.0" +source = "git+https://github.com/fermyon/spin?tag=v1.2.0#c4fbd08eb75ce9b2729bf7c450520076e692c6af" dependencies = [ "anyhow", "bytes", "form_urlencoded", "http", + "routefinder", "spin-macro", - "wit-bindgen-rust 0.2.0", + "thiserror", + "wit-bindgen-rust", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "syn" version = "1.0.90" @@ -323,7 +358,27 @@ dependencies = [ "serde", "serde_json", "spin-sdk", - "wit-bindgen-rust 0.1.0", + "wit-bindgen-rust", +] + +[[package]] +name = "thiserror" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -415,31 +470,13 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "wit-bindgen-gen-core" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "anyhow", - "wit-parser 0.1.0", -] - [[package]] name = "wit-bindgen-gen-core" version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "anyhow", - "wit-parser 0.2.0", -] - -[[package]] -name = "wit-bindgen-gen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.1.0", + "wit-parser", ] [[package]] @@ -448,17 +485,7 @@ version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "heck", - "wit-bindgen-gen-core 0.2.0", -] - -[[package]] -name = "wit-bindgen-gen-rust-wasm" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "heck", - "wit-bindgen-gen-core 0.1.0", - "wit-bindgen-gen-rust 0.1.0", + "wit-bindgen-gen-core", ] [[package]] @@ -467,18 +494,8 @@ version = "0.2.0" source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "heck", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust 0.2.0", -] - -[[package]] -name = "wit-bindgen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "async-trait", - "bitflags", - "wit-bindgen-rust-impl 0.1.0", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust", ] [[package]] @@ -488,18 +505,7 @@ source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460 dependencies = [ "async-trait", "bitflags", - "wit-bindgen-rust-impl 0.2.0", -] - -[[package]] -name = "wit-bindgen-rust-impl" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "proc-macro2", - "syn", - "wit-bindgen-gen-core 0.1.0", - "wit-bindgen-gen-rust-wasm 0.1.0", + "wit-bindgen-rust-impl", ] [[package]] @@ -509,20 +515,8 @@ source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460 dependencies = [ "proc-macro2", "syn", - "wit-bindgen-gen-core 0.2.0", - "wit-bindgen-gen-rust-wasm 0.2.0", -] - -[[package]] -name = "wit-parser" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=dde4694aaa6acf9370206527a798ac4ba6a8c5b8#dde4694aaa6acf9370206527a798ac4ba6a8c5b8" -dependencies = [ - "anyhow", - "id-arena", - "pulldown-cmark", - "unicode-normalization", - "unicode-xid", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust-wasm", ] [[package]] diff --git a/tally/Cargo.toml b/tally/Cargo.toml index 7d69a78..88cd5a5 100644 --- a/tally/Cargo.toml +++ b/tally/Cargo.toml @@ -18,10 +18,9 @@ http = "0.2" # ULID support rusty_ulid = "1.0.0" # The Spin SDK. -spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v0.6.0" } +spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v1.2.0" } # Serde serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" # Crate that generates Rust Wasm bindings from a WebAssembly interface. -wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "dde4694aaa6acf9370206527a798ac4ba6a8c5b8" } - +wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" } diff --git a/tally/README.md b/tally/README.md index c80a400..500ddb8 100644 --- a/tally/README.md +++ b/tally/README.md @@ -1,12 +1,13 @@ -Responsible for sending an individual event to the Redis queue. +Responsible for sending an individual event to the key/value store. This expects a query string with the follow query params: - ulid: The ULID for the session - food: The name of the food -- correct: The string value `t` (or `true`) for correct. +- correct: The string value `t` (or `true`) for correct. -It then transforms this data into the following JSON data that is sent to Redis: +It then transforms this data into the following JSON data that is sent to +key/value: ```json { @@ -20,19 +21,12 @@ It then transforms this data into the following JSON data that is sent to Redis: Run `make build` in this directory to build. -> NOTE: At this time, you will need to edit `Cargo.toml` to point the Spin Rust SDK to the PR with the Redis Outbound implementation. - To serve, use the `spin.toml` file in the parent directory. -You must set the following environment variables either in `spin.toml` or on the CLI: - -- `REDIS_ADDRESS`: The Redis URL, e.g. `redis://127.0.0.1:6379` -- `REDIS_CHANNEL`: The Redis channel name, e.g. `fw-tally` - ## Manual Testing If you `cargo install rusty_ulid`, then you can use this curl command to manually test: ``` curl localhost:3000/tally\?ulid=$(rusty_ulid)\&correct=TRUE\&food=fish -``` \ No newline at end of file +``` diff --git a/tally/src/lib.rs b/tally/src/lib.rs index 4ee091c..b477c0d 100644 --- a/tally/src/lib.rs +++ b/tally/src/lib.rs @@ -3,39 +3,33 @@ use anyhow::Result; use chrono::{Duration, Utc}; use http::Uri; use rusty_ulid::Ulid; +use serde::{Deserialize, Serialize}; use spin_sdk::{ http::{Request, Response}, - http_component, redis, + http_component, + key_value::Store, }; use std::collections::HashMap; +use std::str; mod tally; /// Game duration in seconds const GAME_DURATION_SECONDS: i64 = 30; -// The environment variable set in `spin.toml` that points to the -// address of the Redis server that the component will publish -// a message to. -const REDIS_ADDRESS_ENV: &str = "REDIS_ADDRESS"; - -// The environment variable set in `spin.toml` that specifies -// the Redis channel that the component will publish to. -const REDIS_CHANNEL_ENV: &str = "REDIS_CHANNEL"; - /// A simple Spin HTTP component. #[http_component] fn tally_point(req: Request) -> Result { // This gets info out of query params match parse_query_params(req.uri()) { Ok(tally) => { - // Should store something in Redis. + // Should store something in key/value. match serde_json::to_string(&tally) { Ok(payload) => { - if let Err(e) = publish(payload.clone()) { - eprintln!("Error sending to Redis: {}", e) + if let Err(e) = tally_score(&payload) { + eprintln!("Error tallying score: {}", e) } else { - println!("Sent message to Redis: {}", payload) + println!("Tallied score: {}", payload) } } Err(e) => eprintln!("Error serializing JSON: {}", e), @@ -114,12 +108,61 @@ fn validate_ulid(ulid: &str) -> anyhow::Result { Ok(id) } -fn publish(payload: String) -> Result<()> { - let address = std::env::var(REDIS_ADDRESS_ENV)?; - let channel = std::env::var(REDIS_CHANNEL_ENV)?; +fn tally_score(msg: &str) -> anyhow::Result<()> { + let tally_mon: Tally = serde_json::from_str(msg)?; + + if !tally_mon.correct { + return Ok(()); + } + + let id: rusty_ulid::Ulid = tally_mon.ulid.parse()?; + + let store = Store::open_default()?; + let mut scorecard = match store.get(format!("fw-{}", &id.to_string())) { + Err(_) => Scorecard::new(id), + Ok(data) => serde_json::from_slice(&data).unwrap_or_else(|_| Scorecard::new(id)), + }; + + match tally_mon.food.as_str() { + "chicken" => scorecard.chicken += 1, + "fish" => scorecard.fish += 1, + "beef" => scorecard.beef += 1, + "veg" => scorecard.veg += 1, + _ => {} + }; + + scorecard.total += 1; + + if let Ok(talled_mon) = serde_json::to_vec(&scorecard) { + store + .set(format!("fw-{}", &id.to_string()), &talled_mon) + .map_err(|_| anyhow::anyhow!("Error saving to key/value store"))?; + } + + Ok(()) +} + +#[derive(Deserialize, Serialize)] +struct Scorecard { + pub ulid: rusty_ulid::Ulid, + pub beef: i32, + pub fish: i32, + pub chicken: i32, + pub veg: i32, + pub total: i32, +} - redis::publish(&address, &channel, payload.as_bytes()) - .map_err(|e| anyhow::anyhow!("Error sending to redis: {:?}", e)) +impl Scorecard { + fn new(ulid: rusty_ulid::Ulid) -> Self { + Scorecard { + ulid, + beef: 0, + fish: 0, + chicken: 0, + veg: 0, + total: 0, + } + } } #[cfg(test)]