package handlers import ( "net/http" "galaxy/rtmanager/internal/domain/operation" "galaxy/rtmanager/internal/service/patchruntime" "galaxy/rtmanager/internal/service/startruntime" ) // patchRequestBody mirrors the OpenAPI PatchRequest schema. The // service layer validates `image_ref` shape (semver, distribution // reference) and surfaces `image_ref_not_semver` / // `semver_patch_only` as needed. type patchRequestBody struct { ImageRef string `json:"image_ref"` } // newPatchHandler returns the handler for // `POST /api/v1/internal/runtimes/{game_id}/patch`. func newPatchHandler(deps Dependencies) http.HandlerFunc { logger := loggerFor(deps.Logger, "internal_rest.patch") return func(writer http.ResponseWriter, request *http.Request) { if deps.PatchRuntime == nil { writeError(writer, http.StatusInternalServerError, startruntime.ErrorCodeInternal, "patch runtime service is not wired", ) return } gameID, ok := extractGameID(writer, request) if !ok { return } var body patchRequestBody if err := decodeStrictJSON(request.Body, &body); err != nil { writeError(writer, http.StatusBadRequest, startruntime.ErrorCodeInvalidRequest, err.Error(), ) return } result, err := deps.PatchRuntime.Handle(request.Context(), patchruntime.Input{ GameID: gameID, NewImageRef: body.ImageRef, OpSource: resolveOpSource(request), SourceRef: requestSourceRef(request), }) if err != nil { logger.ErrorContext(request.Context(), "patch runtime service errored", "game_id", gameID, "err", err.Error(), ) writeError(writer, http.StatusInternalServerError, startruntime.ErrorCodeInternal, "patch runtime service failed", ) return } if result.Outcome == operation.OutcomeFailure { writeFailure(writer, result.ErrorCode, result.ErrorMessage) return } writeJSON(writer, http.StatusOK, encodeRuntimeRecord(result.Record)) } }