forked from mroth/sseserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
85 lines (72 loc) · 1.97 KB
/
admin.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
package sseserver
import (
"encoding/json"
"fmt"
"net/http"
"os"
"sort"
"time"
)
type hubStatus struct {
Node string `json:"node"`
Status string `json:"status"`
Reported int64 `json:"reported_at"`
StartupTime int64 `json:"startup_time"`
SentMsgs uint64 `json:"msgs_broadcast"`
Connections connStatusList `json:"connections"`
}
// Status returns the status struct for a given server.
// This mirrors what is made available on the /admin/status.json endpoint.
//
// Mostly useful for reporting.
func (s *Server) Status() hubStatus {
return s.hub.Status()
}
// implement sort.Interface for the connections
type connStatusList []connectionStatus
func (cl connStatusList) Len() int { return len(cl) }
func (cl connStatusList) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] }
func (cl connStatusList) Less(i, j int) bool { return cl[i].Created < cl[j].Created }
// Status returns the status struct for a given connection hub.
// This hub is the real source of truth and Server is just a wrapper but people
// don't know that...
func (h *hub) Status() hubStatus {
stat := hubStatus{
Node: fmt.Sprintf("%s-%s-%s", platform(), env(), dyno()),
Status: "OK",
Reported: time.Now().Unix(),
StartupTime: h.startupTime.Unix(),
SentMsgs: h.sentMsgs,
}
stat.Connections = connStatusList{}
for k := range h.connections {
stat.Connections = append(stat.Connections, k.Status())
}
sort.Sort(stat.Connections)
return stat
}
func platform() string {
return "go"
}
func dyno() string {
dyno := os.Getenv("DYNO")
if dyno != "" {
return dyno
} else {
return "dev.1"
}
}
func env() string {
env := os.Getenv("GO_ENV")
if env != "" {
return env
} else {
return "development"
}
}
func adminStatusDataHandler(w http.ResponseWriter, r *http.Request, h *hub) {
w.Header().Set("Content-Type", "application/json")
b, _ := json.MarshalIndent(h.Status(), "", " ")
fmt.Fprint(w, string(b))
return
}