-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (81 loc) · 1.89 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
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
95
96
97
98
99
100
101
package main
import (
"flag"
"github.com/Topface/diplodocus"
"github.com/fsnotify/fsnotify"
"log"
"net/http"
"os"
"path/filepath"
)
func main() {
listen := flag.String("listen", "", "addreess to listen, example: 127.0.0.1:8000")
root := flag.String("root", "", "root dir for logs")
flag.Parse()
if *listen == "" || *root == "" {
flag.PrintDefaults()
return
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("watcher creation error:", err)
}
m := diplodocus.NewFileManager(watcher)
go func() {
err := m.Watch()
if err != nil {
log.Fatal("watch error:", err)
}
}()
err = filepath.Walk(*root, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
log.Println("watching", path)
return watcher.Add(path)
}
return nil
})
if err != nil {
log.Fatal("watch path adding error:", err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
path := *root + req.URL.Path
file, err := m.GetFile(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
listener := make(diplodocus.Listener, 10)
file.AddListener(listener)
log.Println("listener added for:", path)
var flusher http.Flusher
if f, ok := w.(http.Flusher); ok {
flusher = f
} else {
log.Println("damn, no flushing")
}
for ev := range listener {
if ev.Error != nil {
log.Println("listener error for", path, ev.Error)
file.RemoveListener(listener)
return
}
_, err := w.Write(*ev.Buffer)
if err != nil {
log.Println("write error for", path, err)
file.RemoveListener(listener)
return
}
flusher.Flush()
}
log.Println("listener finished (probably timeout) for", path)
})
log.Println("listening", *listen)
err = http.ListenAndServe(*listen, mux)
if err != nil {
log.Fatal("listen error:", err)
}
}