Skip to content

nick887/customize_router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Customize router

it is a encapsulation of gin router

demo

func main() {
    r := gin.Default()
    cr := &custom.CustmoRouter{IRouter: r}
    customRclt.InitCustmoR(cr)
	UseContentRouter(cr)
	_ = r.Run(":9999")
}


func UseContentRouter(r *custom.CustmoRouter) {
    a := r.GroupC("/api")
    {
        hole := a.GroupC("/hole")
        {
        // the second handler must be type of RequestHandler
        // handlers after the second must be type of gin.HandlerFunc
        // gin.HandlerFunc will excute first
        hole.GETC("/hello",&HelloRequest{} ,Hello)
        }
    }
}

func Hello(ctx *gin.Context) (statusCode *http_status.HttpStatus, json interface{}) {
    return &http_status.HttpStatus{
    Msg:  "error",
	// custmo http error code
	// it always return 500
	// code msg will show in msg
    Code: common.HOLE_NOT_FOUND,
    }, gin.H{
    "msg": "hello",
    }
}

func InitCustmoR(cr *custom.CustmoRouter) {
    logSettings:=config.Conf.LogSettings
    customize_router.InitCustomRouter(&centralog.LogOptions{
    Server:     logSettings.LogCenterHost,
    Db:         logSettings.Db,
    Collection: logSettings.Collection,
    },cr)
}

details

  1. init centralog first
  2. custom error code must larger than 1000

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages