Categorygithub.com/gilperopiola/grpc-gateway-impl
modulepackage
0.0.0-20241224000345-9d0ca11b7e83
Repository: https://github.com/gilperopiola/grpc-gateway-impl.git
Documentation: pkg.go.dev

# README

(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? ๐Ÿค”

โ–ท YESOFCOURSEHONEY โ€” BUUUUUT โ€” 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 -> 'ย '.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author