All the boilerplate you need to get started with writing grpc-gateway powered REST services in Go.
Running main.go
starts a web server on https://0.0.0.0:11000/. You can configure
the port used with the $PORT
environment variable, and to serve on HTTP set
$SERVE_HTTP=true
.
An OpenAPI UI is served on https://0.0.0.0:11000/.
Generating the files requires the protoc
protobuf compiler.
Please install it according to the
installation instructions
for your specific platform.
After cloning the repo, there are a couple of initial steps;
- Install the generate dependencies with
make install
. This will installprotoc-gen-go
,protoc-gen-grpc-gateway
,protoc-gen-openapiv2
andstatik
which are necessary for us to generate the Go, swagger and static files. - If you forked this repo, or cloned it into a different directory from the github structure,
you will need to correct the import paths. Here's a nice
find
one-liner for accomplishing this (replaceyourscmprovider.com/youruser/yourrepo
with your cloned repo path):$ find . -path ./vendor -prune -o -type f \( -name '*.go' -o -name '*.proto' \) -exec sed -i -e "s;github.com/johanbrandhorst/grpc-gateway-boilerplate;yourscmprovider.com/youruser/yourrepo;g" {} +
- Finally, generate the files with
make generate
. If you encounter an error here, make sure you've installedprotoc
and it is accessible in your$PATH
, and make sure you've performed step 1.
Now you can run the web server with go run main.go
.
The next step is to define the interface you want to expose in
proto/example.proto
. See https://developers.google.com/protocol-buffers/
tutorials and guides on writing protofiles.
Once that is done, regenerate the files using
make generate
. This will mean you'll need to implement any functions in
server/server.go
, or else the build will fail since your struct won't
be implementing the interface defined by the generated file in proto/example.pb.go
.
This should hopefully be all you need to get started playing around with the gRPC-Gateway!
Tracing using OpenCensus and exporting the traces to AWS X-Ray
The environment variable $AWS_REGION
has to be set to the region you want to export your traces to.
You must have the X-Ray daemon running locally