Skip to content

Commit 1f9cacf

Browse files
committed
#6 add grpc-web typescript generation support
1 parent f5f09ea commit 1f9cacf

File tree

6 files changed

+89
-11
lines changed

6 files changed

+89
-11
lines changed

example/hello-world-idl/Makefile

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
PROTOC_VERSION := 3.9.2
2+
PROTOC_WEB_VERSION := 1.0.6
23
PROTO_SOURCES := -I /usr/local/include
34
PROTO_SOURCES += -I .
45
PROTO_SOURCES += -I ${GOPATH}/src
56
PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
67

8+
deps-linux: deps-protoc-linux deps-grpc-web-linux
79

8-
9-
deps-linux:
10+
deps-protoc-linux:
1011
curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip
1112
unzip protoc-$(PROTOC_VERSION)-linux-x86_64.zip -d protoc3
1213
sudo mv protoc3/bin/* /usr/local/bin/
1314
sudo mv protoc3/include/* /usr/local/include/
1415
rm -rf protoc3 protoc-$(PROTOC_VERSION)-linux-x86_64.zip
1516

16-
generate:
17+
deps-grpc-web-linux:
18+
curl -OL https://github.com/grpc/grpc-web/releases/download/$(PROTOC_WEB_VERSION)/protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64
19+
sudo mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web
20+
chmod +x /usr/local/bin/protoc-gen-grpc-web
21+
22+
generate: generate-grpc generate-web
23+
24+
generate-grpc:
1725
mkdir -p gen/go
1826
protoc ${PROTO_SOURCES} --go_out=plugins=grpc:./gen/go ./proto/health/*.proto
1927
protoc ${PROTO_SOURCES} --go_out=plugins=grpc:./gen/go ./proto/helloworld/*.proto
2028
cp -f -rv gen/go/proto/* gen/go
2129
rm -rf gen/go/proto
30+
generate-web:
31+
mkdir -p gen/web
32+
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/health/*.proto
33+
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/helloworld/*.proto
34+
cp -f -rv gen/web/proto/* gen/web
35+
rm -rf gen/web/Proto gen/web/proto
2236

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as jspb from "google-protobuf"
2+
3+
export class HealthCheckRequest extends jspb.Message {
4+
getService(): string;
5+
setService(value: string): void;
6+
7+
serializeBinary(): Uint8Array;
8+
toObject(includeInstance?: boolean): HealthCheckRequest.AsObject;
9+
static toObject(includeInstance: boolean, msg: HealthCheckRequest): HealthCheckRequest.AsObject;
10+
static serializeBinaryToWriter(message: HealthCheckRequest, writer: jspb.BinaryWriter): void;
11+
static deserializeBinary(bytes: Uint8Array): HealthCheckRequest;
12+
static deserializeBinaryFromReader(message: HealthCheckRequest, reader: jspb.BinaryReader): HealthCheckRequest;
13+
}
14+
15+
export namespace HealthCheckRequest {
16+
export type AsObject = {
17+
service: string,
18+
}
19+
}
20+
21+
export class HealthCheckResponse extends jspb.Message {
22+
getStatus(): HealthCheckResponse.ServingStatus;
23+
setStatus(value: HealthCheckResponse.ServingStatus): void;
24+
25+
serializeBinary(): Uint8Array;
26+
toObject(includeInstance?: boolean): HealthCheckResponse.AsObject;
27+
static toObject(includeInstance: boolean, msg: HealthCheckResponse): HealthCheckResponse.AsObject;
28+
static serializeBinaryToWriter(message: HealthCheckResponse, writer: jspb.BinaryWriter): void;
29+
static deserializeBinary(bytes: Uint8Array): HealthCheckResponse;
30+
static deserializeBinaryFromReader(message: HealthCheckResponse, reader: jspb.BinaryReader): HealthCheckResponse;
31+
}
32+
33+
export namespace HealthCheckResponse {
34+
export type AsObject = {
35+
status: HealthCheckResponse.ServingStatus,
36+
}
37+
38+
export enum ServingStatus {
39+
UNKNOWN = 0,
40+
SERVING = 1,
41+
NOT_SERVING = 2,
42+
SERVICE_UNKNOWN = 3,
43+
}
44+
}
45+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import * as jspb from "google-protobuf"
2+

example/hello-world/sprout.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ network:
1111
grpc:
1212
host: 0.0.0.0
1313
port: 3000
14-
http:
14+
web:
1515
enabled: true
1616
port: 8080
1717
graphql:

templates/proto/makefile.tmpl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
PROTOC_VERSION := 3.9.2
2+
PROTOC_WEB_VERSION := 1.0.6
23
PROTO_SOURCES := -I /usr/local/include
34
PROTO_SOURCES += -I .
45
PROTO_SOURCES += -I ${GOPATH}/src
@@ -7,16 +8,23 @@ PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_p
78
{{- $language := .Language }}
89
{{- $http := .Network.Http.Enabled }}
910

11+
deps-linux: deps-protoc-linux deps-grpc-web-linux
1012

11-
12-
deps-linux:
13+
deps-protoc-linux:
1314
curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip
1415
unzip protoc-$(PROTOC_VERSION)-linux-x86_64.zip -d protoc3
1516
sudo mv protoc3/bin/* /usr/local/bin/
1617
sudo mv protoc3/include/* /usr/local/include/
1718
rm -rf protoc3 protoc-$(PROTOC_VERSION)-linux-x86_64.zip
1819

19-
generate:
20+
deps-grpc-web-linux:
21+
curl -OL https://github.com/grpc/grpc-web/releases/download/$(PROTOC_WEB_VERSION)/protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64
22+
sudo mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web
23+
chmod +x /usr/local/bin/protoc-gen-grpc-web
24+
25+
generate: generate-grpc {{ if .Network.Web.Enabled }}generate-web{{- end}}
26+
27+
generate-grpc:
2028
mkdir -p gen/{{ $language }}
2129
protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc:./gen/{{ $language }} ./proto/health/*.proto
2230
{{- range .Services}}
@@ -25,3 +33,15 @@ generate:
2533
cp -f -rv gen/go/proto/* gen/go
2634
rm -rf gen/go/proto
2735

36+
37+
{{- if .Network.Web.Enabled }}
38+
generate-web:
39+
mkdir -p gen/web
40+
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/health/*.proto
41+
{{- range .Services}}
42+
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/{{ .Name }}/*.proto
43+
{{- end }}
44+
cp -f -rv gen/web/proto/* gen/web
45+
rm -rf gen/web/Proto gen/web/proto
46+
{{- end}}
47+

templates/sprout/sprout.tmpl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ network:
1111
grpc:
1212
host: 0.0.0.0
1313
port: 3000
14-
http:
14+
web:
1515
enabled: true
1616
port: 8080
17-
graphql:
18-
enabled: true
19-
port: 8082
2017

2118
services:
2219
- name: helloworld

0 commit comments

Comments
 (0)