feat: init api

This commit is contained in:
Ilia Denisov
2026-01-08 13:32:40 +02:00
parent 204d3df8cf
commit 972cfd82be
12 changed files with 240 additions and 108 deletions
+47
View File
@@ -0,0 +1,47 @@
package handler
import (
"errors"
"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.Config, rest.Command) error
var (
ErrCommandNotProcessed = errors.New("command was not processed by executor")
)
func CommandHandler(c *gin.Context, config controller.Config, executor CommandExecutor) {
var cmd rest.Command
if err := c.ShouldBindJSON(&cmd); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err := executor(config, cmd)
switch {
case err == nil:
c.Status(http.StatusOK)
case errors.Is(err, ErrCommandNotProcessed):
c.Status(http.StatusInternalServerError) // TODO: add error text?
default:
// TODO: separate invalid input and game state errors
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}
func ExecuteCommand(config controller.Config, 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
}
}
+31
View File
@@ -0,0 +1,31 @@
package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
e "github.com/iliadenisov/galaxy/internal/error"
)
func transformError(c *gin.Context, err error) bool {
if err == nil {
return false
}
var ge = new(e.GenericError)
if errors.As(err, ge) {
switch ge.Code {
case e.ErrGameNotInitialized:
c.Status(http.StatusNotImplemented)
default:
c.JSON(http.StatusInternalServerError, gin.H{"generic_error": ge.Error(), "code": ge.Code})
}
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
return true
}
+33
View File
@@ -0,0 +1,33 @@
package handler
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.Config) {
var init rest.Init
if err := c.ShouldBindJSON(&init); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
races := make([]string, len(init.Races))
for i := range init.Races {
races[i] = init.Races[i].Name
}
uuid, err := game.GenerateGame(config, races)
if transformError(c, err) {
return
}
c.JSON(http.StatusCreated, rest.InitResponse{
UUID: uuid,
})
}
+23
View File
@@ -0,0 +1,23 @@
package handler
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.Config) {
g, err := game.LoadState(config)
if transformError(c, err) {
return
}
c.JSON(http.StatusOK, rest.Status{
Turn: g.Age,
Players: len(g.Race),
})
}