-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebtail.go
44 lines (39 loc) · 845 Bytes
/
webtail.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
package main
import (
"bufio"
"flag"
"github.com/dustin/go-broadcast"
"github.com/gin-gonic/gin"
"io"
"os"
)
var (
broadcaster broadcast.Broadcaster
addr = flag.String("addr", "0.0.0.0:2333", "run on given address")
)
func runWebServer() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.SetHTMLTemplate(html)
r.GET("/", func(c *gin.Context) {
c.HTML(200, "webtail", nil)
})
r.GET("/log", func(c *gin.Context) {
listener := make(chan interface{})
broadcaster.Register(listener)
defer broadcaster.Unregister(listener)
c.Stream(func(w io.Writer) bool {
c.SSEvent("message", <-listener)
return true
})
})
r.Run(*addr)
}
func main() {
broadcaster = broadcast.NewBroadcaster(10)
scanner := bufio.NewScanner(os.Stdin)
go runWebServer()
for scanner.Scan() {
broadcaster.Submit(scanner.Text())
}
}