forked from appleboy/gorush
/
status.go
97 lines (82 loc) · 2.4 KB
/
status.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
package gorush
import (
"errors"
"net/http"
"github.com/appleboy/gorush/storage/boltdb"
"github.com/appleboy/gorush/storage/buntdb"
"github.com/appleboy/gorush/storage/leveldb"
"github.com/appleboy/gorush/storage/memory"
"github.com/appleboy/gorush/storage/redis"
"github.com/gin-gonic/gin"
"github.com/thoas/stats"
)
// Stats provide response time, status code count, etc.
var Stats = stats.New()
// StatusApp is app status structure
type StatusApp struct {
Version string `json:"version"`
QueueMax int `json:"queue_max"`
QueueUsage int `json:"queue_usage"`
TotalCount int64 `json:"total_count"`
Ios IosStatus `json:"ios"`
Android AndroidStatus `json:"android"`
}
// AndroidStatus is android structure
type AndroidStatus struct {
PushSuccess int64 `json:"push_success"`
PushError int64 `json:"push_error"`
}
// IosStatus is iOS structure
type IosStatus struct {
PushSuccess int64 `json:"push_success"`
PushError int64 `json:"push_error"`
}
// InitAppStatus for initialize app status
func InitAppStatus() error {
switch PushConf.Stat.Engine {
case "memory":
StatStorage = memory.New()
case "redis":
StatStorage = redis.New(PushConf)
case "boltdb":
StatStorage = boltdb.New(PushConf)
case "buntdb":
StatStorage = buntdb.New(PushConf)
case "leveldb":
StatStorage = leveldb.New(PushConf)
default:
err := errors.New("can't find storage driver")
if err != nil {
LogError.Error("storage error: " + err.Error())
return err
}
}
if err := StatStorage.Init(); err != nil {
LogError.Error("storage error: " + err.Error())
return err
}
return nil
}
func appStatusHandler(c *gin.Context) {
result := StatusApp{}
result.Version = GetVersion()
result.QueueMax = cap(QueueNotification)
result.QueueUsage = len(QueueNotification)
result.TotalCount = StatStorage.GetTotalCount()
result.Ios.PushSuccess = StatStorage.GetIosSuccess()
result.Ios.PushError = StatStorage.GetIosError()
result.Android.PushSuccess = StatStorage.GetAndroidSuccess()
result.Android.PushError = StatStorage.GetAndroidError()
c.JSON(http.StatusOK, result)
}
func sysStatsHandler(c *gin.Context) {
c.JSON(http.StatusOK, Stats.Data())
}
// StatMiddleware response time, status code count, etc.
func StatMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
beginning, recorder := Stats.Begin(c.Writer)
c.Next()
Stats.End(beginning, recorder)
}
}