Giter Club home page Giter Club logo

grpc's Introduction

gRPC Examples

Examples for using protocol buffers and gRPC. Includes:

  1. Protocol buffers for an example CRUD service
  2. Go server and client implementations of the CRUD service
  3. Swagger for the CRUD service
  4. A gRPC gateway bridge that registers the CRUD service
  5. A Typescript gRPC web client for the CRUD service

NOTE The following commands should be run from the root of the project.

Protocol Buffers

Install the protobuf compiler, protoc.

Additional documentation on developers.google.com

Go compilation

Install the Go protoc plugin:

go get -u github.com/golang/protobuf/...

The following command will generate the gRPC server and client stubs for Golang:

protoc \
    --proto_path=./proto \
    --go_out=plugins=grpc:./go \
    ./proto/*.proto

It compiled the *.proto files in the /proto directory and adds the generated Go code to the /go directory.

See server.go for the example server.

See client.go for an example client.

Swagger

Install the protoc swagger plugin:

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

Swagger can then be generated with:

protoc \
    --proto_path=./proto \
    --swagger_out=logtostderr=true:./swagger \
    ./proto/*.proto

The swagger can be viewed using the Swagger editor.

Gateway

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
protoc \
    -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
    --proto_path=./proto \
    --grpc-gateway_out=logtostderr=true:./go \
    ./proto/*.proto

The previous commands can also be combined into one:

protoc \
    -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
    --proto_path=./proto \
    --go_out=plugins=grpc:./go \
    --grpc-gateway_out=logtostderr=true:./go \
    --swagger_out=logtostderr=true:./swagger \
    ./proto/*.proto

See client.go for an example client.

gRPC Web

See /grpcweb.

Typescript compilation

To produce the Typescript required for gRPC web, install the tools in package.json

protoc \
    --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
    --js_out=import_style=commonjs,binary:./js \
    --ts_out=service=true:./js \
    -I ./proto \
    proto/*.proto proto/google/**/*.proto

The client bundle is then built with webpack:

npx webpack

Miscellaneous

Square's protowrap tool helps when compiling multiple Go protoc packages.

go get -u github.com/square/goprotowrap/cmd/protowrap

More examples

gRPC Gateway's "a bit of everything" example

Auth

Example gRPC auth methods

TLS

Since gRPC uses HTTP 2, which requires HTTPS, knowing how to self-sign TLS certificates can help with local development.

Self-signed certificates

subjectAltName is needed for Chrome 58+

openssl req -x509 -sha256 -nodes -newkey rsa:2048 -days 1024 -subj "/C=US/ST=CO/O=AARON ELLIS LLC/CN=localhost" -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:localhost")) -keyout localhost.key -out localhost.crt

Add the certificate to chrome using this guide

Or enable Chrome's insecure localhost setting: chrome://flags/#allow-insecure-localhost

Happy hacking!

aodin, 2018

grpc's People

Contributors

aodin avatar

Watchers

James Cloos avatar RJ Nanjegowda avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.