-
Notifications
You must be signed in to change notification settings - Fork 4
/
handlers.go
51 lines (46 loc) · 1.18 KB
/
handlers.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
package main
import (
"html/template"
"log"
"net/http"
"os"
"github.com/hpcloud/tail"
"golang.org/x/net/websocket"
)
func handleHome(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
t := template.Must(template.New("base").Parse(string(MustAsset(opts.Template))))
v := struct {
Host string
Log string
}{
r.Host,
opts.Log,
}
if err := t.Execute(w, &v); err != nil {
log.Printf("Template execute failed, err: %v", err)
return
}
}
func handleTail(w http.ResponseWriter, r *http.Request) {
t, err := tail.TailFile(opts.Log, tail.Config{Follow: true, ReOpen: true, Location: &tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}})
if err != nil {
log.Printf("tail file failed, err: %v", err)
return
}
for line := range t.Lines {
log.Println(line.Text)
w.Write([]byte(line.Text))
}
}
func handleFollow(ws *websocket.Conn) {
t, err := tail.TailFile(opts.Log, tail.Config{Follow: true, ReOpen: true, Location: &tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}})
if err != nil {
log.Printf("tail file failed, err: %v", err)
return
}
for line := range t.Lines {
log.Println(line.Text)
ws.Write([]byte(line.Text))
}
}