gRPC to JSON proxy generator following the gRPC HTTP spec
Clone or download
Pull request Compare This branch is 628 commits ahead, 99 commits behind grpc-ecosystem:master.
Latest commit 5e70d2d Jun 28, 2018
Permalink
Failed to load latest commit information.
.travis Use a released version of protoc Aug 16, 2016
api update version for golang protobuf Jun 27, 2018
bin ci: add codecov (grpc-ecosystem#514) Jan 3, 2018
core update version for golang protobuf Jun 27, 2018
examples Changed to use more appropriate http status code for ResourceExhausted ( Apr 3, 2018
options Deprecate the old custom option May 4, 2015
protoc-gen-grpc-gateway Use importPath to set package name rather than package path. (#537) Feb 23, 2018
protoc-gen-swagger Omit well-known type definitions from swagger output (#541) Mar 12, 2018
protocol Merge commit '316ff314a61b62527b6a5f821a5eb0190e4bdf39' Jun 26, 2018
runtime Changed to use more appropriate http status code for ResourceExhausted ( Apr 3, 2018
swagger/api add swagger generate srcipt and swagger definitions Apr 12, 2018
third_party/googleapis Update upstream proto files and add google.golang.org/genproto suppor… Mar 14, 2017
tron_http add Extension Jun 25, 2018
utilities Revert "Change imports so that it uses the right packages." Feb 18, 2017
vendor update version for golang protobuf Jun 27, 2018
.gitignore Add proto marshaller for proto-over-http (#459) Nov 16, 2017
.travis.yml Update regex to actually match go versions Apr 9, 2018
CHANGELOG.md CHANGELOG: update for 1.3.1 release (grpc-ecosystem#509) Dec 23, 2017
CONTRIBUTING.md []byte in query now uses base64.StdEncoding Mar 8, 2018
ISSUE_TEMPLATE.md Add an issue template Apr 8, 2018
LICENSE.txt add license Apr 7, 2015
Makefile Omit well-known type definitions from swagger output (#541) Mar 12, 2018
README.md add GOPATH guide Jun 28, 2018
gen-proto.sh support http get method Apr 13, 2018
glide.lock update version for golang protobuf Jun 27, 2018
glide.yaml add glide dependency config Apr 12, 2018
main update proto May 14, 2018
test.go Update test.go Apr 12, 2018

README.md

Requirements

Go 1.6 or higher

You can download the binary release from releases or compile the source release.

Make sure that your $GOPATH/bin is in your $PATH.

(Optional)ProtocolBuffers 3.0.0-beta-3 or later

In project, we have provided gRPC stub and reverse-proxy so that this step is optional.

If you want to generate gRPC stub and reverse-proxy by yourself, you need to install ProtocolBuffers 3.0.0-beta-3 or later.

You can download the binary release from releases or compile the source release.

cd /tmp
wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-all-3.5.1.tar.gz
tar -xzvf protobuf-all-3.5.1.tar.gz
cd protobuf-3.5.1 && ./configure --prefix=$HOME/protobuf && make && make install
export PATH=$PATH:./protobuf/bin

note: you can choose to download other os realeases on https://github.com/google/protobuf/releases instead of linux realease

Update code

go get -u github.com/tronprotocol/grpc-gateway

Usage

  1. Make sure your Tron grpc serivice has been started on localhost:50051 , you can visit How to build for starting Tron service.
  2. Get the source code and change word dir (Strongly suggest update your code before you start the service on your server)
# download project
go get -u github.com/tronprotocol/grpc-gateway

# change to project dir
cd $GOPATH/src/github.com/tronprotocol/grpc-gateway
  1. (Optional) Generate gRPC stub and reverse-proxy. Make sure you have installed protoc
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go
./gen-proto.sh
  1. run proxy-server. Make sure your code is lastest version.
go run tron_http/main.go
or
go run tron_http/main.go -port 50051 -host localhost

# listen is your http port
go run tron_http/main.go -port 50051 -host 10.0.8.214 -listen 8080
  1. Test API of tron http
curl -X POST -k http://localhost:8086/wallet/listwitnesses

If you get witness-list json data, congratulations

Note: json to protobuf, bytes should be passed via base64 formate

grpc-gateway

Build Status

grpc-gateway is a plugin of protoc. It reads gRPC service definition, and generates a reverse-proxy server which translates a RESTful JSON API into gRPC. This server is generated according to custom options in your gRPC definition.

It helps you to provide your APIs in both gRPC and RESTful style at the same time.

architecture introduction diagram

Background

gRPC is great -- it generates API clients and server stubs in many programming languages. It is fast, easy-to-use, bandwidth-efficient and its design is combat-proven by Google. However, you might still want to provide a traditional RESTful API as well. Reasons can range from maintaining backwards-compatibility, supporting languages or clients not well supported by gRPC to simply maintaining the aesthetics and tooling involved with a RESTful architecture.

This project aims to provide that HTTP+JSON interface to your gRPC service. A small amount of configuration in your service to attach HTTP semantics is all that's needed to generate a reverse-proxy with this library.

Parameters and flags

protoc-gen-grpc-gateway supports custom mapping from Protobuf import to Golang import path. They are compatible to the parameters with same names in protoc-gen-go.

In addition, we also support the request_context parameter in order to use the http.Request's Context (only for Go 1.7 and above). This parameter can be useful to pass request scoped context between the gateway and the gRPC service.

protoc-gen-grpc-gateway also supports some more command line flags to control logging. You can give these flags together with parameters above. Run protoc-gen-grpc-gateway --help for more details about the flags.

Features

Supported

  • Generating JSON API handlers
  • Method parameters in request body
  • Method parameters in request path
  • Method parameters in query string
  • Enum fields in path parameter (including repeated enum fields).
  • Mapping streaming APIs to newline-delimited JSON streams
  • Mapping HTTP headers with Grpc-Metadata- prefix to gRPC metadata (prefixed with grpcgateway-)
  • Optionally emitting API definition for Swagger.
  • Setting gRPC timeouts through inbound HTTP Grpc-Timeout header.

Mapping gRPC to HTTP

  • How gRPC error codes map to HTTP status codes in the response
  • HTTP request source IP is added as X-Forwarded-For gRPC request header
  • HTTP request host is added as X-Forwarded-Host gRPC request header
  • HTTP Authorization header is added as authorization gRPC request header
  • Remaining Permanent HTTP header keys (as specified by the IANA here are prefixed with grpcgateway- and added with their values to gRPC request header
  • HTTP headers that start with 'Grpc-Metadata-' are mapped to gRPC metadata (prefixed with grpcgateway-)
  • While configurable, the default {un,}marshaling uses jsonpb with OrigName: true.

Contribution

See CONTRIBUTING.md.

License

grpc-gateway is licensed under the BSD 3-Clause License. See LICENSE.txt for more details.