71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"galaxy/rtmanager/internal/domain/operation"
|
|
"galaxy/rtmanager/internal/service/startruntime"
|
|
"galaxy/rtmanager/internal/service/stopruntime"
|
|
)
|
|
|
|
// stopRequestBody mirrors the OpenAPI StopRequest schema. The reason
|
|
// enum is validated at the service layer (`stopruntime.Input.Validate`);
|
|
// unknown values surface as `invalid_request`.
|
|
type stopRequestBody struct {
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
// newStopHandler returns the handler for
|
|
// `POST /api/v1/internal/runtimes/{game_id}/stop`.
|
|
func newStopHandler(deps Dependencies) http.HandlerFunc {
|
|
logger := loggerFor(deps.Logger, "internal_rest.stop")
|
|
return func(writer http.ResponseWriter, request *http.Request) {
|
|
if deps.StopRuntime == nil {
|
|
writeError(writer, http.StatusInternalServerError,
|
|
startruntime.ErrorCodeInternal,
|
|
"stop runtime service is not wired",
|
|
)
|
|
return
|
|
}
|
|
|
|
gameID, ok := extractGameID(writer, request)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var body stopRequestBody
|
|
if err := decodeStrictJSON(request.Body, &body); err != nil {
|
|
writeError(writer, http.StatusBadRequest,
|
|
startruntime.ErrorCodeInvalidRequest,
|
|
err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
result, err := deps.StopRuntime.Handle(request.Context(), stopruntime.Input{
|
|
GameID: gameID,
|
|
Reason: stopruntime.StopReason(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,
|
|
startruntime.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))
|
|
}
|
|
}
|