Template for REST/GRPC API server made with Golang.
Features:
- The log uses logrus
- Load middleware cross-domain, add header
- Graceful stop
- Close connection to DB before stop
- Response template error/success/success with pagination
To install gkit
package, you need to install Go.
- You first need Go installed then you can use the below Go command to install
gkit
.
go get -u github.com/haoletesuji/gkit
- Import it in your code:
import "github.com/haoletesuji/gkit"
The example how to use gkit
with to start REST api in example folder.
package main
import (
"example/wire"
"log"
)
func main() {
server, err := wire.InitializeServer("gkit_example")
if err != nil {
log.Fatal(err)
}
server.Serve()
}
# run main.go and visit 0.0.0.0:3000/api/health on browser
$ go run main.go
Pre defination response template:
- Error response:
GET
/api/error
type ErrResponse struct {
Success bool `json:"success" example:"false"`
Error string `json:"error"`
}
{
"success": false,
"error": "response error"
}
- Successfull response:
GET
/api/success
type SuccessResponse[T any] struct {
Success bool `json:"success" example:"true"`
Data T `json:"data"`
}
{
"success": true,
"data": [
{
"uid": "1",
"name": "John",
"profile_image": "https://google.com/image/1"
}
]
}
- Successfull with pagination:
GET
/api/success_paging
type Pagination struct {
Total int64 `json:"total"`
Limit int64 `json:"limit"`
}
type SuccessPagingResponse[T any] struct {
Success bool `json:"success" example:"true"`
Data T `json:"data"`
Pagination Pagination `json:"pagination"`
}
{
"success": true,
"data": [
{
"uid": "1",
"name": "John",
"profile_image": "https://google.com/image/1"
}
],
"pagination": {
"total": 1,
"limit": 10
}
}
The example how to use gkit
with to start grpc server in example folder.
# run main.go
$ go run main.go
Then you will see the following logging
INFO[0000] grpc server listening on 0.0.0.0:50001 type=grpc