46 lines
866 B
Go
46 lines
866 B
Go
|
package routes
|
||
|
|
||
|
import (
|
||
|
"framework_v2/internal/access"
|
||
|
"framework_v2/internal/consts"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
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 *consts.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 = &consts.Metadata{
|
||
|
Http: c,
|
||
|
}
|
||
|
|
||
|
controller(metadata)
|
||
|
}
|