50 lines
923 B
Go
50 lines
923 B
Go
package providers
|
|
|
|
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
|
|
)
|
|
|
|
type HandlerFunc func(metadata *access.Metadata)
|
|
|
|
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)
|
|
}
|
|
|
|
handleWithMetadata(c, controller)
|
|
})
|
|
}
|
|
|
|
func handleWithMetadata(c *gin.Context, controller HandlerFunc) {
|
|
var metadata = &access.Metadata{
|
|
Http: c,
|
|
}
|
|
|
|
controller(metadata)
|
|
}
|