File size: 1,989 Bytes
84ffb7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package web

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
	"time"
)

// sseLogEvent is the JSON payload sent down the log stream.
type sseLogEvent struct {
	Text  string `json:"text"`
	Color string `json:"color"`
	Ts    int64  `json:"ts"`
}

// handleLogStream streams backend log lines to the browser over
// Server-Sent Events: history is replayed first, then new lines are pushed
// as they are emitted. Idle connections get a comment heartbeat so proxies
// don't close them.
func (s *Server) handleLogStream(w http.ResponseWriter, r *http.Request) {
	rc := http.NewResponseController(w)
	// The server's WriteTimeout would kill a long-lived stream; clear it.
	_ = rc.SetWriteDeadline(time.Time{})

	w.Header().Set("Content-Type", "text/event-stream")
	w.Header().Set("Cache-Control", "no-cache")
	w.Header().Set("Connection", "keep-alive")
	w.Header().Set("X-Accel-Buffering", "no")

	s.log.Info("log stream opened")
	for _, line := range s.hub.History() {
		writeLogEvent(w, line)
	}
	if err := rc.Flush(); err != nil {
		s.log.Warn("log stream flush", "error", err)
		return
	}

	ch := s.hub.Subscribe(r.Context())
	ticker := time.NewTicker(15 * time.Second)
	defer ticker.Stop()

	for {
		select {
		case <-r.Context().Done():
			return
		case line := <-ch:
			writeLogEvent(w, line)
			_ = rc.Flush()
		case <-ticker.C:
			fmt.Fprint(w, ": ping\n\n")
			_ = rc.Flush()
		}
	}
}

// writeLogEvent sends one line as an SSE data frame.
func writeLogEvent(w http.ResponseWriter, line string) {
	event, _ := json.Marshal(sseLogEvent{Text: line, Color: classifyLogLine(line), Ts: time.Now().UnixMilli()})
	fmt.Fprintf(w, "data: %s\n\n", event)
}

// classifyLogLine picks a terminal color class from the line's level token.
func classifyLogLine(line string) string {
	switch {
	case strings.Contains(line, "ERROR"):
		return "error"
	case strings.Contains(line, "WARN"):
		return "warn"
	case strings.Contains(line, "DEBUG"):
		return "dim"
	default:
		return "default"
	}
}