feat: http router, command api

This commit is contained in:
Ilia Denisov
2026-01-07 13:52:20 +02:00
parent c9ed52b268
commit 1b0ab7a079
9 changed files with 404 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
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
}