forked from metahertz/gowebtail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (60 loc) · 1.29 KB
/
main.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
package main
import (
"golang.org/x/net/websocket"
"flag"
"fmt"
"github.com/howeyc/fsnotify"
"log"
"net/http"
"os"
"strings"
"text/template"
)
var homeHtml = string(home_html())
var addr = flag.String("addr", ":8080", "http service address")
var homeTempl = template.Must(template.New("homeHtml").Parse(homeHtml))
func homeHandler(c http.ResponseWriter, req *http.Request) {
homeTempl.Execute(c, req.Host)
}
func MonitorMultiFiles(names []string, out chan []string,
h hub) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
for _, name := range names {
result, _ := ReadLastNLines(name, 10)
PrintMultiLines(result)
MonitorFile(name, out, watcher)
}
for {
select {
case lines := <-out:
content := strings.Join(lines, "\n")
fmt.Print(content)
h.broadcast <- content
}
}
watcher.Close()
}
var usage = func() {
fmt.Fprintf(os.Stderr,
"Usage: gowebtail [FILE]...\n")
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Usage = usage
flag.Parse()
if len(flag.Args()) < 1 {
usage()
}
out := make(chan []string)
go h.run()
go MonitorMultiFiles(flag.Args(), out, h)
http.HandleFunc("/", homeHandler)
http.Handle("/ws", websocket.Handler(wsHandler))
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}