Skip to content

Commit

Permalink
update example (grpc#360)
Browse files Browse the repository at this point in the history
* update relative path for misc

* dep ensure

* ./protogen.sh
  • Loading branch information
atecce authored and easyCZ committed Apr 11, 2019
1 parent e511d30 commit d998c3c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 25 deletions.
36 changes: 30 additions & 6 deletions client/grpc-web-react-example/go/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (

var (
enableTls = flag.Bool("enable_tls", false, "Use TLS - required for HTTP2.")
tlsCertFilePath = flag.String("tls_cert_file", "../misc/localhost.crt", "Path to the CRT/PEM file.")
tlsKeyFilePath = flag.String("tls_key_file", "../misc/localhost.key", "Path to the private key file.")
tlsCertFilePath = flag.String("tls_cert_file", "../../misc/localhost.crt", "Path to the CRT/PEM file.")
tlsKeyFilePath = flag.String("tls_key_file", "../../misc/localhost.key", "Path to the private key file.")
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion client/grpc-web-react-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build:proto": "./protogen.sh",
"get_go_deps": "./get_go_deps.sh",
"postinstall": "npm run get_go_deps",
"webpack-dev:tls": "cd ts && export USE_TLS=true && webpack-dev-server --colors --watch --hot --inline --port 8082 --host 0.0.0.0 --output-public-path=https://localhost:8082/build/ --https --cert=../../misc/localhost.crt --key=../../misc/localhost.key",
"webpack-dev:tls": "cd ts && export USE_TLS=true && webpack-dev-server --colors --watch --hot --inline --port 8082 --host 0.0.0.0 --output-public-path=https://localhost:8082/build/ --https --cert=../../../misc/localhost.crt --key=../../../misc/localhost.key",
"webpack-dev": "cd ts && webpack-dev-server --colors --watch --hot --inline --port 8081 --host 0.0.0.0 --output-public-path=http://localhost:8081/build/",
"start:tls": "npm run build:proto && concurrently --kill-others \"go run go/exampleserver/exampleserver.go --enable_tls=true\" \"npm run webpack-dev:tls\"",
"start": "npm run build:proto && concurrently --kill-others \"go run go/exampleserver/exampleserver.go\" \"npm run webpack-dev\""
Expand Down
2 changes: 1 addition & 1 deletion client/grpc-web-react-example/protogen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if [[ "$PROTOC" == "" ]]; then
fi

# Install protoc-gen-go from the vendored protobuf package to $GOBIN
(cd ../vendor/github.com/golang/protobuf && make install)
(cd ../../vendor/github.com/golang/protobuf && make install)

echo "Compiling protobuf definitions"
protoc \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ proto.examplecom.library.Book.prototype.getIsbn = function() {

/** @param {number} value */
proto.examplecom.library.Book.prototype.setIsbn = function(value) {
jspb.Message.setField(this, 1, value);
jspb.Message.setProto3IntField(this, 1, value);
};


Expand All @@ -191,7 +191,7 @@ proto.examplecom.library.Book.prototype.getTitle = function() {

/** @param {string} value */
proto.examplecom.library.Book.prototype.setTitle = function(value) {
jspb.Message.setField(this, 2, value);
jspb.Message.setProto3StringField(this, 2, value);
};


Expand All @@ -206,7 +206,7 @@ proto.examplecom.library.Book.prototype.getAuthor = function() {

/** @param {string} value */
proto.examplecom.library.Book.prototype.setAuthor = function(value) {
jspb.Message.setField(this, 3, value);
jspb.Message.setProto3StringField(this, 3, value);
};


Expand Down Expand Up @@ -348,7 +348,7 @@ proto.examplecom.library.GetBookRequest.prototype.getIsbn = function() {

/** @param {number} value */
proto.examplecom.library.GetBookRequest.prototype.setIsbn = function(value) {
jspb.Message.setField(this, 1, value);
jspb.Message.setProto3IntField(this, 1, value);
};


Expand Down Expand Up @@ -490,7 +490,7 @@ proto.examplecom.library.QueryBooksRequest.prototype.getAuthorPrefix = function(

/** @param {string} value */
proto.examplecom.library.QueryBooksRequest.prototype.setAuthorPrefix = function(value) {
jspb.Message.setField(this, 1, value);
jspb.Message.setProto3StringField(this, 1, value);
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// file: examplecom/library/book_service.proto

import * as examplecom_library_book_service_pb from "../../examplecom/library/book_service_pb";
import {grpc} from "grpc-web-client";
import {grpc} from "@improbable-eng/grpc-web";

type BookServiceGetBook = {
readonly methodName: string;
Expand Down Expand Up @@ -30,28 +30,45 @@ export class BookService {

export type ServiceError = { message: string, code: number; metadata: grpc.Metadata }
export type Status = { details: string, code: number; metadata: grpc.Metadata }
export type ServiceClientOptions = { transport: grpc.TransportConstructor; debug?: boolean }

interface UnaryResponse {
cancel(): void;
}
interface ResponseStream<T> {
cancel(): void;
on(type: 'data', handler: (message: T) => void): ResponseStream<T>;
on(type: 'end', handler: () => void): ResponseStream<T>;
on(type: 'status', handler: (status: Status) => void): ResponseStream<T>;
}
interface RequestStream<T> {
write(message: T): RequestStream<T>;
end(): void;
cancel(): void;
on(type: 'end', handler: () => void): RequestStream<T>;
on(type: 'status', handler: (status: Status) => void): RequestStream<T>;
}
interface BidirectionalStream<ReqT, ResT> {
write(message: ReqT): BidirectionalStream<ReqT, ResT>;
end(): void;
cancel(): void;
on(type: 'data', handler: (message: ResT) => void): BidirectionalStream<ReqT, ResT>;
on(type: 'end', handler: () => void): BidirectionalStream<ReqT, ResT>;
on(type: 'status', handler: (status: Status) => void): BidirectionalStream<ReqT, ResT>;
}

export class BookServiceClient {
readonly serviceHost: string;

constructor(serviceHost: string, options?: ServiceClientOptions);
constructor(serviceHost: string, options?: grpc.RpcOptions);
getBook(
requestMessage: examplecom_library_book_service_pb.GetBookRequest,
metadata: grpc.Metadata,
callback: (error: ServiceError, responseMessage: examplecom_library_book_service_pb.Book|null) => void
): void;
callback: (error: ServiceError|null, responseMessage: examplecom_library_book_service_pb.Book|null) => void
): UnaryResponse;
getBook(
requestMessage: examplecom_library_book_service_pb.GetBookRequest,
callback: (error: ServiceError, responseMessage: examplecom_library_book_service_pb.Book|null) => void
): void;
callback: (error: ServiceError|null, responseMessage: examplecom_library_book_service_pb.Book|null) => void
): UnaryResponse;
queryBooks(requestMessage: examplecom_library_book_service_pb.QueryBooksRequest, metadata?: grpc.Metadata): ResponseStream<examplecom_library_book_service_pb.Book>;
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// file: examplecom/library/book_service.proto

var examplecom_library_book_service_pb = require("../../examplecom/library/book_service_pb");
var grpc = require("grpc-web-client").grpc;
var grpc = require("@improbable-eng/grpc-web").grpc;

var BookService = (function () {
function BookService() {}
Expand Down Expand Up @@ -39,7 +39,7 @@ BookServiceClient.prototype.getBook = function getBook(requestMessage, metadata,
if (arguments.length === 2) {
callback = arguments[1];
}
grpc.unary(BookService.GetBook, {
var client = grpc.unary(BookService.GetBook, {
request: requestMessage,
host: this.serviceHost,
metadata: metadata,
Expand All @@ -48,13 +48,22 @@ BookServiceClient.prototype.getBook = function getBook(requestMessage, metadata,
onEnd: function (response) {
if (callback) {
if (response.status !== grpc.Code.OK) {
callback(Object.assign(new Error(response.statusMessage), { code: response.status, metadata: response.trailers }), null);
var err = new Error(response.statusMessage);
err.code = response.status;
err.metadata = response.trailers;
callback(err, null);
} else {
callback(null, response.message);
}
}
}
});
return {
cancel: function () {
callback = null;
client.close();
}
};
};

BookServiceClient.prototype.queryBooks = function queryBooks(requestMessage, metadata) {
Expand Down

0 comments on commit d998c3c

Please sign in to comment.