From 1f9cacff2f52be813267d0e3c0f7d80ced951eaa Mon Sep 17 00:00:00 2001 From: pritesh-patel Date: Wed, 25 Sep 2019 13:49:58 -0700 Subject: [PATCH] #6 add grpc-web typescript generation support --- example/hello-world-idl/Makefile | 20 +++++++-- .../gen/web/health/health_pb.d.ts | 45 +++++++++++++++++++ .../gen/web/helloworld/helloworld_pb.d.ts | 2 + example/hello-world/sprout.yml | 2 +- templates/proto/makefile.tmpl | 26 +++++++++-- templates/sprout/sprout.tmpl | 5 +-- 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 example/hello-world-idl/gen/web/health/health_pb.d.ts create mode 100644 example/hello-world-idl/gen/web/helloworld/helloworld_pb.d.ts diff --git a/example/hello-world-idl/Makefile b/example/hello-world-idl/Makefile index 466f6f64c..314e76634 100644 --- a/example/hello-world-idl/Makefile +++ b/example/hello-world-idl/Makefile @@ -1,22 +1,36 @@ PROTOC_VERSION := 3.9.2 +PROTOC_WEB_VERSION := 1.0.6 PROTO_SOURCES := -I /usr/local/include PROTO_SOURCES += -I . PROTO_SOURCES += -I ${GOPATH}/src PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis +deps-linux: deps-protoc-linux deps-grpc-web-linux - -deps-linux: +deps-protoc-linux: curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip unzip protoc-$(PROTOC_VERSION)-linux-x86_64.zip -d protoc3 sudo mv protoc3/bin/* /usr/local/bin/ sudo mv protoc3/include/* /usr/local/include/ rm -rf protoc3 protoc-$(PROTOC_VERSION)-linux-x86_64.zip -generate: +deps-grpc-web-linux: + curl -OL https://github.com/grpc/grpc-web/releases/download/$(PROTOC_WEB_VERSION)/protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 + sudo mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web + chmod +x /usr/local/bin/protoc-gen-grpc-web + +generate: generate-grpc generate-web + +generate-grpc: mkdir -p gen/go protoc ${PROTO_SOURCES} --go_out=plugins=grpc:./gen/go ./proto/health/*.proto protoc ${PROTO_SOURCES} --go_out=plugins=grpc:./gen/go ./proto/helloworld/*.proto cp -f -rv gen/go/proto/* gen/go rm -rf gen/go/proto +generate-web: + mkdir -p gen/web + protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/health/*.proto + protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/helloworld/*.proto + cp -f -rv gen/web/proto/* gen/web + rm -rf gen/web/Proto gen/web/proto diff --git a/example/hello-world-idl/gen/web/health/health_pb.d.ts b/example/hello-world-idl/gen/web/health/health_pb.d.ts new file mode 100644 index 000000000..57a475723 --- /dev/null +++ b/example/hello-world-idl/gen/web/health/health_pb.d.ts @@ -0,0 +1,45 @@ +import * as jspb from "google-protobuf" + +export class HealthCheckRequest extends jspb.Message { + getService(): string; + setService(value: string): void; + + serializeBinary(): Uint8Array; + toObject(includeInstance?: boolean): HealthCheckRequest.AsObject; + static toObject(includeInstance: boolean, msg: HealthCheckRequest): HealthCheckRequest.AsObject; + static serializeBinaryToWriter(message: HealthCheckRequest, writer: jspb.BinaryWriter): void; + static deserializeBinary(bytes: Uint8Array): HealthCheckRequest; + static deserializeBinaryFromReader(message: HealthCheckRequest, reader: jspb.BinaryReader): HealthCheckRequest; +} + +export namespace HealthCheckRequest { + export type AsObject = { + service: string, + } +} + +export class HealthCheckResponse extends jspb.Message { + getStatus(): HealthCheckResponse.ServingStatus; + setStatus(value: HealthCheckResponse.ServingStatus): void; + + serializeBinary(): Uint8Array; + toObject(includeInstance?: boolean): HealthCheckResponse.AsObject; + static toObject(includeInstance: boolean, msg: HealthCheckResponse): HealthCheckResponse.AsObject; + static serializeBinaryToWriter(message: HealthCheckResponse, writer: jspb.BinaryWriter): void; + static deserializeBinary(bytes: Uint8Array): HealthCheckResponse; + static deserializeBinaryFromReader(message: HealthCheckResponse, reader: jspb.BinaryReader): HealthCheckResponse; +} + +export namespace HealthCheckResponse { + export type AsObject = { + status: HealthCheckResponse.ServingStatus, + } + + export enum ServingStatus { + UNKNOWN = 0, + SERVING = 1, + NOT_SERVING = 2, + SERVICE_UNKNOWN = 3, + } +} + diff --git a/example/hello-world-idl/gen/web/helloworld/helloworld_pb.d.ts b/example/hello-world-idl/gen/web/helloworld/helloworld_pb.d.ts new file mode 100644 index 000000000..9444b292f --- /dev/null +++ b/example/hello-world-idl/gen/web/helloworld/helloworld_pb.d.ts @@ -0,0 +1,2 @@ +import * as jspb from "google-protobuf" + diff --git a/example/hello-world/sprout.yml b/example/hello-world/sprout.yml index 7c0888489..fc44b9aca 100644 --- a/example/hello-world/sprout.yml +++ b/example/hello-world/sprout.yml @@ -11,7 +11,7 @@ network: grpc: host: 0.0.0.0 port: 3000 - http: + web: enabled: true port: 8080 graphql: diff --git a/templates/proto/makefile.tmpl b/templates/proto/makefile.tmpl index d86250e6c..ca7d944f2 100644 --- a/templates/proto/makefile.tmpl +++ b/templates/proto/makefile.tmpl @@ -1,4 +1,5 @@ PROTOC_VERSION := 3.9.2 +PROTOC_WEB_VERSION := 1.0.6 PROTO_SOURCES := -I /usr/local/include PROTO_SOURCES += -I . PROTO_SOURCES += -I ${GOPATH}/src @@ -7,16 +8,23 @@ PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_p {{- $language := .Language }} {{- $http := .Network.Http.Enabled }} +deps-linux: deps-protoc-linux deps-grpc-web-linux - -deps-linux: +deps-protoc-linux: curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip unzip protoc-$(PROTOC_VERSION)-linux-x86_64.zip -d protoc3 sudo mv protoc3/bin/* /usr/local/bin/ sudo mv protoc3/include/* /usr/local/include/ rm -rf protoc3 protoc-$(PROTOC_VERSION)-linux-x86_64.zip -generate: +deps-grpc-web-linux: + curl -OL https://github.com/grpc/grpc-web/releases/download/$(PROTOC_WEB_VERSION)/protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 + sudo mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web + chmod +x /usr/local/bin/protoc-gen-grpc-web + +generate: generate-grpc {{ if .Network.Web.Enabled }}generate-web{{- end}} + +generate-grpc: mkdir -p gen/{{ $language }} protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc:./gen/{{ $language }} ./proto/health/*.proto {{- range .Services}} @@ -25,3 +33,15 @@ generate: cp -f -rv gen/go/proto/* gen/go rm -rf gen/go/proto + +{{- if .Network.Web.Enabled }} +generate-web: + mkdir -p gen/web + protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/health/*.proto +{{- range .Services}} + protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/{{ .Name }}/*.proto +{{- end }} + cp -f -rv gen/web/proto/* gen/web + rm -rf gen/web/Proto gen/web/proto +{{- end}} + diff --git a/templates/sprout/sprout.tmpl b/templates/sprout/sprout.tmpl index 1ec3e592e..917932c4f 100644 --- a/templates/sprout/sprout.tmpl +++ b/templates/sprout/sprout.tmpl @@ -11,12 +11,9 @@ network: grpc: host: 0.0.0.0 port: 3000 - http: + web: enabled: true port: 8080 - graphql: - enabled: true - port: 8082 services: - name: helloworld