Generating HTTP handler using protobuf
In today’s tech landscape, Protocol Buffers (or protobuf for short) is a term most developers have come across. Developed by Google, protobuf is a language-agnostic format that acts as a contract between client and server, simplifying data serialization across various programming languages like Java, Python, Go, and more.
Typically, protobuf is paired with gRPC for server generation, allowing us to create a robust RPC-based server. But here’s a question: can we generate an HTTP server directly using protobuf? The answer is yes! There’s a Go library called gRPC Gateway (a.k.a grpc-gateway) that’s been around since August 2016, making it possible to serve HTTP endpoints alongside gRPC. However, grpc-gateway requires setting up a gRPC server first, which can add unnecessary complexity if gRPC isn’t strictly needed.
To address this, I’d like to introduce POT. POT is a code generator designed to create HTTP handlers directly from protobuf syntax, bypassing the need for a gRPC server. It’s a fresh project (just over a week old), but even in its early stage, POT has the potential to cut down on overhead by generating pure HTTP handlers. For now, POT has only been tested with the Echo HTTP framework, but it’s designed to support additional libraries as it evolves.
Curious? Check out the repository and give POT a try!
POT repository: https://github.com/afikrim/pot
Usage with POT
To get started with POT, first clone the POT repository and navigate to the example folder.
$ git clone — branch main — depth 1 https://github.com/afikrim/pot.git
$ cd pot/exampleIn the example project, you’ll find the following folder structure:
example
├── cmd
│ ├── api-client
│ │ └── main.go
│ └── api-server
│ └── main.go
├── generate.sh
├── go.mod
├── go.sum
├── handler
│ └── blog.go
└── protoHere’s a breakdown of each directory and file:
cmd:
api-client: CLI tool to test the client generated by protobufapi-server: CLI tool to start the HTTP server
generate.sh: A script to help you generate protobuf files into HTTP handlers
Get Aziz Fikri Mahmudi’s stories in your inbox
Join Medium for free to get updates from this writer.
handler: Contains implementations of the generated interface from the protobuf file
proto: Directory with protocol buffer definitions
Generate Handlers
To generate the HTTP handlers, run generate.sh. This script is filled with the following commands:
Once you run generate.sh, a new folder will appear in the handler directory:
handler
├── blog.go
└── pb
└── blog
├── blog.pb.go
└── blog_http.pb.goThe files inside handler/pb are generated by POT and should not be edited manually. If you encounter any issues, you can run go mod tidy to ensure dependencies are correctly managed.
The main implementations for the generated handlers (like blog_http.pb.go) should be placed in files like handler/blog.go.
Register Handler to Server
The generated handler can be registered to an HTTP server. Here’s an example of registering it with the native ServeMux HTTP router:
Using POT with Echo
Here’s how you can register the generated handler using the Echo HTTP framework:
With these steps, you’re now set up to generate HTTP handlers using POT and integrate them into a server, whether you’re using the standard ServeMux router or the Echo framework.








