refactor: executors and routers

* refactor: executors and routers
This commit is contained in:
Ilia Denisov
2026-02-09 15:53:34 +03:00
committed by GitHub
parent e48a0c8b96
commit d9c8de27e5
38 changed files with 508 additions and 838 deletions
+20 -26
View File
@@ -1,22 +1,25 @@
package router_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/stretchr/testify/assert"
)
func TestCommand(t *testing.T) {
r := router.SetupRouter()
r := setupRouter()
payload := rest.Command{
Race: "SomeRace",
Vote: &rest.CommandVote{
Recipient: "AnotherRace",
Actor: "SomeRace",
Commands: []json.RawMessage{
encodeCommand(&rest.CommandVote{
CommandMeta: rest.CommandMeta{Type: rest.CommandTypeVote},
Recipient: "AnotherRace",
}),
},
}
@@ -24,17 +27,17 @@ func TestCommand(t *testing.T) {
req, _ := http.NewRequest("PUT", "/api/v1/command", asBody(payload))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body)
assert.Equal(t, http.StatusNoContent, w.Code, w.Body)
// error: notblank validator
payload.Race = ""
payload.Actor = ""
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/command", asBody(payload))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)
payload.Race = " "
payload.Actor = " "
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/command", asBody(payload))
r.ServeHTTP(w, req)
@@ -43,24 +46,7 @@ func TestCommand(t *testing.T) {
// error: no commands
payload = rest.Command{
Race: "SomeRace",
}
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/command", asBody(payload))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)
// error: more than one command
payload = rest.Command{
Race: "SomeRace",
Vote: &rest.CommandVote{
Recipient: "AnotherRace",
},
DeclarePeace: &rest.CommandDeclarePeace{
Opponent: "OpponentRace",
},
Actor: "SomeRace",
}
w = httptest.NewRecorder()
@@ -69,3 +55,11 @@ func TestCommand(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)
}
func encodeCommand(cmd any) json.RawMessage {
v, err := json.Marshal(cmd)
if err != nil {
panic(err)
}
return v
}
+49 -27
View File
@@ -1,47 +1,69 @@
package handler
import (
"errors"
"encoding/json"
"fmt"
"net/http"
"github.com/iliadenisov/galaxy/internal/controller"
"github.com/iliadenisov/galaxy/internal/game"
"github.com/gin-gonic/gin"
"github.com/iliadenisov/galaxy/internal/model/rest"
)
type CommandExecutor func(controller.Configurer, rest.Command) error
var (
ErrCommandNotProcessed = errors.New("command was not processed by executor")
)
func CommandHandler(c *gin.Context, configurer controller.Configurer, executor CommandExecutor) {
func CommandHandler(c *gin.Context, executor CommandExecutor) {
var cmd rest.Command
if err := c.ShouldBindJSON(&cmd); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if errorResponded(c, c.ShouldBindJSON(&cmd)) {
return
}
err := executor(configurer, cmd)
switch {
case err == nil:
c.Status(http.StatusOK)
case errors.Is(err, ErrCommandNotProcessed):
c.Status(http.StatusInternalServerError) // TODO: add error text?
commands := make([]Command, 0)
for i := range cmd.Commands {
command, err := parseCommand(cmd.Actor, cmd.Commands[i])
if errorResponded(c, err) {
return
}
commands = append(commands, command)
}
if len(commands) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "no commands given"})
return
}
if errorResponded(c, executor.Execute(commands...)) {
return
}
c.Status(http.StatusNoContent)
}
func parseCommand(actor string, c json.RawMessage) (Command, error) {
meta := new(rest.CommandMeta)
if err := json.Unmarshal(c, meta); err != nil {
return nil, err
}
switch t := meta.Type; t {
case rest.CommandTypeVote:
return giveVotes(actor, c)
case rest.CommandTypeRelation:
return updateRelation(actor, c)
default:
// TODO: separate invalid input and game state errors
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return nil, fmt.Errorf("unknown comman type: %s", t)
}
}
func ExecuteCommand(config controller.Configurer, cmd rest.Command) error {
switch {
case cmd.DeclareWar != nil:
return game.DeclareWar(config, cmd.Race, cmd.DeclareWar.Opponent)
case cmd.DeclarePeace != nil:
return game.DeclarePeace(config, cmd.Race, cmd.DeclareWar.Opponent)
default:
return ErrCommandNotProcessed
func giveVotes(actor string, c json.RawMessage) (Command, error) {
var v rest.CommandVote
if err := json.Unmarshal(c, &v); err != nil {
return nil, err
}
return func(c controller.Ctrl) error { return c.GiveVotes(actor, v.Recipient) }, nil
}
func updateRelation(actor string, c json.RawMessage) (Command, error) {
var v rest.CommandUpdateRelation
if err := json.Unmarshal(c, &v); err != nil {
return nil, err
}
return func(c controller.Ctrl) error { return c.UpdateRelation(actor, v.Opponent, v.Relation) }, nil
}
+71 -2
View File
@@ -3,18 +3,88 @@ package handler
import (
"errors"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/internal/controller"
e "github.com/iliadenisov/galaxy/internal/error"
"github.com/iliadenisov/galaxy/internal/model/rest"
)
func transformError(c *gin.Context, err error) bool {
type CommandExecutor interface {
GenerateGame([]string) (uuid.UUID, error)
Execute(cmd ...Command) error
GameState() (rest.StateResponse, error)
}
type Command func(controller.Ctrl) error
type executor struct {
cfg controller.Configurer
}
func initConfig() controller.Configurer {
return func(p *controller.Param) {
p.StoragePath = os.Getenv("STORAGE_PATH")
}
}
func NewDefaultExecutor() CommandExecutor {
return NewDefaultConfigExecutor(initConfig())
}
func NewDefaultConfigExecutor(configurer controller.Configurer) CommandExecutor {
return &executor{cfg: configurer}
}
func (e *executor) Execute(cmd ...Command) error {
return controller.ExecuteCommand(e.cfg, func(c controller.Ctrl) error {
for i := range cmd {
if err := cmd[i](c); err != nil {
return err
}
}
return nil
})
}
func (e *executor) GenerateGame(races []string) (uuid.UUID, error) {
return controller.GenerateGame(e.cfg, races)
}
func (e *executor) GameState() (rest.StateResponse, error) {
s, err := controller.GameState(e.cfg)
if err != nil {
return rest.StateResponse{}, err
}
result := &rest.StateResponse{
ID: s.ID,
Turn: s.Turn,
Stage: s.Stage,
Players: make([]rest.PlayerState, len(s.Players)),
}
for i := range s.Players {
result.Players[i].ID = s.Players[i].ID
result.Players[i].Name = s.Players[i].Name
result.Players[i].Extinct = s.Players[i].Extinct
}
return *result, nil
}
func errorResponded(c *gin.Context, err error) bool {
if err == nil {
return false
}
var ge = new(e.GenericError)
if v, ok := err.(validator.ValidationErrors); ok {
c.JSON(http.StatusBadRequest, gin.H{"error": v.Error()})
return true
}
if errors.As(err, ge) {
switch ge.Code {
case e.ErrGameNotInitialized:
@@ -24,7 +94,6 @@ func transformError(c *gin.Context, err error) bool {
}
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
return true
+4 -8
View File
@@ -4,15 +4,12 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/iliadenisov/galaxy/internal/controller"
"github.com/iliadenisov/galaxy/internal/game"
"github.com/iliadenisov/galaxy/internal/model/rest"
)
func InitHandler(c *gin.Context, config controller.Configurer) {
func InitHandler(c *gin.Context, executor CommandExecutor) {
var init rest.Init
if err := c.ShouldBindJSON(&init); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if errorResponded(c, c.ShouldBindJSON(&init)) {
return
}
@@ -21,10 +18,9 @@ func InitHandler(c *gin.Context, config controller.Configurer) {
races[i] = init.Races[i].Name
}
uuid, err := game.GenerateGame(config, races)
if transformError(c, err) {
uuid, err := executor.GenerateGame(races)
if errorResponded(c, err) {
return
}
c.JSON(http.StatusCreated, rest.InitResponse{
+4 -10
View File
@@ -4,20 +4,14 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/iliadenisov/galaxy/internal/controller"
"github.com/iliadenisov/galaxy/internal/game"
"github.com/iliadenisov/galaxy/internal/model/rest"
)
func StatusHandler(c *gin.Context, config controller.Configurer) {
g, err := game.LoadState(config)
func StatusHandler(c *gin.Context, executor CommandExecutor) {
state, err := executor.GameState()
if transformError(c, err) {
if errorResponded(c, err) {
return
}
c.JSON(http.StatusOK, rest.Status{
Turn: g.Turn,
Players: len(g.Race),
})
c.JSON(http.StatusOK, state)
}
+3 -13
View File
@@ -2,7 +2,6 @@ package router_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
@@ -11,6 +10,7 @@ import (
"github.com/iliadenisov/galaxy/internal/controller"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/iliadenisov/galaxy/internal/router/handler"
"github.com/iliadenisov/galaxy/internal/util"
"github.com/stretchr/testify/assert"
)
@@ -19,7 +19,7 @@ func TestInit(t *testing.T) {
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
r := router.SetupRouterConfig(func(p *controller.Param) { p.StoragePath = root })
r := router.SetupRouter(handler.NewDefaultConfigExecutor(func(p *controller.Param) { p.StoragePath = root }))
payload := generateInitRequest(10)
@@ -34,7 +34,7 @@ func TestInit(t *testing.T) {
}
func TestInitValidators(t *testing.T) {
r := router.SetupRouter()
r := setupRouter()
payload := generateInitRequest(9)
w := httptest.NewRecorder()
@@ -43,13 +43,3 @@ func TestInitValidators(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)
}
func generateInitRequest(races int) rest.Init {
request := rest.Init{
Races: make([]rest.Race, races),
}
for i := range request.Races {
request.Races[i] = rest.Race{Name: fmt.Sprintf("Race_%02d", i)}
}
return request
}
+6 -13
View File
@@ -8,7 +8,6 @@ import (
"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"
)
@@ -16,12 +15,6 @@ const (
ISO8601 = "2006-01-02 15:04:05.0 -07:00"
)
func initConfig() func(*controller.Param) {
return func(p *controller.Param) {
p.StoragePath = os.Getenv("STORAGE_PATH")
}
}
type Router struct {
r *gin.Engine
executor handler.CommandExecutor
@@ -32,14 +25,14 @@ func (r Router) Run() error {
}
func NewRouter() Router {
return NewRouterExecutor(handler.ExecuteCommand)
return NewRouterExecutor(handler.NewDefaultExecutor())
}
func NewRouterExecutor(executor handler.CommandExecutor) Router {
return Router{r: setupRouter(initConfig(), executor)}
return Router{r: setupRouter(executor)}
}
func setupRouter(config controller.Configurer, executor handler.CommandExecutor) *gin.Engine {
func setupRouter(executor handler.CommandExecutor) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
@@ -55,9 +48,9 @@ func setupRouter(config controller.Configurer, executor handler.CommandExecutor)
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) })
groupV1.GET("/status", func(ctx *gin.Context) { handler.StatusHandler(ctx, executor) })
groupV1.POST("/init", func(ctx *gin.Context) { handler.InitHandler(ctx, executor) })
groupV1.PUT("/command", LimitMiddleware(1), func(ctx *gin.Context) { handler.CommandHandler(ctx, executor) })
return r
}
+3 -8
View File
@@ -2,14 +2,9 @@ package router
import (
"github.com/gin-gonic/gin"
"github.com/iliadenisov/galaxy/internal/controller"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router/handler"
)
func SetupRouter() *gin.Engine {
return SetupRouterConfig(nil)
}
func SetupRouterConfig(config controller.Configurer) *gin.Engine {
return setupRouter(config, func(controller.Configurer, rest.Command) error { return nil })
func SetupRouter(e handler.CommandExecutor) *gin.Engine {
return setupRouter(e)
}
+28
View File
@@ -0,0 +1,28 @@
package router_test
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/iliadenisov/galaxy/internal/router/handler"
)
type dummyExecutor struct {
}
func (e *dummyExecutor) Execute(cmd ...handler.Command) error {
return nil
}
func (e *dummyExecutor) GenerateGame(races []string) (uuid.UUID, error) {
return uuid.New(), nil
}
func (e *dummyExecutor) GameState() (rest.StateResponse, error) {
return rest.StateResponse{}, nil
}
func setupRouter() *gin.Engine {
return router.SetupRouter(&dummyExecutor{})
}
+16
View File
@@ -2,6 +2,7 @@ package router_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
@@ -10,6 +11,7 @@ import (
"testing"
"github.com/gin-gonic/gin"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/stretchr/testify/assert"
)
@@ -63,3 +65,17 @@ func limitTestingRouter() *gin.Engine {
})
return r
}
func generateInitRequest(races int) rest.Init {
request := rest.Init{
Races: make([]rest.Race, races),
}
for i := range request.Races {
request.Races[i] = rest.Race{Name: raceName(i)}
}
return request
}
func raceName(i int) string {
return fmt.Sprintf("Race_%02d", i)
}
+34 -3
View File
@@ -1,20 +1,51 @@
package router_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/internal/controller"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/iliadenisov/galaxy/internal/router/handler"
"github.com/iliadenisov/galaxy/internal/util"
"github.com/stretchr/testify/assert"
)
func TestGetStatus(t *testing.T) {
r := router.SetupRouter()
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
r := router.SetupRouter(handler.NewDefaultConfigExecutor(func(p *controller.Param) { p.StoragePath = root }))
payload := generateInitRequest(10)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/status", nil)
req, _ := http.NewRequest("POST", "/api/v1/init", asBody(payload))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotImplemented, w.Code, w.Body)
assert.Equal(t, http.StatusCreated, w.Code, w.Body)
var initResponse rest.InitResponse
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &initResponse))
assert.NoError(t, uuid.Validate(initResponse.UUID.String()))
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/api/v1/status", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body)
var stateResponse rest.StateResponse
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &stateResponse))
assert.NoError(t, uuid.Validate(stateResponse.ID.String()))
assert.Equal(t, uint(0), stateResponse.Turn)
assert.Equal(t, uint(0), stateResponse.Stage)
assert.Len(t, stateResponse.Players, 10)
for i := range stateResponse.Players {
assert.NoError(t, uuid.Validate(stateResponse.Players[i].ID.String()))
assert.Equal(t, raceName(i), stateResponse.Players[i].Name)
assert.False(t, stateResponse.Players[i].Extinct)
}
}