改进 路由和 http ctx

This commit is contained in:
Twilight 2024-06-13 15:17:53 +08:00
parent 12c25e0b22
commit bc4e145689
2 changed files with 14 additions and 12 deletions

View File

@ -2,10 +2,12 @@ package access
import ( import (
"framework_v2/internal/providers" "framework_v2/internal/providers"
"github.com/gin-gonic/gin"
) )
type User providers.JwtIDToken type User providers.JwtIDToken
type Metadata struct { type Metadata struct {
Http *gin.Context
User User User User
} }

View File

@ -28,22 +28,22 @@ const (
OPTIONS OPTIONS
) )
type HandlerFunc func(c *gin.Context, metadata *access.Metadata) type HandlerFunc func(metadata *access.Metadata)
func HandleRoute(method httpMethod, relativePath string, handlers ...HandlerFunc) { func HandleRoute(method httpMethod, relativePath string, controller HandlerFunc, middlewares ...gin.HandlerFunc) {
access.Router.Handle(method.String(), relativePath, func(c *gin.Context) { access.Router.Handle(method.String(), relativePath, func(c *gin.Context) {
handleWithMetadata(c, handlers...) for _, middleware := range middlewares {
middleware(c)
}
handleWithMetadata(c, controller)
}) })
} }
func GetUser(c *gin.Context) *access.User { func handleWithMetadata(c *gin.Context, controller HandlerFunc) {
return &access.User{} var metadata = &access.Metadata{
} Http: c,
func handleWithMetadata(c *gin.Context, handlers ...HandlerFunc) {
var metadata = &access.Metadata{}
for _, handler := range handlers {
handler(c, metadata)
} }
controller(metadata)
} }