Skip to content

gilperopiola/grpc-gateway-impl

Repository files navigation

(This README needs updating 🐉)

🟢 GRPC Gateway Implementation

If an HTTP API is worth a dollar, its GRPC counterpart is like 20 cents more.

- Lionel Messi.

And now with GRPC Gateway we get both of them for only... $0.75!!! 😱 — With extra fries and a sizzling Monster Mango Loco™FREE OF CHARGE 🍟🥤

▷ ... What? 🤨 You're not making any sense. Why use emojis on a readme dude ❓

▷ 🙃🔪 — Let's say we define a UsersSvc in a .proto file with 2 endpoints. We can use GRPC Gateway to not also auto-generate a GRPC Server from our predefined UsersSvc — It also auto-generates an HTTP one, translating HTTP to GRPC and viceversa — 🤯 FOR FREE!!!

... 🕓 ... 🧠 ... But wasn't it $0.75? 🤔

YESOFCOURSEHONEYBUUUUUT — ifyoubuy RAIGHTO NAW wellthrowinthe — G-G-Gorgeous. C-C-Charming. B-B-B-B-BreathTAKING .... 🥁 ....HTTP Swagger Spec for your API. And I don't think I even have to mention it, but I'll say it anyways... Th-The Swagger... The Swagger S-Spec. The Swagger Spec... is... au-tO-GE-nE-RRA-ttTTTEDD 🤪 from annotations on the .proto 🎉 Even request validations are configured there, so there is practically no transport layer.

... ️️👁️_👁️ — ... 🤔 ... — OK — 👁️🤔👁️ — ... That's actually cool? ... Shut up and take my money.

So... What's in here? 👀

~I'm glad you ask! - We have two simple APIs: 1 GRPC & 1 HTTP, each of them with 4 endpoints --> Signup, Login, GetUser and GetUsers.

It leverages --> Clean, Hexagonal Architecture 🔷 / MySQL 🐬 / Patterns and Good Practices 📐 / Excellent Documentation 📚 / Gorm 🌱 / Centralized Error Handling 🎯 / JWT 🔑 / TLS 🔒 / RBAC 👑 / GCP ❌ / JJR ❓ / Y2K 🤔 / Swagger 📜 / BRB 🤦‍♂️ / LOL 😂 / Postman Automation 📬 / AFK 🏃‍♀️.

Request lifecycle 🔄

➡️ When a Signup HTTP Request hits the Gateway, the first file to be called is users.pb.gw.go on:

  • RegisterUsersSvcHandlerClient > request_UsersSvc_Signup_0

➡️ Then, when it needs to go through google.golang.org/grpc/server.go, it calls users_grpc.pb.go on:

  • usersSvcClient.Signup > _UsersSvc_Signup_Handler

➡️ Followed by our interceptors in grpc_interceptors.go:

  • rateLimiterInterc > loggerInterc > tokenValidationInterc > inputValidationInterc > etc...

➡️ To finally reach our beloved service_users.go on service.Signup.

Useful Commands ✍🏼

make all: Makes all.

🤪 It cleans the env, generates code, runs tests, and runs the app.

make all fast=1: Makes all, but faster. Skips cleaning and testing.

make help: Shows help message.

make run: Updates go.mod and runs app.

make generate: Based on the .proto files, generates the .pb.go files and Swagger Spec.

For more commands, check the Makefile. 🌈

Code Generation 🖥️

With protofiles your API gets defined before it's implemented.

Using custom annotations on the .proto file and tools like GRPC Gateway you get an HTTP Handler for each gRPC Method, each Handler decoding HTTP Requests into gRPC ones, calling their designated method on the gRPC server and encoding the gRPC Response back into HTTP.

You also get a Validation Layer based on the protofile. And did I mention the free Swagger? 😁 It's $11.99.

Plus taxes.

Auto Generated Files 🕸

Using the UsersSvc as an example

users.pb.go

Autogenerated PB Struct Types and their methods, as defined in the .proto.

  • SignupRequest / SignupResponse / LoginRequest / LoginResponse / UserInfo / PaginationInfo.

users_grpc.pb.go

GRPC Server and Client interfaces, endpoints registration.

type UsersSvcClient interface
type UsersSvcServer interface
func RegisterUsersSvcServer(...)

users.pb.gw.go

Reverse proxy, maps and decodes HTTP into GRPC and viceversa.

request_UsersSvc_Signup_0(...)
request_UsersSvc_Login_0(...)
request_UsersSvc_GetUser_0(...)
request_UsersSvc_GetUsers_0(...)

RegisterUsersSvcHandler(...)
RegisterUsersSvcHandlerFromEndpoint(...)

Qué hacés leyendo esto?! — Ponete a laburar 🤨

@gilperopiola


Non breaking space -> ' '.