As a passionate about scalable project structure with a robust NestJS background, when I began working with Go, I missed some of the default features of this framework, like configuration module, global services, database connection, dependencies injections, etc., so I asked myself if a can apply the same techniques in Golang without any framework and the simplicity of Go… and I got it!
Welcome to the first article of this series. Take a cafe because here we go!
We are going to create a basic login gRPC API, and when you finish, you will know how
Standard gRPC API features are implemented in Golang
Server Configuration and start gRPC server:
Database and common project structure:
- Use GORM to connect to Postgres and migrate your models
- Use Wire to manage dependencies
- Create project structure gRPC -> Service -> Repository -> DAO
Basic login gRPC API with JWT and Evans CLI :
- Implement user creation
- Use Evans CLI client for user creation
- Implements Hash/Check Password
gRPC interceptors, global services, and private/public RPC methods guarded with JWT:
- Implement interceptors(Authorization interceptor & logging interceptor)
- Register public and private gRPC methods with interceptors
- Implements method service + JWT for guard gRPC method
- Create a global service in using context
Protocol Buffer, Configuration, and start Golang gRPC server
We are going to start with Protocol Buffer with Buf and Makefile for automatization
Create the project folder workspace:
$ mkdir logic_grpc && cd logic_grpc
Install protocol buffer compiler
protoc, here for more details
$ brew install protobuf
$ protoc --version # Ensure compiler version is 3+
Install Golang gRPC compiler plugins
$ go install google.golang.org/protobuf/cmd/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]
PATH so that the
protoc compiler can find the plugins:
$ export PATH="$PATH:$HOME/.local/bin"
Nice! now let’s define our protocol buffer file. Create a folder at the root level called api, and inside the folder, create a file named auth.proto and paste the following code.