Skip to content

Commit e7ba6a4

Browse files
charleswhchanjohanbrandhorst
authored andcommitted
Improve README file
1 parent e77cc68 commit e7ba6a4

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

README.md

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
The grpc-gateway is a plugin of the Google protocol buffers compiler
66
[protoc](https://github.com/protocolbuffers/protobuf).
77
It reads protobuf service definitions and generates a reverse-proxy server which
8-
translates a RESTful JSON API into gRPC. This server is generated according to the
8+
'translates a RESTful HTTP API into gRPC. This server is generated according to the
99
[`google.api.http`](https://github.com/googleapis/googleapis/blob/master/google/api/http.proto#L46)
1010
annotations in your service definitions.
1111

12-
It helps you provide your APIs in both gRPC and RESTful style at the same time.
12+
This helps you provide your APIs in both gRPC and RESTful style at the same time.
1313

1414
![architecture introduction diagram](https://docs.google.com/drawings/d/12hp4CPqrNPFhattL_cIoJptFvlAqm5wLQ0ggqI5mkCg/pub?w=749&h=370)
1515

@@ -21,7 +21,7 @@ languages, it is fast, easy-to-use, bandwidth-efficient and its design is
2121
combat-proven by Google. However, you might still want to provide a traditional
2222
RESTful JSON API as well. Reasons can range from maintaining
2323
backwards-compatibility, supporting languages or clients not well supported by
24-
gRPC to simply maintaining the aesthetics and tooling involved with a RESTful
24+
gRPC, to simply maintaining the aesthetics and tooling involved with a RESTful
2525
JSON architecture.
2626

2727
This project aims to provide that HTTP+JSON interface to your gRPC service.
@@ -37,7 +37,7 @@ manager or by downloading one of the releases from the official repository:
3737
https://github.com/protocolbuffers/protobuf/releases
3838

3939

40-
Then, `go get -u` as usual the following packages:
40+
Then use `go get -u` to download the following packages:
4141

4242
```sh
4343
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
@@ -55,9 +55,9 @@ Make sure that your `$GOBIN` is in your `$PATH`.
5555

5656
## Usage
5757

58-
1. Define your service in gRPC
58+
1. Define your [gRPC](https://grpc.io/docs/) service using protocol buffers
5959

60-
your_service.proto:
60+
`your_service.proto`:
6161
```protobuf
6262
syntax = "proto3";
6363
package example;
@@ -69,10 +69,11 @@ Make sure that your `$GOBIN` is in your `$PATH`.
6969
rpc Echo(StringMessage) returns (StringMessage) {}
7070
}
7171
```
72+
7273
2. Add a [`google.api.http`](https://github.com/googleapis/googleapis/blob/master/google/api/http.proto#L46)
7374
annotation to your .proto file
7475

75-
your_service.proto:
76+
`your_service.proto`:
7677
```diff
7778
syntax = "proto3";
7879
package example;
@@ -102,6 +103,7 @@ annotation to your .proto file
102103

103104
3. Generate gRPC stub
104105

106+
The following generates gRPC code for Golang based on `path/to/your_service.proto`:
105107
```sh
106108
protoc -I/usr/local/include -I. \
107109
-I$GOPATH/src \
@@ -111,28 +113,30 @@ annotation to your .proto file
111113
```
112114

113115
It will generate a stub file `path/to/your_service.pb.go`.
116+
114117
4. Implement your service in gRPC as usual
115-
1. (Optional) Generate gRPC stub in the language you want.
116118

117-
e.g.
119+
1. (Optional) Generate gRPC stub in the [other programming languages](https://grpc.io/docs/).
120+
121+
For example, the following generates gRPC code for Ruby based on `path/to/your_service.proto`:
118122
```sh
119123
protoc -I/usr/local/include -I. \
120124
-I$GOPATH/src \
121125
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
122126
--ruby_out=. \
123-
path/to/your/service_proto
127+
path/to/your_service.proto
124128

125129
protoc -I/usr/local/include -I. \
126130
-I$GOPATH/src \
127131
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
128132
--plugin=protoc-gen-grpc=grpc_ruby_plugin \
129133
--grpc-ruby_out=. \
130-
path/to/your/service.proto
134+
path/to/your_service.proto
131135
```
132136
2. Add the googleapis-common-protos gem (or your language equivalent) as a dependency to your project.
133-
3. Implement your service
137+
3. Implement your gRPC service stubs
134138

135-
5. Generate reverse-proxy
139+
5. Generate reverse-proxy using `protoc-gen-grpc-gateway`
136140

137141
```sh
138142
protoc -I/usr/local/include -I. \
@@ -144,54 +148,58 @@ annotation to your .proto file
144148

145149
It will generate a reverse proxy `path/to/your_service.pb.gw.go`.
146150

147-
6. Write an entrypoint
151+
6. Write an entrypoint for the HTTP reverse-proxy server
148152

149-
Now you need to write an entrypoint of the proxy server.
150153
```go
151154
package main
152-
155+
153156
import (
157+
"context" // Use "golang.org/x/net/context" for Golang version <= 1.6
154158
"flag"
155159
"net/http"
156-
160+
157161
"github.com/golang/glog"
158-
"golang.org/x/net/context"
159162
"github.com/grpc-ecosystem/grpc-gateway/runtime"
160163
"google.golang.org/grpc"
161-
162-
gw "path/to/your_service_package"
164+
165+
gw "path/to/your_service_package" // Update
163166
)
164-
167+
165168
var (
166-
echoEndpoint = flag.String("echo_endpoint", "localhost:9090", "endpoint of YourService")
169+
// command-line options:
170+
// gRPC server endpoint
171+
grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:9090", "gRPC server endpoint")
167172
)
168-
173+
169174
func run() error {
170175
ctx := context.Background()
171176
ctx, cancel := context.WithCancel(ctx)
172177
defer cancel()
173-
178+
179+
// Register gRPC server endpoint
180+
// Note: Make sure the gRPC server is running properly and accessible
174181
mux := runtime.NewServeMux()
175182
opts := []grpc.DialOption{grpc.WithInsecure()}
176-
err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux, *echoEndpoint, opts)
183+
err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
177184
if err != nil {
178185
return err
179186
}
180-
181-
return http.ListenAndServe(":8080", mux)
187+
188+
// Start HTTP server (and proxy calls to gRPC server endpoint)
189+
return http.ListenAndServe(":8081", mux)
182190
}
183-
191+
184192
func main() {
185193
flag.Parse()
186194
defer glog.Flush()
187-
195+
188196
if err := run(); err != nil {
189197
glog.Fatal(err)
190198
}
191199
}
192200
```
193201
194-
7. (Optional) Generate swagger definitions
202+
7. (Optional) Generate swagger definitions using `protoc-gen-grpc-swagger`
195203
196204
```sh
197205
protoc -I/usr/local/include -I. \
@@ -226,7 +234,7 @@ More examples are available under `examples` directory.
226234
227235
To use the same port for custom HTTP handlers (e.g. serving `swagger.json`),
228236
gRPC-gateway, and a gRPC server, see
229-
[this code example by CoreOS](https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go)
237+
[this example by CoreOS](https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go)
230238
(and its accompanying [blog post](https://coreos.com/blog/gRPC-protobufs-swagger.html)).
231239
232240
## Features

0 commit comments

Comments
 (0)