1 Star 0 Fork 0

万聂青 / ginrest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
dispatch.go 1.19 KB
一键复制 编辑 原始数据 按行查看 历史
package dispatch
import (
"gitee.com/Ivy1996-encode/ginrest/exceptions"
"gitee.com/Ivy1996-encode/ginrest/ghttp"
"gitee.com/Ivy1996-encode/ginrest/ghttp/handlers"
"github.com/gin-gonic/gin"
"reflect"
"strings"
)
type RequestDispatcher interface {
DispatchRequest(context *gin.Context)
}
type MapRequestDispatcher map[string]gin.HandlerFunc
func (r MapRequestDispatcher) DispatchRequest(context *gin.Context) {
defer handlers.HandlerErrorMiddleware()(context)
requestMethod := strings.Title(strings.ToLower(context.Request.Method))
if handler := r[requestMethod]; handler != nil {
handler(context)
} else {
panic(exceptions.MethodNotAllowedError)
}
}
func MakeStructAsHandlerFunc(view interface{}) gin.HandlerFunc {
value := reflect.ValueOf(view)
requestDispatcher := make(MapRequestDispatcher)
for _, requestMethod := range ghttp.RequestMethods {
if handler := value.MethodByName(requestMethod); handler.IsValid() {
if methodHandler, ok := handler.Interface().(func(ctx *gin.Context)); ok {
requestDispatcher[requestMethod] = handlers.RegisterHandlerErrorMiddleware(methodHandler)
}
}
}
return func(context *gin.Context) {
requestDispatcher.DispatchRequest(context)
}
}
Go
1
https://gitee.com/Ivy1996-encode/ginrest.git
git@gitee.com:Ivy1996-encode/ginrest.git
Ivy1996-encode
ginrest
ginrest
5823538d6d83

搜索帮助

53164aa7 5694891 3bd8fe86 5694891