-
Notifications
You must be signed in to change notification settings - Fork 0
/
toukei.go
115 lines (92 loc) · 2.33 KB
/
toukei.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"github.com/seasonlabs/toukei/checker"
"go/build"
)
import "github.com/kylelemons/go-gypsy/yaml"
import "github.com/simonz05/godis"
import "code.google.com/p/go.net/websocket"
const basePkg = "github.com/seasonlabs/toukei/"
var port *int = flag.Int("p", 8080, "Port to listen.")
func rootDir() string {
// find and serve static files
p, err := build.Default.Import(basePkg, "", build.FindOnly)
if err != nil {
log.Fatalf("Couldn't find toukei files: %v", err)
}
root := p.Dir
return root
}
func assetsDir() string {
return rootDir() + "/assets"
}
func main() {
flag.Parse()
config, err := yaml.ReadFile(rootDir() + "/config.yml")
if err != nil {
log.Fatalf("readfile(%q): %s", "config.yml", err)
}
path, err := config.Get("path")
if err != nil {
log.Fatal(err)
}
go checker.Check(path)
go listen()
http.Handle("/json", websocket.Handler(jsonServer))
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir(assetsDir()))))
http.HandleFunc("/", MainServer)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}
func listen() {
c := godis.New("", 0, "")
s, err := c.Subscribe("toukei")
if err != nil {
log.Fatal(err)
}
for {
m := <-s.Messages
println(m.Elem.String())
}
}
func MainServer(w http.ResponseWriter, req *http.Request) {
t := template.Must(template.New("foo").ParseGlob(rootDir() + "/index.html"))
if err := t.ExecuteTemplate(w, "index", req.Host+":"+req.URL.Scheme); err != nil {
log.Fatal(err)
}
}
// jsonServer handles listening to reddis messages and push the result to connected clients
func jsonServer(ws *websocket.Conn) {
c := godis.New("", 0, "")
elem, err := c.Get("toukei")
if err != nil {
log.Println(err)
elem = []byte("{}")
}
println(string(elem))
websocketSend(ws, elem)
s, err := c.Subscribe("toukei")
if err != nil {
log.Fatal(err)
}
for {
m := <-s.Messages
websocketSend(ws, m.Elem)
}
}
func websocketSend(ws *websocket.Conn, elem []byte) {
var stat checker.Stat
if err := json.Unmarshal(elem, &stat); err != nil {
log.Fatal(err)
}
// Send send a text message serialized T as JSON.
if err := websocket.JSON.Send(ws, stat); err != nil {
log.Fatal(err)
}
fmt.Printf("send: %#v\n", stat)
}