rag/internal/providers/gin.go

50 lines
923 B
Go
Raw Normal View History

2024-06-13 06:32:33 +00:00
package providers
2024-06-13 07:08:55 +00:00
import (
"framework_v2/internal/access"
"github.com/gin-gonic/gin"
)
func InitGin() {
access.Router = gin.Default()
access.Router.Use(gin.Recovery())
}
type httpMethod int
var httpMethodStr = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
func (h httpMethod) String() string {
return httpMethodStr[h-1]
}
const (
GET httpMethod = iota + 1
POST
PUT
DELETE
PATCH
HEAD
OPTIONS
)
2024-06-13 07:17:53 +00:00
type HandlerFunc func(metadata *access.Metadata)
2024-06-13 07:08:55 +00:00
2024-06-13 07:17:53 +00:00
func HandleRoute(method httpMethod, relativePath string, controller HandlerFunc, middlewares ...gin.HandlerFunc) {
2024-06-13 07:08:55 +00:00
access.Router.Handle(method.String(), relativePath, func(c *gin.Context) {
2024-06-13 07:17:53 +00:00
for _, middleware := range middlewares {
middleware(c)
}
2024-06-13 07:08:55 +00:00
2024-06-13 07:17:53 +00:00
handleWithMetadata(c, controller)
})
2024-06-13 07:08:55 +00:00
}
2024-06-13 07:17:53 +00:00
func handleWithMetadata(c *gin.Context, controller HandlerFunc) {
var metadata = &access.Metadata{
Http: c,
2024-06-13 07:08:55 +00:00
}
2024-06-13 07:17:53 +00:00
controller(metadata)
2024-06-13 07:08:55 +00:00
}