-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatc_server.go
More file actions
96 lines (83 loc) · 2.59 KB
/
Copy pathatc_server.go
File metadata and controls
96 lines (83 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package netpipe
import (
"encoding/json"
"fmt"
"net/http"
)
// ATCServer is the HTTP server that serves the ATC dashboard and streams
// telemetry events via Server-Sent Events (SSE). No external dependencies.
//
// Endpoints:
// GET / - serves the ATC dashboard HTML
// GET /events - SSE stream of real-time events
// GET /snapshot - JSON snapshot of current state
type ATCServer struct {
bus *ATCBus
port int
snapshotFunc func() ATCSnapshot // callback to get current state
}
// newATCServer creates the HTTP server. Call start() to begin serving.
func newATCServer(bus *ATCBus, port int, snapshotFunc func() ATCSnapshot) *ATCServer {
return &ATCServer{
bus: bus,
port: port,
snapshotFunc: snapshotFunc,
}
}
// start begins serving the ATC dashboard. Runs in a goroutine.
func (a *ATCServer) start() {
mux := http.NewServeMux()
mux.HandleFunc("/", a.handleDashboard)
mux.HandleFunc("/events", a.handleSSE)
mux.HandleFunc("/snapshot", a.handleSnapshot)
addr := fmt.Sprintf(":%d", a.port)
fmt.Printf("netpipe ATC dashboard: http://localhost%s\n", addr)
go http.ListenAndServe(addr, mux)
}
// handleDashboard serves the embedded HTML dashboard.
func (a *ATCServer) handleDashboard(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(atcDashboardHTML))
}
// handleSSE streams events to the dashboard via Server-Sent Events.
func (a *ATCServer) handleSSE(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "SSE not supported", http.StatusInternalServerError)
return
}
// send recent log as initial batch
for _, evt := range a.bus.recentLog() {
data := marshalEvent(evt)
fmt.Fprintf(w, "data: %s\n\n", data)
}
flusher.Flush()
// subscribe to live events
ch := a.bus.subscribe()
defer a.bus.unsubscribe(ch)
ctx := r.Context()
for {
select {
case <-ctx.Done():
return
case evt, ok := <-ch:
if !ok {
return
}
data := marshalEvent(evt)
fmt.Fprintf(w, "data: %s\n\n", data)
flusher.Flush()
}
}
}
// handleSnapshot returns a JSON snapshot of the current state.
func (a *ATCServer) handleSnapshot(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
snapshot := a.snapshotFunc()
json.NewEncoder(w).Encode(snapshot)
}