/
main.go
83 lines (65 loc) · 1.41 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
package main
import (
"github.com/codegangsta/martini"
"github.com/dustin/go-humanize"
"github.com/tanner/isgtwifidown.com/gtwifi"
"html/template"
"log"
"net/http"
"time"
)
type PageData struct {
Green bool
Yellow bool
Red bool
Reason string
LastUpdated string
}
type LastData struct {
Status int
Reason string
TimeRetrieved time.Time
}
var lastData LastData
func main() {
m := martini.Classic()
template := template.Must(template.ParseFiles("index.tmpl"))
schedule(checkData, 10*time.Minute)
checkData()
m.Get("/", func(res http.ResponseWriter, req *http.Request) {
data := PageData{}
data.Green = lastData.Status == gtwifi.GREEN
data.Yellow = lastData.Status == gtwifi.YELLOW
data.Red = lastData.Status == gtwifi.RED
data.Reason = lastData.Reason
data.LastUpdated = humanize.Time(lastData.TimeRetrieved)
template.Execute(res, data)
})
m.Run()
}
func checkData() {
log.Println("Retrieving new data...")
status, err := gtwifi.GetStatus()
if err != nil {
log.Println(err)
return
}
log.Println("New data retrieved!")
lastData = LastData{status.Status, status.Reason, time.Now()}
}
func schedule(caller func(), delay time.Duration) chan bool {
ticker := time.NewTicker(delay)
stop := make(chan bool)
go func() {
for {
select {
case <-ticker.C:
caller()
case <-stop:
ticker.Stop()
return
}
}
}()
return stop
}