• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.bazelci/H06-Nov-2019-

.circleci/H06-Nov-2019-

.github/H06-Nov-2019-

bin/H06-Nov-2019-

codegenerator/H06-Nov-2019-

docs/H06-Nov-2019-

examples/H06-Nov-2019-

internal/H06-Nov-2019-

protoc-gen-grpc-gateway/H06-Nov-2019-

protoc-gen-swagger/H06-Nov-2019-

runtime/H06-Nov-2019-

third_party/googleapis/H06-Nov-2019-

utilities/H06-Nov-2019-

.gitignoreH A D06-Nov-2019162

.goreleaser.ymlH A D06-Nov-2019590

BUILDH A D06-Nov-2019535

CHANGELOG.mdH A D06-Nov-2019117.7 KiB

CONTRIBUTING.mdH A D06-Nov-20191.5 KiB

ISSUE_TEMPLATE.mdH A D06-Nov-20192 KiB

MakefileH A D06-Nov-201910.8 KiB

README.mdH A D06-Nov-201911.3 KiB

WORKSPACEH A D06-Nov-20192 KiB

fuzzit.shH A D06-Nov-2019739

go.modH A D06-Nov-2019535

go.sumH A D06-Nov-20195.4 KiB

repositories.bzlH A D06-Nov-20195.4 KiB

README.md

1# grpc-gateway
2
3[![release](https://img.shields.io/github/release/grpc-ecosystem/grpc-gateway.svg?style=flat-square)](https://github.com/grpc-ecosystem/grpc-gateway/releases) [![CircleCI](https://img.shields.io/circleci/project/github/grpc-ecosystem/grpc-gateway/master.svg?style=flat-square)](https://circleci.com/gh/grpc-ecosystem/grpc-gateway) [![fuzzit](https://app.fuzzit.dev/badge?org_id=grpc-gateway)](https://app.fuzzit.dev/orgs/grpc-gateway/dashboard) [![coverage](https://img.shields.io/codecov/c/github/grpc-ecosystem/grpc-gateway/master.svg?style=flat-square)](https://codecov.io/gh/grpc-ecosystem/grpc-gateway) [![license](https://img.shields.io/github/license/grpc-ecosystem/grpc-gateway.svg?style=flat-square)](LICENSE.txt)
4
5The grpc-gateway is a plugin of the Google protocol buffers compiler
6[protoc](https://github.com/protocolbuffers/protobuf).
7It reads protobuf service definitions and generates a reverse-proxy server which
8'translates a RESTful HTTP API into gRPC. This server is generated according to the
9[`google.api.http`](https://github.com/googleapis/googleapis/blob/master/google/api/http.proto#L46)
10annotations in your service definitions.
11
12This helps you provide your APIs in both gRPC and RESTful style at the same time.
13
14![architecture introduction diagram](https://docs.google.com/drawings/d/12hp4CPqrNPFhattL_cIoJptFvlAqm5wLQ0ggqI5mkCg/pub?w=749&h=370)
15
16## Check out our [documentation](https://grpc-ecosystem.github.io/grpc-gateway/)!
17
18## Background
19gRPC is great -- it generates API clients and server stubs in many programming
20languages, it is fast, easy-to-use, bandwidth-efficient and its design is
21combat-proven by Google. However, you might still want to provide a traditional
22RESTful JSON API as well. Reasons can range from maintaining
23backwards-compatibility, supporting languages or clients not well supported by
24gRPC, to simply maintaining the aesthetics and tooling involved with a RESTful
25JSON architecture.
26
27This project aims to provide that HTTP+JSON interface to your gRPC service.
28A small amount of configuration in your service to attach HTTP semantics is all
29that's needed to generate a reverse-proxy with this library.
30
31## Installation
32
33The grpc-gateway requires a local installation of the Google protocol buffers
34compiler `protoc` v3.0.0 or above. Please install this via your local package
35manager or by downloading one of the releases from the official repository:
36
37https://github.com/protocolbuffers/protobuf/releases
38
39
40Then use `go get -u` to download the following packages:
41
42```sh
43go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
44go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
45go get -u github.com/golang/protobuf/protoc-gen-go
46```
47
48This will place three binaries in your `$GOBIN`;
49
50* `protoc-gen-grpc-gateway`
51* `protoc-gen-swagger`
52* `protoc-gen-go`
53
54Make sure that your `$GOBIN` is in your `$PATH`.
55
56## Usage
57
581. Define your [gRPC](https://grpc.io/docs/) service using protocol buffers
59
60   `your_service.proto`:
61   ```protobuf
62   syntax = "proto3";
63   package example;
64   message StringMessage {
65     string value = 1;
66   }
67
68   service YourService {
69     rpc Echo(StringMessage) returns (StringMessage) {}
70   }
71   ```
72
732. Add a [`google.api.http`](https://github.com/googleapis/googleapis/blob/master/google/api/http.proto#L46)
74annotation to your .proto file
75
76   `your_service.proto`:
77   ```diff
78    syntax = "proto3";
79    package example;
80   +
81   +import "google/api/annotations.proto";
82   +
83    message StringMessage {
84      string value = 1;
85    }
86
87    service YourService {
88   -  rpc Echo(StringMessage) returns (StringMessage) {}
89   +  rpc Echo(StringMessage) returns (StringMessage) {
90   +    option (google.api.http) = {
91   +      post: "/v1/example/echo"
92   +      body: "*"
93   +    };
94   +  }
95    }
96   ```
97
98   If you do not want to modify the proto file for use with grpc-gateway you can
99   alternatively use an external
100   [gRPC Service Configuration](https://cloud.google.com/endpoints/docs/grpc/grpc-service-config) file.
101   [Check our documentation](https://grpc-ecosystem.github.io/grpc-gateway/docs/grpcapiconfiguration.html)
102   for more information.
103
1043. Generate gRPC stub
105
106   The following generates gRPC code for Golang based on `path/to/your_service.proto`:
107   ```sh
108   protoc -I/usr/local/include -I. \
109     -I$GOPATH/src \
110     -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
111     --go_out=plugins=grpc:. \
112     path/to/your_service.proto
113   ```
114
115   It will generate a stub file `path/to/your_service.pb.go`.
116
1174. Implement your service in gRPC as usual
118
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`:
122     ```sh
123     protoc -I/usr/local/include -I. \
124       -I$GOPATH/src \
125       -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
126       --ruby_out=. \
127       path/to/your_service.proto
128
129     protoc -I/usr/local/include -I. \
130       -I$GOPATH/src \
131       -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
132       --plugin=protoc-gen-grpc=grpc_ruby_plugin \
133       --grpc-ruby_out=. \
134       path/to/your_service.proto
135     ```
136   2. Add the googleapis-common-protos gem (or your language equivalent) as a dependency to your project.
137   3. Implement your gRPC service stubs
138
1395. Generate reverse-proxy using `protoc-gen-grpc-gateway`
140
141   ```sh
142   protoc -I/usr/local/include -I. \
143     -I$GOPATH/src \
144     -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
145     --grpc-gateway_out=logtostderr=true:. \
146     path/to/your_service.proto
147   ```
148
149   It will generate a reverse proxy `path/to/your_service.pb.gw.go`.
150
1516. Write an entrypoint for the HTTP reverse-proxy server
152
153   ```go
154   package main
155
156   import (
157     "context"  // Use "golang.org/x/net/context" for Golang version <= 1.6
158     "flag"
159     "net/http"
160
161     "github.com/golang/glog"
162     "github.com/grpc-ecosystem/grpc-gateway/runtime"
163     "google.golang.org/grpc"
164
165     gw "path/to/your_service_package"  // Update
166   )
167
168   var (
169     // command-line options:
170     // gRPC server endpoint
171     grpcServerEndpoint = flag.String("grpc-server-endpoint",  "localhost:9090", "gRPC server endpoint")
172   )
173
174   func run() error {
175     ctx := context.Background()
176     ctx, cancel := context.WithCancel(ctx)
177     defer cancel()
178
179     // Register gRPC server endpoint
180     // Note: Make sure the gRPC server is running properly and accessible
181     mux := runtime.NewServeMux()
182     opts := []grpc.DialOption{grpc.WithInsecure()}
183     err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux,  *grpcServerEndpoint, opts)
184     if err != nil {
185       return err
186     }
187
188     // Start HTTP server (and proxy calls to gRPC server endpoint)
189     return http.ListenAndServe(":8081", mux)
190   }
191
192   func main() {
193     flag.Parse()
194     defer glog.Flush()
195
196     if err := run(); err != nil {
197       glog.Fatal(err)
198     }
199   }
200   ```
201
2027. (Optional) Generate swagger definitions using `protoc-gen-swagger`
203
204   ```sh
205   protoc -I/usr/local/include -I. \
206     -I$GOPATH/src \
207     -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
208     --swagger_out=logtostderr=true:. \
209     path/to/your_service.proto
210   ```
211
212## Parameters and flags
213`protoc-gen-grpc-gateway` supports custom mapping from Protobuf `import` to
214Golang import paths. They are compatible to
215[the parameters with same names in `protoc-gen-go`](https://github.com/golang/protobuf#parameters)
216(except `source_relative`).
217
218In addition we also support the `request_context` parameter in order to use the
219`http.Request`'s Context (only for Go 1.7 and above). This parameter can be
220useful to pass request scoped context between the gateway and the gRPC service.
221
222`protoc-gen-grpc-gateway` also supports some more command line flags to control
223logging. You can give these flags together with parameters above. Run
224`protoc-gen-grpc-gateway --help` for more details about the flags.
225
226## More Examples
227More examples are available under `examples` directory.
228* `proto/examplepb/echo_service.proto`, `proto/examplepb/a_bit_of_everything.proto`, `proto/examplepb/unannotated_echo_service.proto`: service definition
229  * `proto/examplepb/echo_service.pb.go`, `proto/examplepb/a_bit_of_everything.pb.go`, `proto/examplepb/unannotated_echo_service.pb.go`: [generated] stub of the service
230  * `proto/examplepb/echo_service.pb.gw.go`, `proto/examplepb/a_bit_of_everything.pb.gw.go`, `proto/examplepb/uannotated_echo_service.pb.gw.go`: [generated] reverse proxy for the service
231  * `proto/examplepb/unannotated_echo_service.yaml`: gRPC API Configuration for ```unannotated_echo_service.proto```
232* `server/main.go`: service implementation
233* `main.go`: entrypoint of the generated reverse proxy
234
235To use the same port for custom HTTP handlers (e.g. serving `swagger.json`),
236gRPC-gateway, and a gRPC server, see
237[this example by CoreOS](https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go)
238(and its accompanying [blog post](https://coreos.com/blog/grpc-protobufs-swagger.html)).
239
240## Features
241
242### Supported
243
244* Generating JSON API handlers.
245* Method parameters in request body.
246* Method parameters in request path.
247* Method parameters in query string.
248* Enum fields in path parameter (including repeated enum fields).
249* Mapping streaming APIs to newline-delimited JSON streams.
250* Mapping HTTP headers with `Grpc-Metadata-` prefix to gRPC metadata (prefixed with `grpcgateway-`)
251* Optionally emitting API definitions for
252[OpenAPI (Swagger) v2](https://swagger.io/docs/specification/2-0/basic-structure/).
253* Setting [gRPC timeouts](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests)
254through inbound HTTP `Grpc-Timeout` header.
255* Partial support for [gRPC API Configuration](https://cloud.google.com/endpoints/docs/grpc/grpc-service-config)
256files as an alternative to annotation.
257* Automatically translating PATCH requests into Field Mask gRPC requests. See
258[the docs](https://grpc-ecosystem.github.io/grpc-gateway/docs/patch.html)
259for more information.
260
261### No plan to support
262But patch is welcome.
263* Method parameters in HTTP headers.
264* Handling trailer metadata.
265* Encoding request/response body in XML.
266* True bi-directional streaming.
267
268# Mapping gRPC to HTTP
269
270* [How gRPC error codes map to HTTP status codes in the response](https://github.com/grpc-ecosystem/grpc-gateway/blob/master/runtime/errors.go#L15).
271* HTTP request source IP is added as `X-Forwarded-For` gRPC request header.
272* HTTP request host is added as `X-Forwarded-Host` gRPC request header.
273* HTTP `Authorization` header is added as `authorization` gRPC request header.
274* Remaining Permanent HTTP header keys (as specified by the IANA
275[here](http://www.iana.org/assignments/message-headers/message-headers.xhtml)
276are prefixed with `grpcgateway-` and added with their values to gRPC request
277header.
278* HTTP headers that start with 'Grpc-Metadata-' are mapped to gRPC metadata
279(prefixed with `grpcgateway-`).
280* While configurable, the default {un,}marshaling uses
281[jsonpb](https://godoc.org/github.com/golang/protobuf/jsonpb) with
282`OrigName: true`.
283
284# Contribution
285See [CONTRIBUTING.md](http://github.com/grpc-ecosystem/grpc-gateway/blob/master/CONTRIBUTING.md).
286
287# License
288grpc-gateway is licensed under the BSD 3-Clause License.
289See [LICENSE.txt](https://github.com/grpc-ecosystem/grpc-gateway/blob/master/LICENSE.txt) for more details.
290