-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (48 loc) · 1.02 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Count struct {
Count int `json:"count"`
}
var count = Count{Count: 0}
func main() {
addr, found := syscall.Getenv("ADDR")
if !found {
addr = ":8080"
}
file, err := ioutil.ReadFile("data/count.json")
if err == nil {
json.Unmarshal(file, &count)
}
hub := newHub()
go hub.run()
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
fs := http.FileServer(http.Dir("static"))
http.Handle("/", fs)
metrics := promhttp.Handler()
http.HandleFunc("/metrics", metrics.ServeHTTP)
go func() {
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatal(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
res, err := json.Marshal(count)
if err != nil {
panic(err)
}
os.Mkdir("data", 0777)
ioutil.WriteFile("./data/count.json", res, 0644)
}