Golang gRPC starter kit

Mohamed Mohamed
3 min readFeb 22, 2019

--

TL;DR If you struggled like me to describe your API with Protobuf then generate your Golang gRPC client, server, REST gateway, swagger description and have all of them run together nicely in a docker container, then, you are in the right place.

I started my journey with Golang since a while now and every now and then I find very useful features and started to add them to my day to day pipeline and work habits to enhance my productivity. After all, one of the pros of Golang is that it helps developers to boost their productivity. One of the practices that I started following and turned out to be really efficient is to start by describing my APIs using protobuf. Afterwards, I can generate the gRPC and REST gateway as well as the Swagger description.

gRPC is very fast compared to REST but there are many scenarios where a gRPC client is a hard objective to have (as far as I know when I am writing this blog). This is where the REST gateway comes in. It is a light weight server that is generated to run with the gRPC and all what it does is to proxy the REST calls to it (the gRPC server). In order to make this API easy to use, a nice documentation is always handy and it would be better if it is automatically generated for you. The swagger plugin allows to do that and keep your API well described and easy to share with others.

To get familiar with this process, this blog post was very helpful to me and contains a very good demo of using gRPC to build a robust client/server application. Herein, I am adding the hosting of the swagger description with the server and the possibility to have all of that in one docker container.

The needed resources are in this Github repository. After cloning the repository, you can start by editing the protobuf description to make it fit your API. I kept it very simple to create and delete object so it is very easy to follow.

When ready you can use make to generate the server, the client, and the swagger api description from the root of the repository.

$ make

Next step is to build the docker container that will host all of these components. In the root of the project you can run the following command to build and run the container.

$ docker build -t my-nice-app .

The docker file is using multi-stage build process and will end up with a very small container image. When the build is done, you can start your container:

$ docker run -p 7788:7788 -p 7789:7789 my-nice-app

If everything is right you should be able to go to http://localhost:7789/swaggerui to see your swagger UI and try out your methods. It should look something like:

Congratulations, you just started your Golang app with Protobuf, gRPC, REST gateway and a fancy swagger description all in a docker container. Now you can enhance your api and use ‘make’ to update your generated components.

I hope this blog was useful and that it will help you to get started. Please feel free to comment and reach out over Linkedin.

--

--