Files
galaxy-game/internal/router/router.go
T
2026-01-07 18:38:06 +02:00

60 lines
1.2 KiB
Go

package router
import (
"os"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/iliadenisov/galaxy/internal/controller"
"github.com/iliadenisov/galaxy/internal/model/rest"
)
var (
StoragePath string
)
func init() {
StoragePath = os.Getenv("STORAGE_PATH")
}
func param() func(*controller.Param) {
return func(p *controller.Param) {
// TODO: initialize base controller settings
p.StoragePath = StoragePath
}
}
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.GET("/api/v1/status", StatusHandler)
r.PUT("/api/v1/command", LimitMiddleware(1), func(ctx *gin.Context) { CommandHandler(ctx, executor) })
return r
}