ginHelper

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2019 License: MIT Imports: 3 Imported by: 8

README

ginHelper

ginHelper是一个自动把handlerFunc添加到Gin并且设置路由的工具包。

  1. 在handler文件夹中添加main.go文件
type helper struct {
}

func Build(r gin.IRoutes) {
    h := new(helper)
    valueOfh := reflect.ValueOf(h)
    numMethod := valueOfh.NumMethod()
    for i := 0; i < numMethod; i++ {
        rt := valueOfh.Method(i).Call(nil)[0].Interface().(*handlerHelper.Router)
        rt.AddHandler(r)
    }
}
  1. 之后每次写handlerFunc的时候都类似下方的helloHandler前面加上一个 *helper的一个方法HelloHandler()中设置路由。
func (h *helper) HelloHandler() (r *handlerHelper.Router) {
    return &handlerHelper.Router{
        Path:   "/HelloHandler",
        Method: "GET",
        Handlers: []gin.HandlerFunc{
            helloHandler,
        }}
}
func helloHandler(c *gin.Context) {

    c.String(http.StatusOK, "Hello world!")
}
  1. 在gin中调用
r := gin.New()
//handler中自动建立路由
handler.Build(r)
//user的Group中自动建立路由
user.Build(r.Group("/user"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(h interface{}, r gin.IRoutes)

Types

type Router

type Router struct {
	Path     string
	Method   string
	Handlers []gin.HandlerFunc
}

func (*Router) AddHandler

func (rt *Router) AddHandler(r gin.IRoutes)

Jump to

Keyboard shortcuts

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