Generate RESTful services using

gRPC-Gateway

Rajiv Ranjan Singh
JSSATE Bengaluru

India 🇮🇳

GopherCon Europe 2022

Introduction

We all know that gRPC is not a tool for everything. There are cases where we still want to provide a traditional HTTP/JSON API. The reasons can range from maintaining backward compatibility to supporting programming languages or clients not well supported by gRPC. But writing another service just to expose an HTTP/JSON API is quite a time-consuming and tedious task.

Prerequisites: gRPC, HTTP/JSON, Protobuf

About gRPC-Gateway

The gRPC-Gateway is a plugin of the Google protocol buffers compiler protoc. It reads protobuf service definitions and generates a reverse-proxy server which translates a RESTful HTTP API into gRPC. This server is generated according to the google.api.http annotations in your service definitions.

This helps you provide your APIs in both gRPC and HTTP/JSON format at the same time.

How does it work?

  • A plugin of protobuf compiler
  • Generate proxy codes from protobuf
  • Translates HTTP JSON Call to gRPC
  • In-process translation: only for unary gRPC
  • Separate proxy server: both unary and streaming gRPC

Architecture of gRPC-Gateway

Demonstration Hello World gRPC-Gateway

  • Defining Hello World gRPC service using protocol buffers.
  • Creating main.go where we will write our client and server logic.
  • Adding gRPC-Gateway annotations to an existing proto file.
  • Generating the gRPC-Gateway stubs.

References

Thank You, 🙏 🎉