-
Notifications
You must be signed in to change notification settings - Fork 0
/
placeholder.go
62 lines (51 loc) · 1.43 KB
/
placeholder.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
package main
import (
"encoding/json"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
// ActionFunc describes the main action to run when this command is invoked
type PlaceHolderFunc func() string
type ServerStatus struct {
Online bool `json:"online"`
ViewerCount int `json:"viewerCount"`
OverallMaxViewerCount int `json:"overallMaxViewerCount"`
SessionMaxViewerCount int `json:"sessionMaxViewerCount"`
LastConnectTime time.Time `json:"lastConnectTime"`
LastDisconnectTime time.Time `json:"lastDisconnectTime"`
VersionNumber string `json:"versionNumber"`
}
var (
serverStatus *ServerStatus // singleton
)
var (
Uptime PlaceHolderFunc = func() string {
ss, err := getServerStatus()
if err != nil {
log.Errorln(err)
return ""
}
diff := time.Now().Sub(ss.LastConnectTime)
return diff.Truncate(time.Second).String()
}
)
// getServerStatus discover when a server is broadcasting,
// the number of active viewers as well as
// other useful information for updating the user interface.
func getServerStatus() (*ServerStatus, error) {
if serverStatus != nil {
return serverStatus, nil
}
resp, err := http.DefaultClient.Get(ServerURL + "/api/status")
if err != nil {
return nil, err
}
defer resp.Body.Close()
var ss ServerStatus
if err := json.NewDecoder(resp.Body).Decode(&ss); err != nil {
return nil, err
}
serverStatus = &ss
return &ss, nil
}