Skip to content

Commit

Permalink
Update to use @grpc/grpc-js node package, and update helloworld example
Browse files Browse the repository at this point in the history
  • Loading branch information
stanley-cheung committed Jun 11, 2020
1 parent b89d8f1 commit 365a97b
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 109 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion net/grpc/gateway/docker/node_server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion net/grpc/gateway/examples/echo/node-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
11 changes: 5 additions & 6 deletions net/grpc/gateway/examples/echo/node-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
55 changes: 13 additions & 42 deletions net/grpc/gateway/examples/helloworld/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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 &
```
Expand All @@ -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
```
22 changes: 10 additions & 12 deletions net/grpc/gateway/examples/helloworld/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});


Expand All @@ -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}"`);
});
19 changes: 0 additions & 19 deletions net/grpc/gateway/examples/helloworld/envoy.Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion net/grpc/gateway/examples/helloworld/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}]
2 changes: 0 additions & 2 deletions net/grpc/gateway/examples/helloworld/helloworld.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion net/grpc/gateway/examples/helloworld/node-client.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion net/grpc/gateway/examples/helloworld/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 8 additions & 22 deletions net/grpc/gateway/examples/helloworld/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
*/
Expand All @@ -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;

0 comments on commit 365a97b

Please sign in to comment.