diff --git a/docker-compose.yml b/docker-compose.yml index 68d3eb449..4706d030d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,7 +33,7 @@ services: context: ./ dockerfile: ./net/grpc/gateway/docker/node_server/Dockerfile depends_on: - - common + - prereqs image: grpcweb/node-server ports: - "9090:9090" diff --git a/net/grpc/gateway/docker/node_server/Dockerfile b/net/grpc/gateway/docker/node_server/Dockerfile index 5ae20d2a6..6e448bd50 100644 --- a/net/grpc/gateway/docker/node_server/Dockerfile +++ b/net/grpc/gateway/docker/node_server/Dockerfile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -FROM grpcweb/common +FROM grpcweb/prereqs WORKDIR /github/grpc-web/net/grpc/gateway/examples/echo/node-server diff --git a/net/grpc/gateway/examples/echo/node-server/package.json b/net/grpc/gateway/examples/echo/node-server/package.json index 59a1b8a2d..675491c44 100644 --- a/net/grpc/gateway/examples/echo/node-server/package.json +++ b/net/grpc/gateway/examples/echo/node-server/package.json @@ -3,10 +3,10 @@ "version": "0.1.0", "main": "server.js", "dependencies": { + "@grpc/grpc-js": "~1.0.5", "@grpc/proto-loader": "~0.5.0", "async": "~1.5.2", "google-protobuf": "~3.12.0", - "grpc": "~1.24.2", "lodash": "~4.17.0" } } diff --git a/net/grpc/gateway/examples/echo/node-server/server.js b/net/grpc/gateway/examples/echo/node-server/server.js index 6e929a877..94be6c6d8 100644 --- a/net/grpc/gateway/examples/echo/node-server/server.js +++ b/net/grpc/gateway/examples/echo/node-server/server.js @@ -21,7 +21,7 @@ var PROTO_PATH = __dirname + '/../echo.proto'; var assert = require('assert'); var async = require('async'); var _ = require('lodash'); -var grpc = require('grpc'); +var grpc = require('@grpc/grpc-js'); var protoLoader = require('@grpc/proto-loader'); var packageDefinition = protoLoader.loadSync( PROTO_PATH, @@ -105,13 +105,12 @@ function getServer() { } if (require.main === module) { - // If this is run as a script, start a server on an unused port var echoServer = getServer(); echoServer.bindAsync( - '0.0.0.0:9090', grpc.ServerCredentials.createInsecure(), (err, port) => { - assert.ifError(err); - echoServer.start(); - }); + '0.0.0.0:9090', grpc.ServerCredentials.createInsecure(), (err, port) => { + assert.ifError(err); + echoServer.start(); + }); } exports.getServer = getServer; diff --git a/net/grpc/gateway/examples/helloworld/README.md b/net/grpc/gateway/examples/helloworld/README.md index 5fe22aff0..cad2eb81d 100644 --- a/net/grpc/gateway/examples/helloworld/README.md +++ b/net/grpc/gateway/examples/helloworld/README.md @@ -45,7 +45,7 @@ a nice response and send it back to the client via `callback(null, response)`. ```js var PROTO_PATH = __dirname + '/helloworld.proto'; -var grpc = require('grpc'); +var grpc = require('@grpc/grpc-js'); var protoLoader = require('@grpc/proto-loader'); var packageDefinition = protoLoader.loadSync( PROTO_PATH, @@ -74,8 +74,11 @@ function getServer() { if (require.main === module) { var server = getServer(); - server.bind('0.0.0.0:9090', grpc.ServerCredentials.createInsecure()); - server.start(); + server.bindAsync( + '0.0.0.0:9090', grpc.ServerCredentials.createInsecure(), (err, port) => { + assert.ifError(err); + server.start(); + }); } exports.getServer = getServer; @@ -89,11 +92,6 @@ Envoy to listen at port `:8080`, and forward any gRPC-Web requests to a cluster at port `:9090`. ```yaml -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 @@ -132,7 +130,7 @@ static_resources: type: logical_dns http2_protocol_options: {} lb_policy: round_robin - hosts: [{ socket_address: { address: localhost, port_value: 9090 }}] + hosts: [{ socket_address: { address: 0.0.0.0, port_value: 9090 }}] ``` NOTE: As per [this issue](https://github.com/grpc/grpc-web/issues/436): if @@ -150,15 +148,6 @@ or if your version of Docker on Mac older then v18.03.0, change it to: hosts: [{ socket_address: { address: docker.for.mac.localhost, port_value: 9090 }}] ``` -To run Envoy (for later), you will need a simple Dockerfile. Put this in a -`envoy.Dockerfile`. - -```dockerfile -FROM envoyproxy/envoy:v1.14.1 -COPY ./envoy.yaml /etc/envoy/envoy.yaml -CMD /usr/local/bin/envoy -c /etc/envoy/envoy.yaml -``` - ## Write Client Code Now, we are ready to write some client code! Put this in a `client.js` file. @@ -194,11 +183,12 @@ the `client.js` files. "name": "grpc-web-simple-example", "version": "0.1.0", "description": "gRPC-Web simple example", + "main": "server.js", "devDependencies": { + "@grpc/grpc-js": "~1.0.5", "@grpc/proto-loader": "~0.5.4", "async": "~1.5.2", "google-protobuf": "~3.12.0", - "grpc": "~1.24.2", "grpc-web": "~1.1.0", "lodash": "~4.17.0", "webpack": "~4.43.0", @@ -243,14 +233,6 @@ first from [here](https://github.com/protocolbuffers/protobuf/releases). Make sure they are both executable and are discoverable from your PATH. -For example, in MacOS, you can do: - -``` -$ sudo mv ~/Downloads/protoc-gen-grpc-web-1.1.0-darwin-x86_64 \ - /usr/local/bin/protoc-gen-grpc-web -$ chmod +x /usr/local/bin/protoc-gen-grpc-web -``` - When you have both `protoc` and `protoc-gen-grpc-web` installed, you can now run this command: @@ -301,27 +283,21 @@ run the 3 processes all in the background. above). ```sh - $ docker build -t helloworld/envoy -f ./envoy.Dockerfile . - $ docker run -d -p 8080:8080 -p 9901:9901 --network=host helloworld/envoy + $ docker run -d -v "$(pwd)"/envoy.yaml:/etc/envoy/envoy.yaml:ro \ + --network=host envoyproxy/envoy:v1.14.1 ``` NOTE: As per [this issue](https://github.com/grpc/grpc-web/issues/436): if you are running Docker on Mac/Windows, remove the `--network=host` option: ```sh - ... - $ docker run -d -p 8080:8080 -p 9901:9901 helloworld/envoy + $ docker run -d -v "$(pwd)"/envoy.yaml:/etc/envoy/envoy.yaml:ro \ + envoyproxy/envoy:v1.14.1 ``` 3. Run the simple Web Server. This hosts the static file `index.html` and `dist/main.js` we generated earlier. - ```sh - $ python2 -m SimpleHTTPServer 8081 & - ``` - - or for Python 3.x - ```sh $ python3 -m http.server 8081 & ``` @@ -337,8 +313,3 @@ Open up the developer console and you should see the following printed out: ``` Hello! World ``` - -You can also browse to the envoy admin via -``` -localhost:9901 -``` diff --git a/net/grpc/gateway/examples/helloworld/client.js b/net/grpc/gateway/examples/helloworld/client.js index 5e3da682c..124f80a88 100644 --- a/net/grpc/gateway/examples/helloworld/client.js +++ b/net/grpc/gateway/examples/helloworld/client.js @@ -28,7 +28,12 @@ var request = new HelloRequest(); request.setName('World'); client.sayHello(request, {}, (err, response) => { - console.log(response.getMessage()); + if (err) { + console.log(`Unexpected error for sayHello: code = ${err.code}` + + `, message = "${err.message}"`); + } else { + console.log(response.getMessage()); + } }); @@ -41,14 +46,7 @@ var stream = client.sayRepeatHello(streamRequest, {}); stream.on('data', (response) => { console.log(response.getMessage()); }); - - -// deadline exceeded -var deadline = new Date(); -deadline.setSeconds(deadline.getSeconds() + 1); - -client.sayHelloAfterDelay(request, {deadline: deadline.getTime()}, - (err, response) => { - console.log('Got error, code = ' + err.code + - ', message = ' + err.message); - }); +stream.on('error', (err) => { + console.log(`Unexpected stream error: code = ${err.code}` + + `, message = "${err.message}"`); +}); diff --git a/net/grpc/gateway/examples/helloworld/envoy.Dockerfile b/net/grpc/gateway/examples/helloworld/envoy.Dockerfile deleted file mode 100644 index c5369e290..000000000 --- a/net/grpc/gateway/examples/helloworld/envoy.Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -FROM envoyproxy/envoy:v1.14.1 - -COPY ./envoy.yaml /etc/envoy/envoy.yaml - -CMD /usr/local/bin/envoy -c /etc/envoy/envoy.yaml diff --git a/net/grpc/gateway/examples/helloworld/envoy.yaml b/net/grpc/gateway/examples/helloworld/envoy.yaml index 1be87ec62..b7f43ca2a 100644 --- a/net/grpc/gateway/examples/helloworld/envoy.yaml +++ b/net/grpc/gateway/examples/helloworld/envoy.yaml @@ -42,4 +42,4 @@ static_resources: http2_protocol_options: {} lb_policy: round_robin # win/mac hosts: Use address: host.docker.internal instead of address: localhost in the line below - hosts: [{ socket_address: { address: localhost, port_value: 9090 }}] + hosts: [{ socket_address: { address: 0.0.0.0, port_value: 9090 }}] diff --git a/net/grpc/gateway/examples/helloworld/helloworld.proto b/net/grpc/gateway/examples/helloworld/helloworld.proto index e95d57b80..3dc2a0435 100644 --- a/net/grpc/gateway/examples/helloworld/helloworld.proto +++ b/net/grpc/gateway/examples/helloworld/helloworld.proto @@ -21,8 +21,6 @@ service Greeter { rpc SayHello(HelloRequest) returns (HelloReply); // server streaming call rpc SayRepeatHello(RepeatHelloRequest) returns (stream HelloReply); - // unary call - response after a length delay - rpc SayHelloAfterDelay(HelloRequest) returns (HelloReply); } message HelloRequest { diff --git a/net/grpc/gateway/examples/helloworld/node-client.js b/net/grpc/gateway/examples/helloworld/node-client.js index 3c1c1aa3c..d9183f925 100644 --- a/net/grpc/gateway/examples/helloworld/node-client.js +++ b/net/grpc/gateway/examples/helloworld/node-client.js @@ -1,7 +1,7 @@ var PROTO_PATH = __dirname + '/helloworld.proto'; var async = require('async'); -var grpc = require('grpc'); +var grpc = require('@grpc/grpc-js'); var protoLoader = require('@grpc/proto-loader'); var packageDefinition = protoLoader.loadSync( PROTO_PATH, diff --git a/net/grpc/gateway/examples/helloworld/package.json b/net/grpc/gateway/examples/helloworld/package.json index 698605376..c76bd697a 100644 --- a/net/grpc/gateway/examples/helloworld/package.json +++ b/net/grpc/gateway/examples/helloworld/package.json @@ -4,10 +4,10 @@ "description": "gRPC-Web simple example", "main": "server.js", "devDependencies": { + "@grpc/grpc-js": "~1.0.5", "@grpc/proto-loader": "~0.5.4", "async": "~1.5.2", "google-protobuf": "~3.12.0", - "grpc": "~1.24.2", "grpc-web": "~1.1.0", "lodash": "~4.17.0", "webpack": "~4.43.0", diff --git a/net/grpc/gateway/examples/helloworld/server.js b/net/grpc/gateway/examples/helloworld/server.js index f5f1a525c..c9832c8f4 100644 --- a/net/grpc/gateway/examples/helloworld/server.js +++ b/net/grpc/gateway/examples/helloworld/server.js @@ -18,9 +18,10 @@ var PROTO_PATH = __dirname + '/helloworld.proto'; -var grpc = require('grpc'); -var _ = require('lodash'); +var assert = require('assert'); var async = require('async'); +var _ = require('lodash'); +var grpc = require('@grpc/grpc-js'); var protoLoader = require('@grpc/proto-loader'); var packageDefinition = protoLoader.loadSync( PROTO_PATH, @@ -62,23 +63,6 @@ function doSayRepeatHello(call) { }); } -/** - * @param {!Object} call - * @param {function():?} callback - */ -function doSayHelloAfterDelay(call, callback) { - function dummy() { - return (cb) => { - _.delay(cb, 5000); - }; - } - async.series([dummy()], () => { - callback(null, { - message: 'Hello! '+call.request.name - }); - }); -} - /** * @return {!Object} gRPC server */ @@ -87,15 +71,17 @@ function getServer() { server.addService(helloworld.Greeter.service, { sayHello: doSayHello, sayRepeatHello: doSayRepeatHello, - sayHelloAfterDelay: doSayHelloAfterDelay }); return server; } if (require.main === module) { var server = getServer(); - server.bind('0.0.0.0:9090', grpc.ServerCredentials.createInsecure()); - server.start(); + server.bindAsync( + '0.0.0.0:9090', grpc.ServerCredentials.createInsecure(), (err, port) => { + assert.ifError(err); + server.start(); + }); } exports.getServer = getServer;