120 lines
2.9 KiB
Go
120 lines
2.9 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) {
|
|
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()
|
|
}
|
|
}
|
|
}
|