Skip to content

Commit

Permalink
Merge pull request #7 from commitdev/grpc-web-typescript-support
Browse files Browse the repository at this point in the history
#6 add grpc-web typescript generation support
  • Loading branch information
Pritesh-Patel committed Oct 2, 2019
2 parents 8983404 + 1f9cacf commit 1c6622a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
20 changes: 17 additions & 3 deletions 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

45 changes: 45 additions & 0 deletions 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,
}
}

2 changes: 2 additions & 0 deletions example/hello-world-idl/gen/web/helloworld/helloworld_pb.d.ts
@@ -0,0 +1,2 @@
import * as jspb from "google-protobuf"

2 changes: 1 addition & 1 deletion example/hello-world/sprout.yml
Expand Up @@ -11,7 +11,7 @@ network:
grpc:
host: 0.0.0.0
port: 3000
http:
web:
enabled: true
port: 8080
graphql:
Expand Down
26 changes: 23 additions & 3 deletions 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
Expand All @@ -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}}
Expand All @@ -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}}

5 changes: 1 addition & 4 deletions templates/sprout/sprout.tmpl
Expand Up @@ -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
Expand Down

0 comments on commit 1c6622a

Please sign in to comment.