From 06954caaaf3715f7ea3a6494a1399a79866b1ece Mon Sep 17 00:00:00 2001 From: jeff Date: Sun, 3 Jan 2021 20:42:54 +0000 Subject: [PATCH] Successful (but very manual) grpc-web requests. --- Cargo.lock | 73 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- envoy/Dockerfile | 3 ++ envoy/envoy.yaml | 54 +++++++++++++++++++++++++++ src/main.rs | 2 +- static/scripts/src/index.ts | 24 +++++++++--- 6 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 envoy/Dockerfile create mode 100644 envoy/envoy.yaml diff --git a/Cargo.lock b/Cargo.lock index d2cffa6..949186b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1872,6 +1872,21 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "ring" +version = "0.16.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "024a1e66fea74c66c66624ee5622a7ff0e4b73a13b4f5c326ddb50c708944226" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin", + "untrusted", + "web-sys", + "winapi 0.3.9", +] + [[package]] name = "rocket" version = "0.5.0-dev" @@ -1947,6 +1962,7 @@ dependencies = [ "state", "time 0.2.23", "tokio", + "tokio-rustls", "uncased", "unicode-xid", "version_check", @@ -1988,6 +2004,19 @@ dependencies = [ "semver 0.9.0", ] +[[package]] +name = "rustls" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d1126dcf58e93cee7d098dbda643b5f92ed724f1f6a63007c1116eed6700c81" +dependencies = [ + "base64 0.12.3", + "log", + "ring", + "sct", + "webpki", +] + [[package]] name = "ryu" version = "1.0.5" @@ -2019,6 +2048,16 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "sct" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3042af939fca8c3453b7af0f1c66e533a15a86169e39de2657310ade8f98d3c" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "security-framework" version = "2.0.0" @@ -2187,6 +2226,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "sqlformat" version = "0.1.5" @@ -2609,6 +2654,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-rustls" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a" +dependencies = [ + "futures-core", + "rustls", + "tokio", + "webpki", +] + [[package]] name = "tokio-util" version = "0.3.1" @@ -3027,6 +3084,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + [[package]] name = "url" version = "2.2.0" @@ -3148,6 +3211,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki" +version = "0.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "which" version = "3.1.1" diff --git a/Cargo.toml b/Cargo.toml index 7862f6e..deca536 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ barrel = { version = "0.6", features = ["sqlite3"] } [dependencies.rocket] git = "https://github.com/SergioBenitez/Rocket" branch = "master" -features = ["secrets"] +features = ["secrets", "tls"] [dependencies.rocket_contrib] git = "https://github.com/SergioBenitez/Rocket" diff --git a/envoy/Dockerfile b/envoy/Dockerfile new file mode 100644 index 0000000..4f684c1 --- /dev/null +++ b/envoy/Dockerfile @@ -0,0 +1,3 @@ +FROM envoyproxy/envoy:v1.16-latest +COPY ./envoy.yaml /etc/envoy/envoy.yaml +CMD /usr/local/bin/envoy -c /etc/envoy/envoy.yaml diff --git a/envoy/envoy.yaml b/envoy/envoy.yaml new file mode 100644 index 0000000..ce27b9b --- /dev/null +++ b/envoy/envoy.yaml @@ -0,0 +1,54 @@ +admin: + access_log_path: /tmp/admin_access.log + address: + socket_address: { address: 0.0.0.0, port_value: 9901 } + +static_resources: + listeners: + - name: listener_0 + address: + socket_address: { address: 0.0.0.0, port_value: 8080 } + filter_chains: + - filters: + - name: envoy.filters.network.http_connection_manager + typed_config: + "@type": type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager + codec_type: auto + stat_prefix: ingress_http + route_config: + name: local_route + virtual_hosts: + - name: local_service + domains: ["*"] + routes: + - match: { prefix: "/" } + route: + cluster: greeter_service + max_grpc_timeout: 0s + cors: + allow_origin_string_match: + - prefix: "*" + allow_methods: GET, PUT, DELETE, POST, OPTIONS + allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout + max_age: "1728000" + expose_headers: custom-header-1,grpc-status,grpc-message + http_filters: + - name: envoy.filters.http.grpc_web + - name: envoy.filters.http.cors + - name: envoy.filters.http.router + clusters: + - name: greeter_service + connect_timeout: 0.25s + type: logical_dns + http2_protocol_options: {} + lb_policy: round_robin + # win/mac hosts: Use address: host.docker.internal instead of address: localhost in the line below + load_assignment: + cluster_name: cluster_0 + endpoints: + - lb_endpoints: + - endpoint: + address: + socket_address: + address: localhost + port_value: 9090 diff --git a/src/main.rs b/src/main.rs index 0140627..85d4f52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,7 +44,7 @@ async fn gin_and_tonic() -> Result<(), Box> { } } - let addr = "[::1]:50051".parse()?; + let addr = "[::1]:9090".parse()?; let greeter = MyGreeter::default(); println!("Running tonic"); diff --git a/static/scripts/src/index.ts b/static/scripts/src/index.ts index e8455a0..06fbc28 100644 --- a/static/scripts/src/index.ts +++ b/static/scripts/src/index.ts @@ -1,10 +1,24 @@ import { grpc } from "@improbable-eng/grpc-web"; -// import { CofdApi } from "../_proto/cofd_api_pb_service"; +import { CofdApi } from "../_proto/cofd_api_pb_service"; import { UpdateSkillValueRequest } from "../_proto/cofd_api_pb"; +import { CofdSheet } from "../_proto/cofd_pb"; let x = new UpdateSkillValueRequest(); -// x.setCharacterId(1); -// x.setCharacterUsername("guy"); -// console.log("hello", x); +x.setCharacterId(1); +x.setCharacterUsername("guy"); +console.log("hello", x); -console.log("hello from index"); +grpc.invoke(CofdApi.UpdateSkillValue, { + request: x, + host: window.location.protocol + "//" + window.location.hostname + ":8080", + onMessage: (message: CofdSheet.Skill) => { + console.log("got skill: ", message.toObject()); + }, + onEnd: (code: grpc.Code, msg: string | undefined, trailers: grpc.Metadata) => { + if (code == grpc.Code.OK) { + console.log("all ok") + } else { + console.log("hit an error", code, msg, trailers); + } + } +});