|
|
|
@ -9,9 +9,15 @@ package router
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"git.zhangdeman.cn/zhangdeman/exception"
|
|
|
|
|
"git.zhangdeman.cn/zhangdeman/gin/response"
|
|
|
|
|
|
|
|
|
|
"git.zhangdeman.cn/zhangdeman/gin/request"
|
|
|
|
|
|
|
|
|
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
@ -37,7 +43,9 @@ func Register(port int, controllerList ...any) {
|
|
|
|
|
// 忽略空指针
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
parseController(controller)
|
|
|
|
|
}
|
|
|
|
|
ginRouter.Run(fmt.Sprintf(":%d", port))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parseController 解析controller
|
|
|
|
@ -74,11 +82,9 @@ func parseController(controller any) {
|
|
|
|
|
middlewareList = res[0].Interface().([]gin.HandlerFunc)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(111, middlewareList)
|
|
|
|
|
}
|
|
|
|
|
for funcIdx := 0; funcIdx < controllerType.NumMethod(); funcIdx++ {
|
|
|
|
|
method := controllerType.Method(funcIdx)
|
|
|
|
|
// methodValue := controllerValue.Method(funcIdx)
|
|
|
|
|
if method.Name == PrefixFuncName || method.Name == MiddlewareFuncName {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
@ -90,6 +96,7 @@ func parseController(controller any) {
|
|
|
|
|
if nil == uriConfig {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
registerUri(uriConfig, controllerValue.Method(funcIdx), middlewareList)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -119,17 +126,74 @@ func parseUriConfig(methodType reflect.Type, routerPrefix string) (*UriConfig, e
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
uriConfig := &UriConfig{
|
|
|
|
|
Path: strings.TrimRight(routerPrefix, "/") + "/" + strings.TrimLeft(metaField.Tag.Get(TagNamePath), "/"),
|
|
|
|
|
Method: strings.ToUpper(metaField.Tag.Get(TagNameMethod)),
|
|
|
|
|
TagList: strings.Split(metaField.Tag.Get(TagNameUriTag), "|"),
|
|
|
|
|
Desc: metaField.Tag.Get(TagNameDesc),
|
|
|
|
|
Strict: wrapper.ArrayType([]string{"", "true"}).Has(strings.ToLower(metaField.Tag.Get(TagNameStrict))) >= 0,
|
|
|
|
|
Path: strings.TrimRight(routerPrefix, "/") + "/" + strings.TrimLeft(metaField.Tag.Get(TagNamePath), "/"),
|
|
|
|
|
Method: strings.ToUpper(metaField.Tag.Get(TagNameMethod)),
|
|
|
|
|
TagList: strings.Split(metaField.Tag.Get(TagNameUriTag), "|"),
|
|
|
|
|
Desc: metaField.Tag.Get(TagNameDesc),
|
|
|
|
|
Strict: wrapper.ArrayType([]string{"", "true"}).Has(strings.ToLower(metaField.Tag.Get(TagNameStrict))) >= 0,
|
|
|
|
|
FormDataType: methodType.In(2).Elem(),
|
|
|
|
|
}
|
|
|
|
|
// 解析第一个返回值
|
|
|
|
|
// 解析第二个返回值
|
|
|
|
|
return uriConfig, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// registerUri 注册路由
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 18:00 2024/7/21
|
|
|
|
|
func registerUri(uriConfig *UriConfig, methodValue reflect.Value, middlewareList []gin.HandlerFunc) {
|
|
|
|
|
if nil == middlewareList {
|
|
|
|
|
middlewareList = make([]gin.HandlerFunc, 0)
|
|
|
|
|
}
|
|
|
|
|
handlerFunc := func(ctx *gin.Context) {
|
|
|
|
|
formDataReceiver := reflect.New(uriConfig.FormDataType)
|
|
|
|
|
if err := request.Form.Parse(ctx, formDataReceiver.Interface()); nil != err {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
returnValue := methodValue.Call([]reflect.Value{reflect.ValueOf(ctx), formDataReceiver})
|
|
|
|
|
businessData := returnValue[0].Interface()
|
|
|
|
|
errData := returnValue[1]
|
|
|
|
|
if errData.IsNil() {
|
|
|
|
|
response.Success(ctx, businessData)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err := errData.Interface()
|
|
|
|
|
if e, ok := err.(exception.IException); ok {
|
|
|
|
|
response.SendWithException(ctx, e, businessData)
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
response.SendWithException(ctx, exception.NewFromError(-1, errData.Interface().(error)), businessData)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
middlewareList = append(middlewareList, handlerFunc)
|
|
|
|
|
switch uriConfig.Method {
|
|
|
|
|
case http.MethodGet:
|
|
|
|
|
ginRouter.GET(uriConfig.Path, middlewareList...)
|
|
|
|
|
case http.MethodHead:
|
|
|
|
|
ginRouter.HEAD(uriConfig.Path, middlewareList...)
|
|
|
|
|
case http.MethodPost:
|
|
|
|
|
ginRouter.PUT(uriConfig.Path, middlewareList...)
|
|
|
|
|
case http.MethodPut:
|
|
|
|
|
ginRouter.PUT(uriConfig.Path, middlewareList...)
|
|
|
|
|
case http.MethodPatch:
|
|
|
|
|
ginRouter.PATCH(uriConfig.Path, middlewareList...)
|
|
|
|
|
case http.MethodDelete:
|
|
|
|
|
ginRouter.DELETE(uriConfig.Path, middlewareList...)
|
|
|
|
|
case http.MethodConnect:
|
|
|
|
|
ginRouter.Handle(http.MethodConnect, uriConfig.Path, middlewareList...)
|
|
|
|
|
case http.MethodOptions:
|
|
|
|
|
ginRouter.OPTIONS(uriConfig.Path, middlewareList...)
|
|
|
|
|
case http.MethodTrace:
|
|
|
|
|
ginRouter.Handle(http.MethodTrace, uriConfig.Path, middlewareList...)
|
|
|
|
|
case "ANY":
|
|
|
|
|
ginRouter.Any(uriConfig.Path, middlewareList...)
|
|
|
|
|
default:
|
|
|
|
|
panic(uriConfig.Path + " : " + uriConfig.Method + " is not support")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// debugLog ...
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|