framework_v2/internal/app/gin/route.go

112 lines
2.5 KiB
Go
Raw Normal View History

2024-06-15 16:55:25 +00:00
package gin
2024-06-13 07:08:55 +00:00
import (
2024-06-15 16:45:32 +00:00
"fmt"
2024-06-15 16:55:25 +00:00
"framework_v2/internal/app/facades"
"framework_v2/internal/app/helpers"
"framework_v2/internal/app/user"
http2 "framework_v2/internal/middleware/http"
2024-06-13 07:08:55 +00:00
"github.com/gin-gonic/gin"
2024-06-15 16:45:32 +00:00
"net/http"
"reflect"
2024-06-13 07:08:55 +00:00
)
2024-06-15 16:45:32 +00:00
//func HandleRoute(method httpMethod, relativePath string, controller HandlerFunc, middlewares ...gin.HandlerFunc) {
// access.Router.Handle(method.String(), relativePath, func(c *gin.Context) {
// for _, middleware := range middlewares {
// middleware(c)
// }
//
// if !c.IsAborted() {
// handleWithMetadata(c, controller)
// }
// })
//}
2024-06-13 08:36:10 +00:00
2024-06-15 16:45:32 +00:00
//func handleWithMetadata(c *gin.Context, controller HandlerFunc) {
// var metadata = &consts.Request{
// Http: c,
// User: GetAuthFromCtx(c),
// }
//
// controller(metadata)
//}
2024-06-13 08:36:10 +00:00
2024-06-15 16:45:32 +00:00
func GET(relativePath string, handlers ...interface{}) {
2024-06-15 16:55:25 +00:00
facades.Router.GET(relativePath, func(c *gin.Context) {
2024-06-15 16:45:32 +00:00
doHandler(c, handlers...)
})
2024-06-13 08:36:10 +00:00
}
2024-06-15 16:45:32 +00:00
func POST(relativePath string, handlers ...interface{}) {
2024-06-15 16:55:25 +00:00
facades.Router.POST(relativePath, func(c *gin.Context) {
2024-06-15 16:45:32 +00:00
doHandler(c, handlers...)
})
}
2024-06-13 08:36:10 +00:00
2024-06-15 16:45:32 +00:00
func PUT(relativePath string, handlers ...interface{}) {
2024-06-15 16:55:25 +00:00
facades.Router.PUT(relativePath, func(c *gin.Context) {
2024-06-15 16:45:32 +00:00
doHandler(c, handlers...)
})
}
2024-06-13 08:36:10 +00:00
2024-06-15 16:45:32 +00:00
func PATCH(relativePath string, handlers ...interface{}) {
2024-06-15 16:55:25 +00:00
facades.Router.PATCH(relativePath, func(c *gin.Context) {
2024-06-15 16:45:32 +00:00
doHandler(c, handlers...)
})
}
2024-06-13 08:36:10 +00:00
2024-06-15 16:45:32 +00:00
func DELETE(relativePath string, handlers ...interface{}) {
2024-06-15 16:55:25 +00:00
facades.Router.DELETE(relativePath, func(c *gin.Context) {
2024-06-15 16:45:32 +00:00
doHandler(c, handlers...)
2024-06-13 08:36:10 +00:00
})
}
2024-06-15 16:45:32 +00:00
func doHandler(c *gin.Context, handlers ...interface{}) {
for _, handler := range handlers {
if c.IsAborted() {
// 是否已经响应
if c.Writer.Written() {
return
} else {
2024-06-15 16:55:25 +00:00
helpers.ResponseError(c, http.StatusBadRequest, http2.ErrEmptyResponse)
2024-06-15 16:45:32 +00:00
}
return
}
wrapHandler(c, handler)
}
}
func wrapHandler(c *gin.Context, f interface{}) {
fnValue := reflect.ValueOf(f)
fnType := fnValue.Type()
var args []reflect.Value
for i := 0; i < fnType.NumIn(); i++ {
argType := fnType.In(i)
var argValue reflect.Value
switch argType {
case reflect.TypeOf((*gin.Context)(nil)):
argValue = reflect.ValueOf(c)
2024-06-15 16:55:25 +00:00
case reflect.TypeOf((*user.User)(nil)):
userInfo := http2.DIJWTAuth(c)
2024-06-15 16:45:32 +00:00
if userInfo == nil {
2024-06-15 16:55:25 +00:00
helpers.ResponseError(c, http.StatusUnauthorized, http2.ErrNotValidToken)
2024-06-15 16:45:32 +00:00
return
}
argValue = reflect.ValueOf(userInfo)
default:
2024-06-15 16:55:25 +00:00
helpers.ResponseError(c, http.StatusBadRequest, fmt.Errorf("invalid argument type: %s", argType.String()))
2024-06-15 16:45:32 +00:00
return
}
args = append(args, argValue)
2024-06-13 08:36:10 +00:00
}
2024-06-15 16:45:32 +00:00
fnValue.Call(args)
2024-06-13 07:08:55 +00:00
}