bullets

module
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT

README

bullets

there is no silver bullet but sometimes may be helpful,

usage

  • gin middlewares: print request and handle panic
package main

import (
    "github.com/gin-gonic/gin"
    "github.com/yuchanns/bullets/common/middlewares"
)

func main() {
    engine := gin.New()
    engine.Use(
        middlewares.NewDefaultRequestInterceptor(),
        middlewares.NewDefaultRequestInterceptor(),
    )
}
  • log
package main

import "github.com/yuchanns/bullets/common"

func main() {
    common.Logger.
        Fields(map[string]interface{}{"foo": "bar"}).
    	DebugInfo("debug")
}
  • return json response and log regular error
package main

import (
    "github.com/gin-gonic/gin"
    "github.com/pkg/errors"
    "github.com/yuchanns/bullets/common"
    "os"
)

func openNotExistFile() error {
	if _, err := os.Open("NOT_EXIST_FILE"); err != nil {
        // must wrap error with pkg/errors
		return errors.Wrap(err, "open error")
	}
	return nil
}

func OpenNotExistFileHandler(ctx *gin.Context) {
	if err := openNotExistFile(); err != nil {
		common.JsonFailWithStack(ctx, err, nil)
		return
	}
	common.JsonSuccess(ctx, "success", nil)
}
  • opentracing
closeOpenTracerFunc, openTracerMiddleware, err := BuildOpenTracerInterceptor("testOpenTrace", os.Getenv("AGENT_HOSTPORT"), []byte("api-request-"))
	if err != nil {
		panic(err)
	}
	defer closeOpenTracerFunc()
	engine := gin.New()
	engine.Use(openTracerMiddleware)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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