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:
- Basic tutorial for Python gRPC: https://www.velotio.com/engineering-blog/grpc-implementation-using-python
- Information about
grpc-gateway
: https://github.com/grpc-ecosystem/grpc-gateway/
Things I would like to improve:
google/
directory containing a copy of some proto definitions should be moved underproto/third_party
;- Python auto generated code should go under
src/python_grpc/autogenerated
, notsrc/python_grpc/
;
Tested with Docker version 18.03.1-ce, build 9ee9f40
. Note TLS is enabled when
running via Docker.
./docker-build.sh
./docker-run.sh
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.
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
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
python src/python_grpc/server.py &
python src/python_grpc/client.py
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
cd src/go_rest_proxy/autogenerated/proto/
go mod init github.com/thiagorobert/grpc-example/autogenerated_proto
go mod tidy
cd -
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
go build src/go_rest_proxy/rest_reverse_proxy.go
./rest_reverse_proxy &
curl -X POST localhost:8081/v1/response -d '{"message": "test"}'
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.