feat: status api

This commit is contained in:
Ilia Denisov
2026-01-07 18:38:06 +02:00
parent 1b0ab7a079
commit 204d3df8cf
20 changed files with 188 additions and 40 deletions
+37
View File
@@ -0,0 +1,37 @@
package router
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
e "github.com/iliadenisov/galaxy/internal/error"
"github.com/iliadenisov/galaxy/internal/game"
"github.com/iliadenisov/galaxy/internal/model/rest"
)
func StatusHandler(c *gin.Context) {
g, err := game.LoadState(param())
if err == nil {
c.JSON(http.StatusOK, rest.Status{
Turn: g.Age,
Players: len(g.Race),
})
return
}
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})
}
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}