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
+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)
}