-
Notifications
You must be signed in to change notification settings - Fork 30
/
http.go
104 lines (89 loc) · 2.6 KB
/
http.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
package gossm
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"time"
)
func calculateServerUptime(statusAtTime []*statusAtTime) string {
if len(statusAtTime) == 0 {
return "unknown"
}
var sum float64
for _, val := range statusAtTime {
var i float64
if val.Status {
i = 1
} else {
i = 0
}
sum += i
}
return fmt.Sprintf("%.2f", sum/float64(len(statusAtTime))*100)
}
func lastStatus(statusAtTime []*statusAtTime) string {
if len(statusAtTime) == 0 {
return "Not yet checked"
}
lastChecked := statusAtTime[len(statusAtTime)-1]
difference := time.Since(lastChecked.Time)
status := "OK"
if !lastChecked.Status {
status = "ERR"
}
return fmt.Sprintf("%s, %.0f seconds ago", status, difference.Seconds())
}
func RunHttp(address string, monitor *Monitor) {
funcMap := template.FuncMap{
"calculateServerUptime": calculateServerUptime,
"lastStatus": lastStatus,
}
t := template.Must(template.New("main").Funcs(funcMap).Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>GOSSM - Dashboard</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
<div class="container">
<br>
<center><h1>Dashboard</h1></center>
<hr>
<div class="row">
{{ range $server, $statusAtTime := .}}
<div class="col-md-4">
<div class="card" style="margin-top: 5px;">
<div class="card-body">
<h4 class="card-title">{{ $server.Name }}</h4>
<p class="card-text">{{ $server }}<br>tested {{ len $statusAtTime }} times<br>{{ $statusAtTime | lastStatus }}</p>
<a href="#" class="btn btn-primary">{{ $statusAtTime | calculateServerUptime }}%</a>
</div>
</div>
</div>
{{ end }}
</div>
</div>
</body>
</html>`))
http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
t.Execute(rw, monitor.serverStatusData.GetServerStatus())
})
http.HandleFunc("/json", func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json")
jsonBytes, err := json.Marshal(monitor.serverStatusData.GetServerStatus())
if err != nil {
jsonError, _ := json.Marshal(struct {
Message string `json:"message"`
}{Message: "Unable to format JSON."})
rw.Write(jsonError)
return
}
rw.Write(jsonBytes)
})
http.ListenAndServe(address, nil)
}