Files
galaxy-game/gamemaster/internal/api/internalhttp/handlers/stopruntime.go
T
2026-05-03 07:59:03 +02:00

60 lines
1.6 KiB
Go

package handlers
import (
"net/http"
"galaxy/gamemaster/internal/domain/operation"
"galaxy/gamemaster/internal/service/adminstop"
)
// stopRuntimeRequestBody mirrors the OpenAPI StopRuntimeRequest
// schema.
type stopRuntimeRequestBody struct {
Reason string `json:"reason"`
}
// newStopRuntimeHandler returns the handler for
// `POST /api/v1/internal/runtimes/{game_id}/stop`.
func newStopRuntimeHandler(deps Dependencies) http.HandlerFunc {
logger := loggerFor(deps.Logger, "internal_rest.stop_runtime")
return func(writer http.ResponseWriter, request *http.Request) {
if deps.StopRuntime == nil {
writeError(writer, http.StatusInternalServerError, errorCodeInternal, "stop runtime service is not wired")
return
}
gameID, ok := extractGameID(writer, request)
if !ok {
return
}
var body stopRuntimeRequestBody
if err := decodeStrictJSON(request.Body, &body); err != nil {
writeError(writer, http.StatusBadRequest, errorCodeInvalidRequest, err.Error())
return
}
result, err := deps.StopRuntime.Handle(request.Context(), adminstop.Input{
GameID: gameID,
Reason: body.Reason,
OpSource: resolveOpSource(request),
SourceRef: requestSourceRef(request),
})
if err != nil {
logger.ErrorContext(request.Context(), "stop runtime service errored",
"game_id", gameID,
"err", err.Error(),
)
writeError(writer, http.StatusInternalServerError, errorCodeInternal, "stop runtime service failed")
return
}
if result.Outcome == operation.OutcomeFailure {
writeFailure(writer, result.ErrorCode, result.ErrorMessage)
return
}
writeJSON(writer, http.StatusOK, encodeRuntimeRecord(result.Record))
}
}