spaxel/mothership/internal/api/utils.go
jedarden 01547269cc feat: verify dashboard WebSocket feed supports events, alerts, BLE, triggers, health
All 5 new message types (event, alert, ble_scan, trigger_state,
system_health) were already implemented in hub.go with broadcast methods,
called from main.go/ingestion/volume_triggers/events, and handled in
app.js. Also includes security mode persistence from anomaly DB and
OpenAPI docs for triggers endpoints.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-07 09:54:14 -04:00

36 lines
1.1 KiB
Go

// Package api provides REST API handlers for Spaxel.
package api
import (
"encoding/json"
"log"
"net/http"
"strings"
)
// writeJSON writes a JSON response with the given status code.
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(v); err != nil {
log.Printf("[ERROR] Failed to encode JSON response: %v", err)
}
}
// writeJSONData writes a JSON response without setting status (assumes status already set).
func writeJSONData(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(v); err != nil {
log.Printf("[ERROR] Failed to encode JSON response: %v", err)
}
}
// writeJSONError writes a JSON error response.
func writeJSONError(w http.ResponseWriter, status int, message string) {
writeJSON(w, status, map[string]string{"error": message})
}
// joinComma joins a slice of strings with ", " separator.
func joinComma(parts []string) string {
return strings.Join(parts, ", ")
}