41 lines
870 B
Go
41 lines
870 B
Go
package router
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/iliadenisov/galaxy/internal/model/rest"
|
|
)
|
|
|
|
type Executor func(rest.Command) error
|
|
|
|
type Router struct {
|
|
r *gin.Engine
|
|
executor Executor
|
|
}
|
|
|
|
func (r Router) Run() error {
|
|
return r.r.Run()
|
|
}
|
|
|
|
func NewRouter() Router {
|
|
return NewRouterExecutor(Execute)
|
|
}
|
|
|
|
func NewRouterExecutor(executor Executor) Router {
|
|
return Router{r: setupRouter(executor)}
|
|
}
|
|
|
|
func setupRouter(executor Executor) *gin.Engine {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
r := gin.New()
|
|
r.Use(gin.Recovery())
|
|
|
|
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
|
v.RegisterValidation("notblank", notBlankStringValidator)
|
|
}
|
|
|
|
r.PUT("/api/v1/command", LimitMiddleware(1), func(ctx *gin.Context) { CommandHandler(ctx, executor) })
|
|
return r
|
|
}
|