Skip to content

thiagorobert/grpc-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gRPC example code

Background

I'm brushing up on modern Web API technologies, including gRPC and OpenAPI. This example has been useful to me (once I got it working) so I'm checkpointing it and making it available publicly - hope its also useful to others!

See also:

Things I would like to improve:

  • google/ directory containing a copy of some proto definitions should be moved under proto/third_party;
  • Python auto generated code should go under src/python_grpc/autogenerated, not src/python_grpc/;

Working with the code

Docker

Tested with Docker version 18.03.1-ce, build 9ee9f40. Note TLS is enabled when running via Docker.

Build image
./docker-build.sh
Run it
./docker-run.sh

Manually

Tested with Python 3.7.3 and go1.17.5 on linux/amd64.

Use commands below in sequence to generate required code from the proto definition and start/test the gRPC and REST proxy services.

Clean up all generated files
rm -rf src/go_rest_proxy/autogenerated/*
rm -rf src/python_grpc/proto
rm -rf src/python_grpc/google
rm -f go.mod go.sum
rm -f rest_reverse_proxy
Generate proto code for Python
python -m grpc_tools.protoc \
  -I=. \
  --python_out=./src/python_grpc \
  --grpc_python_out=./src/python_grpc \
  ./google/api/http.proto ./google/api/annotations.proto \
  ./proto/unary.proto
Start Python gRPC server in the background
python src/python_grpc/server.py &
Run Python gRPC client to test the server
python src/python_grpc/client.py
Generate proto code for Go
protoc \
  -I=. \
  --go_out ./src/go_rest_proxy/autogenerated/ --go_opt paths=source_relative \
  --go-grpc_out ./src/go_rest_proxy/autogenerated/ --go-grpc_opt paths=source_relative \
  --grpc-gateway_out ./src/go_rest_proxy/autogenerated/ \
  --grpc-gateway_opt logtostderr=true \
  --grpc-gateway_opt paths=source_relative \
  ./proto/unary.proto
Create Go module for the autogenerated proto code
cd src/go_rest_proxy/autogenerated/proto/
go mod init github.com/thiagorobert/grpc-example/autogenerated_proto
go mod tidy
cd -
Create Go module for the REST reverse proxy
go mod init github.com/thiagorobert/grpc-example
echo "replace github.com/thiagorobert/grpc-example/autogenerated_proto => ./src/go_rest_proxy/autogenerated/proto/" >> go.mod
go mod tidy -compat=1.17
Build REST proxy
go build src/go_rest_proxy/rest_reverse_proxy.go
Start REST proxy in the background
./rest_reverse_proxy &
Query REST API
curl -X POST localhost:8081/v1/response -d '{"message": "test"}'

TLS

Use flag --cert_path to enable TLS. Note the Python gRPC server will also require flag --private_key_path. TLS data (certificate, private key) is available under tls_data directory.

About

gRPC example code.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published