Files
browser/platform/api/game_client_bridge_companion_handlers.go
T

144 lines
4.4 KiB
Go

package api
import (
"net/http"
"browser.local/platform/dto"
)
// gameClientBridgeCompanionLogEvents godoc
// @Summary Stream live Run logs to a game companion
// @Description Authorizes a component session and relays only the current supervised process log stream to the companion. The platform does not persist log bodies on this route.
// @Tags game-client-bridge
// @Accept json
// @Produce text/event-stream
// @Param body body dto.GameClientBridgeLogStreamRequest true "Component log stream request"
// @Success 200 {object} dto.LogStreamEventResponse
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Failure 405 {object} dto.ErrorResponse
// @Router /api/v1/game-client-bridge/companion/logs/events [post]
func (h *coreHandlers) gameClientBridgeCompanionLogEvents(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeMethodNotAllowed(w, http.MethodPost)
return
}
request, err := decodeJSON[dto.GameClientBridgeLogStreamRequest](r)
if err != nil {
writeDecodeError(w, err)
return
}
instance, err := h.core.AuthorizeGameClientBridgeLogStream(request.ToDomain())
if err != nil {
writeServiceError(w, err)
return
}
subscription, err := h.core.SubscribeLogEvents(instance.ID)
if err != nil {
writeServiceError(w, err)
return
}
defer subscription.Close()
streams, liveEligible, err := h.loadLiveLogSnapshot(instance.ID)
if err != nil {
writeServiceError(w, err)
return
}
h.streamCurrentLogEvents(w, r, instance, streams, liveEligible, subscription)
}
func (h *coreHandlers) gameClientBridgeCompanionClaim(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeMethodNotAllowed(w, http.MethodPost)
return
}
request, err := decodeJSON[dto.GameClientBridgeClaimRequest](r)
if err != nil {
writeDecodeError(w, err)
return
}
commands, err := h.core.ClaimGameClientBridgeCommands(request.ToDomain())
if err != nil {
writeServiceError(w, err)
return
}
writeJSON(w, http.StatusOK, dto.GameClientBridgeClaimResponseFromDomain(commands))
}
func (h *coreHandlers) gameClientBridgeCompanionAck(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeMethodNotAllowed(w, http.MethodPost)
return
}
request, err := decodeJSON[dto.GameClientBridgeAckRequest](r)
if err != nil {
writeDecodeError(w, err)
return
}
command, err := h.core.AckGameClientBridgeCommand(request.ToDomain(r.PathValue("commandId")))
if err != nil {
writeServiceError(w, err)
return
}
writeJSON(w, http.StatusOK, dto.GameClientBridgeAckFromDomain(command))
}
func (h *coreHandlers) gameClientBridgeCompanionResult(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeMethodNotAllowed(w, http.MethodPost)
return
}
request, err := decodeJSON[dto.GameClientBridgeResultRequest](r)
if err != nil {
writeDecodeError(w, err)
return
}
command, err := h.core.CompleteGameClientBridgeCommand(request.ToDomain(r.PathValue("commandId")))
if err != nil {
writeServiceError(w, err)
return
}
writeJSON(w, http.StatusOK, dto.GameClientBridgeResultFromDomain(command))
}
func (h *coreHandlers) gameClientBridgeCompanionSnapshot(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeMethodNotAllowed(w, http.MethodPost)
return
}
request, err := decodeJSON[dto.GameClientBridgeSnapshotIngestRequest](r)
if err != nil {
writeDecodeError(w, err)
return
}
snapshot, err := h.core.UploadGameClientBridgeSnapshot(request.ToDomain())
if err != nil {
writeServiceError(w, err)
return
}
writeJSON(w, http.StatusAccepted, dto.GameClientBridgeSnapshotIngestFromDomain(snapshot))
}
func (h *coreHandlers) gameClientBridgeCompanionDiagnostics(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeMethodNotAllowed(w, http.MethodPost)
return
}
request, err := decodeJSON[dto.GameClientBridgeSnapshotIngestRequest](r)
if err != nil {
writeDecodeError(w, err)
return
}
if request.Type != "companion.health" && request.Type != "bridge.diagnostics" {
writeAPIError(w, http.StatusBadRequest, errorCodeValidation, "validation failed", []string{"diagnostic snapshot type is invalid"})
return
}
snapshot, err := h.core.UploadGameClientBridgeSnapshot(request.ToDomain())
if err != nil {
writeServiceError(w, err)
return
}
writeJSON(w, http.StatusAccepted, dto.GameClientBridgeSnapshotIngestFromDomain(snapshot))
}