88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package publichttp
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"galaxy/lobby/internal/service/pausegame"
|
|
"galaxy/lobby/internal/service/resumegame"
|
|
)
|
|
|
|
const (
|
|
pauseGamePath = "/api/v1/lobby/games/{game_id}/pause"
|
|
resumeGamePath = "/api/v1/lobby/games/{game_id}/resume"
|
|
)
|
|
|
|
// registerPauseResumeRoutes binds the voluntary pause and
|
|
// resume 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 registerPauseResumeRoutes(mux *http.ServeMux, deps Dependencies, logger *slog.Logger) {
|
|
h := &pauseResumeHandlers{
|
|
deps: deps,
|
|
logger: logger.With("component", "public_http.pauseresume"),
|
|
}
|
|
mux.HandleFunc("POST "+pauseGamePath, h.handlePause)
|
|
mux.HandleFunc("POST "+resumeGamePath, h.handleResume)
|
|
}
|
|
|
|
type pauseResumeHandlers struct {
|
|
deps Dependencies
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func (h *pauseResumeHandlers) handlePause(writer http.ResponseWriter, request *http.Request) {
|
|
if h.deps.PauseGame == nil {
|
|
writeError(writer, http.StatusInternalServerError, "internal_error", "pause 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.PauseGame.Handle(request.Context(), pausegame.Input{
|
|
Actor: actor,
|
|
GameID: gameID,
|
|
})
|
|
if err != nil {
|
|
writeErrorFromService(writer, h.logger, err)
|
|
return
|
|
}
|
|
|
|
writeJSON(writer, http.StatusOK, encodeGameRecord(record))
|
|
}
|
|
|
|
func (h *pauseResumeHandlers) handleResume(writer http.ResponseWriter, request *http.Request) {
|
|
if h.deps.ResumeGame == nil {
|
|
writeError(writer, http.StatusInternalServerError, "internal_error", "resume 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.ResumeGame.Handle(request.Context(), resumegame.Input{
|
|
Actor: actor,
|
|
GameID: gameID,
|
|
})
|
|
if err != nil {
|
|
writeErrorFromService(writer, h.logger, err)
|
|
return
|
|
}
|
|
|
|
writeJSON(writer, http.StatusOK, encodeGameRecord(record))
|
|
}
|