feat: game lobby service
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package publichttp
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"galaxy/lobby/internal/service/retrystartgame"
|
||||
"galaxy/lobby/internal/service/startgame"
|
||||
)
|
||||
|
||||
const (
|
||||
startGamePath = "/api/v1/lobby/games/{game_id}/start"
|
||||
retryStartGamePath = "/api/v1/lobby/games/{game_id}/retry-start"
|
||||
)
|
||||
|
||||
// registerStartRoutes binds the start and retry-start routes on
|
||||
// the public port. Both routes require the X-User-ID header so the actor
|
||||
// is always a user; admins use the internal port.
|
||||
func registerStartRoutes(mux *http.ServeMux, deps Dependencies, logger *slog.Logger) {
|
||||
h := &startHandlers{
|
||||
deps: deps,
|
||||
logger: logger.With("component", "public_http.startgame"),
|
||||
}
|
||||
mux.HandleFunc("POST "+startGamePath, h.handleStart)
|
||||
mux.HandleFunc("POST "+retryStartGamePath, h.handleRetryStart)
|
||||
}
|
||||
|
||||
type startHandlers struct {
|
||||
deps Dependencies
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (h *startHandlers) handleStart(writer http.ResponseWriter, request *http.Request) {
|
||||
if h.deps.StartGame == nil {
|
||||
writeError(writer, http.StatusInternalServerError, "internal_error", "start game service is not wired")
|
||||
return
|
||||
}
|
||||
|
||||
games := &gameHandlers{deps: h.deps, logger: h.logger}
|
||||
actor, ok := games.requireUserActor(writer, request)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
gameID, ok := games.extractGameID(writer, request)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
record, err := h.deps.StartGame.Handle(request.Context(), startgame.Input{
|
||||
Actor: actor,
|
||||
GameID: gameID,
|
||||
})
|
||||
if err != nil {
|
||||
writeErrorFromService(writer, h.logger, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(writer, http.StatusOK, encodeGameRecord(record))
|
||||
}
|
||||
|
||||
func (h *startHandlers) handleRetryStart(writer http.ResponseWriter, request *http.Request) {
|
||||
if h.deps.RetryStartGame == nil {
|
||||
writeError(writer, http.StatusInternalServerError, "internal_error", "retry start game service is not wired")
|
||||
return
|
||||
}
|
||||
|
||||
games := &gameHandlers{deps: h.deps, logger: h.logger}
|
||||
actor, ok := games.requireUserActor(writer, request)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
gameID, ok := games.extractGameID(writer, request)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
record, err := h.deps.RetryStartGame.Handle(request.Context(), retrystartgame.Input{
|
||||
Actor: actor,
|
||||
GameID: gameID,
|
||||
})
|
||||
if err != nil {
|
||||
writeErrorFromService(writer, h.logger, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(writer, http.StatusOK, encodeGameRecord(record))
|
||||
}
|
||||
Reference in New Issue
Block a user