gkit

package module
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2023 License: MIT Imports: 14 Imported by: 0

README

Gkit

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

Installation

To install gkit package, you need to install Go.

  1. You first need Go installed then you can use the below Go command to install gkit.
go get -u github.com/haoletesuji/gkit
  1. Import it in your code:
import "github.com/haoletesuji/gkit"

Quick start

The example how to use gkit 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
  }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidParam = errors.New("invalid parameter")
	ErrServer       = errors.New("server error")
	ErrUnauthorized = errors.New("unauthorized")
)
View Source
var (
	JWTAuthHeader = "Authorization"
	JaegerHeader  = "Trace-Id"
)
View Source
var (
	DefaultLimit int64 = 20
)

Functions

func ConstructKey

func ConstructKey(prefix, key string) string

func CorsMiddleware

func CorsMiddleware() gin.HandlerFunc

func ExtractTokenFromHeader

func ExtractTokenFromHeader(r *http.Request) string

func GetEnv added in v0.1.4

func GetEnv(key, fallback string) string

func GetPageNumber

func GetPageNumber(page string) int

func GetServerAddrs

func GetServerAddrs(addrs string) []string

func InitLogger

func InitLogger(mode string)

func Join

func Join(strs ...string) string

func LoggingMiddleware

func LoggingMiddleware(logger HttpLogger) gin.HandlerFunc

func MaxAllowed

func MaxAllowed(n int64) gin.HandlerFunc

Types

type Closer

type Closer interface {
	Close() error
}

type ErrResponse

type ErrResponse struct {
	Success bool   `json:"success" example:"false"`
	Error   string `json:"error"`
}

type GrpcServer

type GrpcServer interface {
	Register()
	Run()
	GracefulStop()
}

type HttpLogger

type HttpLogger struct {
	*log.Entry
}

func NewHttpLogger

func NewHttpLogger() HttpLogger

type HttpServer

type HttpServer interface {
	RegisterRoutes()
	Run()
	GracefulShutdown(ctx context.Context) error
}

type IDGenerator

type IDGenerator interface {
	NextID() (uint64, error)
}

func NewIDGenerator

func NewIDGenerator() (IDGenerator, error)

type Migrator added in v0.1.5

type Migrator interface {
	Migrate() error
}

type Pagination

type Pagination struct {
	Total int64 `json:"total"`
	Limit int64 `json:"limit"`
}

type Runner

type Runner interface {
	Run()
	GracefulShutdown(ctx context.Context) error
}

type SQLMigrator added in v0.1.6

type SQLMigrator struct {
	// contains filtered or unexported fields
}

func NewSQLMigrator added in v0.1.6

func NewSQLMigrator(migrator Migrator) *SQLMigrator

func (*SQLMigrator) Migrate added in v0.1.6

func (m *SQLMigrator) Migrate() error

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(name string, runner Runner, closer Closer) *Server

func (*Server) GracefulShutdown

func (s *Server) GracefulShutdown(ctx context.Context, done chan bool)

func (*Server) Serve

func (s *Server) Serve()

type SuccessPagingResponse

type SuccessPagingResponse[T any] struct {
	Success    bool       `json:"success" example:"true"`
	Data       T          `json:"data"`
	Pagination Pagination `json:"pagination"`
}

type SuccessResponse

type SuccessResponse[T any] struct {
	Success bool `json:"success" example:"true"`
	Data    T    `json:"data"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL