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
.
$ go run main.go
An OpenAPI UI is served on https://0.0.0.0:11000/.
If you want to use a separate gRPC server, for example one written in Java or C++, you can run the standalone web server instead:
$ go run ./cmd/standalone/ --server-address dns:///0.0.0.0:10000
After cloning the repo, there are a couple of initial steps;
- Install
buf
withmake install
, which is necessary for us to generate the Go and OpenAPIv2 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):- Open
Makefile
and replaceNEW_REPO_PATH
with what your new repository path is:then perform# TODO: Replace with your new repo name NEW_REPO_PATH=yourscmprovider.com/youruser/yourrepo # ...
$ make adjust_template
- Or perform the following in your shell
$ find . -path ./vendor -prune -o -type f \( -name '*.go' -o -name '*.proto' \) -exec sed -i -e "s;github.com/jasonkolodziej/octo-broccoli;yourscmprovider.com/youruser/yourrepo;g" {} +
- Open
- Finally, generate the files with
make generate
.
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/asset.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/asset.pb.go
.
This should hopefully be all you need to get started playing around with the gRPC-Gateway!