Files
Unified_Dashboard_Suite_Go/internal/khira/events.go
T
vaishnosingh cc6bed127b feat(khira): complete remediation, performance optimization, and Cloudflare Turnstile integration
- Backend:
  - Fix ListTickets parameter order mismatch for assignee filtering
  - Replicate all 13 PostgreSQL tables and columns during disaster recovery sync and rollback
  - Implement server-side session store with HttpOnly cookies and Bearer tokens
  - Add profile update verification requiring current password
  - Whitelist allowed file uploads and apply strict security headers (nosniff, CSP)
  - Enforce role check on error deletion (admin/lead/tester only) and authenticate SSE stream
  - Align foreign key user_id for saved filters and api tokens
  - Add ReopenTicket endpoint and workflow
  - Integrate Cloudflare Turnstile verification with testing/production key support
- Frontend:
  - Remove liquid glass displacement filter and lens capsule for high-performance UI
  - Eliminate recursive backdrop-filters and heavy inset shadows
  - Add debounced search and optimize SSE re-rendering
  - Wire Saved Views filter bar with creation, active indicators, and deletion
  - Add Linked & Related Tickets management UI to sheet
  - Seed 6 demo personas in PostgreSQL with instant sign-in
  - Add Cloudflare Turnstile widgets to authentication modals
  - Add keyboard arrow navigation to Command Palette
- Image Manipulator, Docs & Tooling:
  - Sync image manipulator updates, docs, and test coordinates
2026-11-17 18:39:25 +05:30

126 lines
3.0 KiB
Go

package khira
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
)
type RealtimeEvent struct {
Type string `json:"type"` // "ticket_created", "ticket_updated", "ticket_deleted", "ticket_archived", "ticket_completed", "remark_added", "error_logged", "notification", etc.
TicketID string `json:"ticket_id,omitempty"` // ID of the ticket affected
ErrorID string `json:"error_id,omitempty"` // ID of the runtime error affected
Actor string `json:"actor,omitempty"` // Username who performed the action
Action string `json:"action,omitempty"` // Action verb
Payload interface{} `json:"payload,omitempty"` // Extra metadata
Timestamp time.Time `json:"timestamp"`
}
type EventBroker struct {
mu sync.RWMutex
clients map[chan RealtimeEvent]struct{}
}
func NewEventBroker() *EventBroker {
return &EventBroker{
clients: make(map[chan RealtimeEvent]struct{}),
}
}
func (b *EventBroker) Subscribe() chan RealtimeEvent {
b.mu.Lock()
defer b.mu.Unlock()
ch := make(chan RealtimeEvent, 64)
b.clients[ch] = struct{}{}
return ch
}
func (b *EventBroker) Unsubscribe(ch chan RealtimeEvent) {
b.mu.Lock()
defer b.mu.Unlock()
if _, ok := b.clients[ch]; ok {
delete(b.clients, ch)
close(ch)
}
}
func (b *EventBroker) Broadcast(eventType, ticketID, errorID, actor, action string, payload interface{}) {
if b == nil {
return
}
evt := RealtimeEvent{
Type: eventType,
TicketID: ticketID,
ErrorID: errorID,
Actor: actor,
Action: action,
Payload: payload,
Timestamp: time.Now(),
}
b.mu.RLock()
defer b.mu.RUnlock()
for ch := range b.clients {
select {
case ch <- evt:
default:
// Non-blocking drop if channel buffer full
}
}
}
// handleEventsSSE handles Server-Sent Events stream for connected clients
func (s *Server) handleEventsSSE(w http.ResponseWriter, r *http.Request) {
actor := s.getActor(r)
if actor == nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache, no-transform")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("X-Accel-Buffering", "no")
ch := s.Store.Broker.Subscribe()
defer s.Store.Broker.Unsubscribe(ch)
// Send initial handshake
fmt.Fprintf(w, "event: connected\ndata: {\"status\":\"live\",\"time\":%d}\n\n", time.Now().Unix())
flusher.Flush()
// 15-second heartbeat ticker to maintain proxy connection
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
ctx := r.Context()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
fmt.Fprintf(w, ": ping\n\n")
flusher.Flush()
case evt, ok := <-ch:
if !ok {
return
}
data, err := json.Marshal(evt)
if err != nil {
continue
}
fmt.Fprintf(w, "event: message\ndata: %s\n\n", data)
flusher.Flush()
}
}
}