Files
galaxy-game/internal/router/router.go
T
Ilia Denisov 972cfd82be feat: init api
2026-01-08 13:32:40 +02:00

54 lines
1.4 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/router/handler"
)
func initConfig() func(*controller.Param) {
return func(p *controller.Param) {
// TODO: initialize base controller settings
p.StoragePath = os.Getenv("STORAGE_PATH")
}
}
type Router struct {
r *gin.Engine
executor handler.CommandExecutor
}
func (r Router) Run() error {
return r.r.Run()
}
func NewRouter() Router {
return NewRouterExecutor(handler.ExecuteCommand)
}
func NewRouterExecutor(executor handler.CommandExecutor) Router {
return Router{r: setupRouter(initConfig(), executor)}
}
func setupRouter(config controller.Config, executor handler.CommandExecutor) *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)
}
groupV1 := r.Group("/api/v1")
groupV1.GET("/status", func(ctx *gin.Context) { handler.StatusHandler(ctx, config) })
groupV1.POST("/init", func(ctx *gin.Context) { handler.InitHandler(ctx, config) })
groupV1.PUT("/command", LimitMiddleware(1), func(ctx *gin.Context) { handler.CommandHandler(ctx, config, executor) })
return r
}