2024-06-13 06:32:33 +00:00
|
|
|
package providers
|
2024-06-13 07:08:55 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-15 16:45:32 +00:00
|
|
|
"fmt"
|
2024-06-13 07:08:55 +00:00
|
|
|
"framework_v2/internal/access"
|
2024-06-13 08:36:10 +00:00
|
|
|
"framework_v2/internal/consts"
|
2024-06-15 16:45:32 +00:00
|
|
|
"framework_v2/internal/helper"
|
2024-06-13 07:36:51 +00:00
|
|
|
ginzap "github.com/gin-contrib/zap"
|
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:36:51 +00:00
|
|
|
"time"
|
2024-06-13 07:08:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func InitGin() {
|
2024-06-13 07:36:51 +00:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
2024-06-13 07:08:55 +00:00
|
|
|
|
2024-06-13 07:36:51 +00:00
|
|
|
access.Router = gin.New()
|
2024-06-13 07:17:53 +00:00
|
|
|
|
2024-06-13 08:36:10 +00:00
|
|
|
access.Router.Use(ginzap.Ginzap(access.Logger, time.RFC3339, true))
|
|
|
|
//access.Router.Use(gin.Recovery())
|
|
|
|
//access.Router.Use(ginzap.RecoveryWithZap(access.Logger, true))
|
|
|
|
}
|
|
|
|
|
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{}) {
|
|
|
|
access.Router.GET(relativePath, func(c *gin.Context) {
|
|
|
|
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{}) {
|
|
|
|
access.Router.POST(relativePath, func(c *gin.Context) {
|
|
|
|
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{}) {
|
|
|
|
access.Router.PUT(relativePath, func(c *gin.Context) {
|
|
|
|
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{}) {
|
|
|
|
access.Router.PATCH(relativePath, func(c *gin.Context) {
|
|
|
|
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{}) {
|
|
|
|
access.Router.DELETE(relativePath, func(c *gin.Context) {
|
|
|
|
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 {
|
|
|
|
helper.ResponseError(c, http.StatusBadRequest, ErrEmptyResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
case reflect.TypeOf((*consts.User)(nil)):
|
|
|
|
userInfo := DIJWTAuth(c)
|
|
|
|
if userInfo == nil {
|
|
|
|
helper.ResponseError(c, http.StatusUnauthorized, ErrNotValidToken)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
argValue = reflect.ValueOf(userInfo)
|
|
|
|
default:
|
|
|
|
helper.ResponseError(c, http.StatusBadRequest, fmt.Errorf("invalid argument type: %s", argType.String()))
|
|
|
|
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
|
|
|
}
|