-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
94 lines (80 loc) · 2 KB
/
logs.go
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
package api
import (
"bufio"
"fmt"
"net/http"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/kunalsin9h/meltcd/internal/core"
"github.com/valyala/fasthttp"
)
// LiveLogs godoc
//
// @summary Get Live Logs using SSE
// @tags General
// @security ApiKeyAuth
// @success 200 string string
// @router /logs/live [get]
func LiveLogs(c *fiber.Ctx) error {
// Server Sent Events
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "Content-Type")
c.Status(http.StatusOK)
logsStream := make(chan []byte)
core.CurrentSession.AddSession(&logsStream)
notifyConnClose := c.Context().Done()
c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
keepAliveTickler := time.NewTicker(15 * time.Second)
go func() {
<-notifyConnClose
core.CurrentSession.RemoveSession(&logsStream)
}()
for {
select {
case l := <-logsStream:
_, err := fmt.Fprint(w, formatSSEMessage("log", string(l)))
if err != nil {
continue
}
err = w.Flush()
// Connection is closed now
if err != nil {
core.CurrentSession.RemoveSession(&logsStream)
return
}
case <-keepAliveTickler.C:
fmt.Fprint(w, formatSSEMessage("message", "keepalive"))
err := w.Flush()
// Connection is closed now
if err != nil {
core.CurrentSession.RemoveSession(&logsStream)
return
}
}
}
}))
return nil
}
// Logs godoc
//
// @summary Get Logs
// @tags General
// @security ApiKeyAuth
// @success 200 string string
// @router /logs [get]
func Logs(c *fiber.Ctx) error {
c.Status(http.StatusOK)
return nil
}
func formatSSEMessage(eventType, data string) string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("event: %s\n", eventType))
sb.WriteString(fmt.Sprintf("retry: %d\n", 15000))
sb.WriteString(fmt.Sprintf("data: %v\n\n", data))
return sb.String()
}