From 438594bb2a1838db7d05ae9fdd37adc3ec2ac492 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Thu, 6 Oct 2022 21:22:44 -0400 Subject: Test server communication --- Cargo.lock | 72 +++++++++++++++++++++++++++++++++++++++++++----------- server/src/main.rs | 10 ++++++-- webapp/Cargo.toml | 2 ++ webapp/src/main.rs | 23 +++++++++++++++++ 4 files changed, 91 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e3a582..a6e81de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -751,6 +751,26 @@ dependencies = [ "web-sys", ] +[[package]] +name = "gloo-net" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec897194fb9ac576c708f63d35604bc58f2a262b8cec0fabfed26f3991255f21" +dependencies = [ + "futures-channel", + "futures-core", + "futures-sink", + "gloo-utils", + "js-sys", + "pin-project", + "serde", + "serde_json", + "thiserror", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "gloo-render" version = "0.1.1" @@ -1051,9 +1071,9 @@ checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" [[package]] name = "js-sys" -version = "0.3.59" +version = "0.3.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "258451ab10b34f8af53416d1fdab72c22e805f0c92a1136d59470ec0b11138b2" +checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" dependencies = [ "wasm-bindgen", ] @@ -1434,6 +1454,26 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +[[package]] +name = "pin-project" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.2.9" @@ -2503,19 +2543,21 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.82" +version = "0.2.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d" +checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" dependencies = [ "cfg-if 1.0.0", + "serde", + "serde_json", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.82" +version = "0.2.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "662cd44805586bd52971b9586b1df85cdbbd9112e4ef4d8f41559c334dc6ac3f" +checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" dependencies = [ "bumpalo", "log", @@ -2528,9 +2570,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.32" +version = "0.4.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa76fb221a1f8acddf5b54ace85912606980ad661ac7a503b4570ffd3a624dad" +checksum = "23639446165ca5a5de86ae1d8896b737ae80319560fbaa4c2887b7da6e7ebd7d" dependencies = [ "cfg-if 1.0.0", "js-sys", @@ -2540,9 +2582,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.82" +version = "0.2.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b260f13d3012071dfb1512849c033b1925038373aea48ced3012c09df952c602" +checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2550,9 +2592,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.82" +version = "0.2.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5be8e654bdd9b79216c2929ab90721aa82faf65c48cdf08bdc4e7f51357b80da" +checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" dependencies = [ "proc-macro2", "quote", @@ -2563,9 +2605,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.82" +version = "0.2.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a" +checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" [[package]] name = "wasm-logger" @@ -2597,12 +2639,14 @@ dependencies = [ "dotenv", "env_logger", "getrandom", + "gloo-net", "lazy_static", "log", "rand", "regex", "strum", "strum_macros", + "wasm-bindgen-futures", "wasm-logger", "wee_alloc", "yew", diff --git a/server/src/main.rs b/server/src/main.rs index 0e9fa84..411ea47 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -7,10 +7,11 @@ use rocket::http::uri::Reference; use rocket::http::{Cookie, CookieJar, Status}; use rocket::outcome::Outcome; use rocket::request::{self, FromRequest}; -use rocket::response::Redirect; +use rocket::response::{Redirect, content}; use rocket::Request; use serde::{Deserialize, Serialize}; use std::result::Result; +use rocket::serde::json::Json; use openidconnect::core::{ CoreAuthenticationFlow, CoreClient, CoreProviderMetadata, CoreResponseType, CoreUserInfoClaims, @@ -75,6 +76,11 @@ fn index(user: Option) -> String { } } +#[get("/test")] +fn test() -> Json { + Json(String::from("test")) +} + async fn keycloak_client() -> CoreClient { // // Use OpenID Connect Discovery to fetch the provider metadata. let provider_metadata = CoreProviderMetadata::discover_async( @@ -154,7 +160,7 @@ async fn main() -> Result<(), anyhow::Error> { env_logger::init(); rocket::build() - .mount("/api", routes![index, login, keycloak_callback]) + .mount("/api", routes![index, test, login, keycloak_callback]) .mount("/", FileServer::from(std::env::var("WEBAPP_PATH").unwrap())) .launch() .await?; diff --git a/webapp/Cargo.toml b/webapp/Cargo.toml index 91fd87c..13a7e6a 100644 --- a/webapp/Cargo.toml +++ b/webapp/Cargo.toml @@ -16,6 +16,8 @@ wee_alloc = "0.4.3" anyhow = "1.0" regex = "1.0" lazy_static = "1.4" +gloo-net = "0.2.4" +wasm-bindgen-futures = "0.4.33" [dev-dependencies] env_logger = "0.8.4" diff --git a/webapp/src/main.rs b/webapp/src/main.rs index 8a1fccc..8b77ea2 100644 --- a/webapp/src/main.rs +++ b/webapp/src/main.rs @@ -5,6 +5,7 @@ pub mod bridge_engine; pub mod card; pub mod components; use components::Game; +use gloo_net::http::Request; extern crate wee_alloc; // Use `wee_alloc` as the global allocator. @@ -19,8 +20,30 @@ fn main() { #[function_component(App)] pub fn app() -> Html { + let msg = use_state(|| "".to_string()); + { + let msg = msg.clone(); + use_effect_with_deps( + move |_| { + wasm_bindgen_futures::spawn_local(async move { + msg.set( + Request::get("/api/test") + .send() + .await + .unwrap() + .json() + .await + .unwrap(), + ) + }); + || () + }, + (), + ) + } html! { <> +

{ &*msg }

-- cgit v1.2.3