package httpapi import ( "encoding/json" "fmt" "net/http" "strings" ) func writeJSON(w http.ResponseWriter, status int, value any) { w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(value) } func writeError(w http.ResponseWriter, status int, message string, codes ...string) { errorPayload := map[string]any{ "message": message, "status": status, } if len(codes) > 0 { if code := strings.TrimSpace(codes[0]); code != "" { errorPayload["code"] = code } } writeJSON(w, status, map[string]any{"error": errorPayload}) } func sendSSE(w http.ResponseWriter, event string, payload any) { bytes, _ := json.Marshal(payload) _, _ = fmt.Fprintf(w, "event: %s\n", event) _, _ = fmt.Fprintf(w, "data: %s\n\n", bytes) }