diff --git a/gorush/config.go b/config/config/config.go similarity index 99% rename from gorush/config.go rename to config/config/config.go index 4e8f60e..d28e616 100644 --- a/gorush/config.go +++ b/config/config/config.go @@ -1,4 +1,4 @@ -package gorush +package config import ( "gopkg.in/yaml.v2" diff --git a/gorush/config_test.go b/config/config/config_test.go similarity index 98% rename from gorush/config_test.go rename to config/config/config_test.go index 0888620..ae694d8 100644 --- a/gorush/config_test.go +++ b/config/config/config_test.go @@ -1,4 +1,4 @@ -package gorush +package config import ( "github.com/stretchr/testify/assert" diff --git a/gorush.go b/gorush.go index afc28df..13adf9e 100644 --- a/gorush.go +++ b/gorush.go @@ -3,6 +3,7 @@ package main import ( "flag" "github.com/appleboy/gorush/gorush" + "github.com/appleboy/gorush/gorush/config" "log" ) @@ -39,11 +40,11 @@ func main() { var err error // set default parameters. - gorush.PushConf = gorush.BuildDefaultPushConf() + gorush.PushConf = config.BuildDefaultPushConf() // load user define config. if *confPath != "" { - gorush.PushConf, err = gorush.LoadConfYaml(*confPath) + gorush.PushConf, err = config.LoadConfYaml(*confPath) if err != nil { log.Printf("Load yaml config file error: '%v'", err) diff --git a/gorush/global.go b/gorush/global.go index 9beab50..fbccf27 100644 --- a/gorush/global.go +++ b/gorush/global.go @@ -3,13 +3,14 @@ package gorush import ( "crypto/tls" "github.com/Sirupsen/logrus" + "github.com/appleboy/gorush/gorush/config" apns "github.com/sideshow/apns2" "gopkg.in/redis.v3" ) var ( // PushConf is gorush config - PushConf ConfYaml + PushConf config.ConfYaml // QueueNotification is chan type QueueNotification chan PushNotification // CertificatePemIos is ios certificate file @@ -20,8 +21,8 @@ var ( LogAccess *logrus.Logger // LogError is log server error log LogError *logrus.Logger - // RushStatus is notification status - RushStatus StatusApp // RedisClient is global variable for redis RedisClient *redis.Client + // StatStorage implements the storage interface + StatStorage Storage ) diff --git a/gorush/log_test.go b/gorush/log_test.go index b5cdbcc..41c6bd6 100644 --- a/gorush/log_test.go +++ b/gorush/log_test.go @@ -2,6 +2,7 @@ package gorush import ( "github.com/Sirupsen/logrus" + "github.com/appleboy/gorush/gorush/config" "github.com/stretchr/testify/assert" "testing" ) @@ -34,7 +35,7 @@ func TestSetLogOut(t *testing.T) { } func TestInitDefaultLog(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() // no errors on default config assert.Nil(t, InitLog()) @@ -45,7 +46,7 @@ func TestInitDefaultLog(t *testing.T) { } func TestAccessLevel(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Log.AccessLevel = "invalid" @@ -53,7 +54,7 @@ func TestAccessLevel(t *testing.T) { } func TestErrorLevel(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Log.ErrorLevel = "invalid" @@ -61,7 +62,7 @@ func TestErrorLevel(t *testing.T) { } func TestAccessLogPath(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Log.AccessLog = "logs/access.log" @@ -69,7 +70,7 @@ func TestAccessLogPath(t *testing.T) { } func TestErrorLogPath(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Log.ErrorLog = "logs/error.log" diff --git a/gorush/notification.go b/gorush/notification.go index 5a7ffec..ee68dc7 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -199,7 +199,7 @@ func queueNotification(req RequestPush) int { count += len(notification.Tokens) } - addTotalCount(int64(count)) + StatStorage.AddTotalCount(int64(count)) return count } @@ -315,7 +315,7 @@ func PushToIOS(req PushNotification) bool { // apns server error LogPush(FailedPush, token, req, err) isError = true - addIosError(1) + StatStorage.AddIosError(1) continue } @@ -323,13 +323,13 @@ func PushToIOS(req PushNotification) bool { // error message: // ref: https://github.com/sideshow/apns2/blob/master/response.go#L14-L65 LogPush(FailedPush, token, req, errors.New(res.Reason)) - addIosError(1) + StatStorage.AddIosError(1) continue } if res.Sent() { LogPush(SucceededPush, token, req, nil) - addIosSuccess(1) + StatStorage.AddIosSuccess(1) } } @@ -410,8 +410,8 @@ func PushToAndroid(req PushNotification) bool { } LogAccess.Debug(fmt.Sprintf("Android Success count: %d, Failure count: %d", res.Success, res.Failure)) - addAndroidSuccess(int64(res.Success)) - addAndroidError(int64(res.Failure)) + StatStorage.AddAndroidSuccess(int64(res.Success)) + StatStorage.AddAndroidError(int64(res.Failure)) for k, result := range res.Results { if result.Error != "" { diff --git a/gorush/notification_test.go b/gorush/notification_test.go index bf86965..394ab07 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -2,6 +2,7 @@ package gorush import ( "encoding/json" + "github.com/appleboy/gorush/gorush/config" "github.com/buger/jsonparser" "github.com/google/go-gcm" "github.com/sideshow/apns2" @@ -13,7 +14,7 @@ import ( ) func TestDisabledAndroidIosConf(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() err := CheckPushConf() @@ -22,7 +23,7 @@ func TestDisabledAndroidIosConf(t *testing.T) { } func TestMissingIOSCertificate(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true PushConf.Ios.PemKeyPath = "" @@ -34,7 +35,7 @@ func TestMissingIOSCertificate(t *testing.T) { } func TestMissingAndroidAPIKey(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Android.Enabled = true PushConf.Android.APIKey = "" @@ -46,7 +47,7 @@ func TestMissingAndroidAPIKey(t *testing.T) { } func TestCorrectConf(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Android.Enabled = true PushConf.Android.APIKey = "xxxxx" @@ -217,11 +218,12 @@ func TestAndroidNotificationStructure(t *testing.T) { } func TestPushToIOS(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem" InitAPNSClient() + InitAppStatus() req := PushNotification{ Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, @@ -234,7 +236,7 @@ func TestPushToIOS(t *testing.T) { } func TestPushToAndroidWrongAPIKey(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Android.Enabled = true PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") + "a" @@ -250,7 +252,7 @@ func TestPushToAndroidWrongAPIKey(t *testing.T) { } func TestPushToAndroidWrongToken(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Android.Enabled = true PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") @@ -266,7 +268,7 @@ func TestPushToAndroidWrongToken(t *testing.T) { } func TestPushToAndroidRightTokenForJSONLog(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Android.Enabled = true PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") @@ -286,7 +288,7 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) { } func TestPushToAndroidRightTokenForStringLog(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Android.Enabled = true PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") @@ -304,7 +306,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) { } func TestOverwriteAndroidAPIKey(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Android.Enabled = true PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") @@ -324,7 +326,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) { } func TestSenMultipleNotifications(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() InitWorkers(2, 2) @@ -359,7 +361,7 @@ func TestSenMultipleNotifications(t *testing.T) { } func TestDisabledAndroidNotifications(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem" @@ -392,7 +394,7 @@ func TestDisabledAndroidNotifications(t *testing.T) { } func TestDisabledIosNotifications(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = false PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem" @@ -425,7 +427,7 @@ func TestDisabledIosNotifications(t *testing.T) { } func TestMissingIosCertificate(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true PushConf.Ios.PemKeyPath = "test" @@ -435,7 +437,7 @@ func TestMissingIosCertificate(t *testing.T) { } func TestAPNSClientDevHost(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem" @@ -445,7 +447,7 @@ func TestAPNSClientDevHost(t *testing.T) { } func TestAPNSClientProdHost(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true PushConf.Ios.Production = true @@ -520,7 +522,7 @@ func TestGCMMessage(t *testing.T) { } func TestCheckAndroidMessage(t *testing.T) { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Android.Enabled = true PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") diff --git a/gorush/server_test.go b/gorush/server_test.go index 68b578b..c3f6f4b 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -2,6 +2,7 @@ package gorush import ( "github.com/appleboy/gofight" + "github.com/appleboy/gorush/gorush/config" "github.com/buger/jsonparser" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" @@ -15,7 +16,7 @@ import ( var goVersion = runtime.Version() func initTest() { - PushConf = BuildDefaultPushConf() + PushConf = config.BuildDefaultPushConf() PushConf.Core.Mode = "test" } diff --git a/gorush/status.go b/gorush/status.go index b221e82..724c2b4 100644 --- a/gorush/status.go +++ b/gorush/status.go @@ -1,12 +1,11 @@ package gorush import ( - "github.com/asdine/storm" + "github.com/appleboy/gorush/gorush/storage/boltdb" + "github.com/appleboy/gorush/gorush/storage/memory" + "github.com/appleboy/gorush/gorush/storage/redis" "github.com/gin-gonic/gin" - "gopkg.in/redis.v3" "net/http" - "strconv" - "sync/atomic" ) // StatusApp is app status structure @@ -30,234 +29,40 @@ type IosStatus struct { PushError int64 `json:"push_error"` } -func initApp() { - RushStatus.TotalCount = 0 - RushStatus.Ios.PushSuccess = 0 - RushStatus.Ios.PushError = 0 - RushStatus.Android.PushSuccess = 0 - RushStatus.Android.PushError = 0 -} +// InitAppStatus for initialize app status +func InitAppStatus() error { + switch PushConf.Stat.Engine { + case "memory": + StatStorage = memory.New() + case "redis": + StatStorage = redis.New(PushConf) + err := StatStorage.Init() -func initRedis() error { - RedisClient = redis.NewClient(&redis.Options{ - Addr: PushConf.Stat.Redis.Addr, - Password: PushConf.Stat.Redis.Password, - DB: PushConf.Stat.Redis.DB, - }) + if err != nil { + LogError.Error("redis error: " + err.Error()) - _, err := RedisClient.Ping().Result() + return err + } - if err != nil { - // redis server error - LogError.Error("Can't connect redis server: " + err.Error()) - - return err + case "boltdb": + StatStorage = boltdb.New(PushConf) + default: + StatStorage = memory.New() } - RushStatus.TotalCount = getTotalCount() - RushStatus.Ios.PushSuccess = getIosSuccess() - RushStatus.Ios.PushError = getIosError() - RushStatus.Android.PushSuccess = getAndroidSuccess() - RushStatus.Android.PushError = getAndroidError() - return nil } -func initBoltDB() { - RushStatus.TotalCount = getTotalCount() - RushStatus.Ios.PushSuccess = getIosSuccess() - RushStatus.Ios.PushError = getIosError() - RushStatus.Android.PushSuccess = getAndroidSuccess() - RushStatus.Android.PushError = getAndroidError() -} - -// InitAppStatus for initialize app status -func InitAppStatus() { - switch PushConf.Stat.Engine { - case "memory": - initApp() - case "redis": - initRedis() - case "boltdb": - initBoltDB() - default: - initApp() - } - -} - -func getRedisInt64Result(key string, count *int64) { - val, _ := RedisClient.Get(key).Result() - *count, _ = strconv.ParseInt(val, 10, 64) -} - -func boltdbSet(key string, count int64) { - db, _ := storm.Open(PushConf.Stat.BoltDB.Path) - db.Set(PushConf.Stat.BoltDB.Bucket, key, count) - defer db.Close() -} - -func boltdbGet(key string, count *int64) { - db, _ := storm.Open(PushConf.Stat.BoltDB.Path) - db.Get(PushConf.Stat.BoltDB.Bucket, key, count) - defer db.Close() -} - -func addTotalCount(count int64) { - switch PushConf.Stat.Engine { - case "memory": - atomic.AddInt64(&RushStatus.TotalCount, count) - case "redis": - RedisClient.Set(TotalCountKey, strconv.Itoa(int(count)), 0) - case "boltdb": - boltdbSet(TotalCountKey, count) - default: - atomic.AddInt64(&RushStatus.TotalCount, count) - } -} - -func addIosSuccess(count int64) { - switch PushConf.Stat.Engine { - case "memory": - atomic.AddInt64(&RushStatus.Ios.PushSuccess, count) - case "redis": - RedisClient.Set(IosSuccessKey, strconv.Itoa(int(count)), 0) - case "boltdb": - boltdbSet(IosSuccessKey, count) - default: - atomic.AddInt64(&RushStatus.Ios.PushSuccess, count) - } -} - -func addIosError(count int64) { - switch PushConf.Stat.Engine { - case "memory": - atomic.AddInt64(&RushStatus.Ios.PushError, count) - case "redis": - RedisClient.Set(IosErrorKey, strconv.Itoa(int(count)), 0) - case "boltdb": - boltdbSet(IosErrorKey, count) - default: - atomic.AddInt64(&RushStatus.Ios.PushError, count) - } -} - -func addAndroidSuccess(count int64) { - switch PushConf.Stat.Engine { - case "memory": - atomic.AddInt64(&RushStatus.Android.PushSuccess, count) - case "redis": - RedisClient.Set(AndroidSuccessKey, strconv.Itoa(int(count)), 0) - case "boltdb": - boltdbSet(AndroidSuccessKey, count) - default: - atomic.AddInt64(&RushStatus.Android.PushSuccess, count) - } -} - -func addAndroidError(count int64) { - switch PushConf.Stat.Engine { - case "memory": - atomic.AddInt64(&RushStatus.Android.PushError, count) - case "redis": - RedisClient.Set(AndroidErrorKey, strconv.Itoa(int(count)), 0) - case "boltdb": - boltdbSet(AndroidErrorKey, count) - default: - atomic.AddInt64(&RushStatus.Android.PushError, count) - } -} - -func getTotalCount() int64 { - var count int64 - switch PushConf.Stat.Engine { - case "memory": - count = atomic.LoadInt64(&RushStatus.TotalCount) - case "redis": - getRedisInt64Result(TotalCountKey, &count) - case "boltdb": - boltdbGet(TotalCountKey, &count) - default: - count = atomic.LoadInt64(&RushStatus.TotalCount) - } - - return count -} - -func getIosSuccess() int64 { - var count int64 - switch PushConf.Stat.Engine { - case "memory": - count = atomic.LoadInt64(&RushStatus.Ios.PushSuccess) - case "redis": - getRedisInt64Result(IosSuccessKey, &count) - case "boltdb": - boltdbGet(IosSuccessKey, &count) - default: - count = atomic.LoadInt64(&RushStatus.Ios.PushSuccess) - } - - return count -} - -func getIosError() int64 { - var count int64 - switch PushConf.Stat.Engine { - case "memory": - count = atomic.LoadInt64(&RushStatus.Ios.PushError) - case "redis": - getRedisInt64Result(IosErrorKey, &count) - case "boltdb": - boltdbGet(IosErrorKey, &count) - default: - count = atomic.LoadInt64(&RushStatus.Ios.PushError) - } - - return count -} - -func getAndroidSuccess() int64 { - var count int64 - switch PushConf.Stat.Engine { - case "memory": - count = atomic.LoadInt64(&RushStatus.Android.PushSuccess) - case "redis": - getRedisInt64Result(AndroidSuccessKey, &count) - case "boltdb": - boltdbGet(AndroidSuccessKey, &count) - default: - count = atomic.LoadInt64(&RushStatus.Android.PushSuccess) - } - - return count -} - -func getAndroidError() int64 { - var count int64 - switch PushConf.Stat.Engine { - case "memory": - count = atomic.LoadInt64(&RushStatus.Android.PushError) - case "redis": - getRedisInt64Result(AndroidErrorKey, &count) - case "boltdb": - boltdbGet(AndroidErrorKey, &count) - default: - count = atomic.LoadInt64(&RushStatus.Android.PushError) - } - - return count -} - func appStatusHandler(c *gin.Context) { result := StatusApp{} result.QueueMax = cap(QueueNotification) result.QueueUsage = len(QueueNotification) - result.TotalCount = getTotalCount() - result.Ios.PushSuccess = getIosSuccess() - result.Ios.PushError = getIosError() - result.Android.PushSuccess = getAndroidSuccess() - result.Android.PushError = getAndroidError() + 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) } diff --git a/gorush/status_test.go b/gorush/status_test.go index 044d4a1..fd51000 100644 --- a/gorush/status_test.go +++ b/gorush/status_test.go @@ -2,67 +2,92 @@ package gorush import ( "github.com/stretchr/testify/assert" - "sync/atomic" + // "sync/atomic" "testing" ) -func TestAddTotalCount(t *testing.T) { +// func TestAddTotalCount(t *testing.T) { +// InitAppStatus() +// addTotalCount(1000) + +// val := atomic.LoadInt64(&RushStatus.TotalCount) + +// assert.Equal(t, int64(1000), val) +// } + +// func TestAddIosSuccess(t *testing.T) { +// InitAppStatus() +// addIosSuccess(1000) + +// val := atomic.LoadInt64(&RushStatus.Ios.PushSuccess) + +// assert.Equal(t, int64(1000), val) +// } + +// func TestAddIosError(t *testing.T) { +// InitAppStatus() +// addIosError(1000) + +// val := atomic.LoadInt64(&RushStatus.Ios.PushError) + +// assert.Equal(t, int64(1000), val) +// } + +// func TestAndroidSuccess(t *testing.T) { +// InitAppStatus() +// addAndroidSuccess(1000) + +// val := atomic.LoadInt64(&RushStatus.Android.PushSuccess) + +// assert.Equal(t, int64(1000), val) +// } + +// func TestAddAndroidError(t *testing.T) { +// InitAppStatus() +// addAndroidError(1000) + +// val := atomic.LoadInt64(&RushStatus.Android.PushError) + +// assert.Equal(t, int64(1000), val) +// } + +func TestStatForMemoryEngine(t *testing.T) { + var val int64 + PushConf.Stat.Engine = "memory" InitAppStatus() - addTotalCount(1000) - val := atomic.LoadInt64(&RushStatus.TotalCount) + StatStorage.AddTotalCount(100) + StatStorage.AddIosSuccess(200) + StatStorage.AddIosError(300) + StatStorage.AddAndroidSuccess(400) + StatStorage.AddAndroidError(500) - assert.Equal(t, int64(1000), val) -} - -func TestAddIosSuccess(t *testing.T) { - InitAppStatus() - addIosSuccess(1000) - - val := atomic.LoadInt64(&RushStatus.Ios.PushSuccess) - - assert.Equal(t, int64(1000), val) -} - -func TestAddIosError(t *testing.T) { - InitAppStatus() - addIosError(1000) - - val := atomic.LoadInt64(&RushStatus.Ios.PushError) - - assert.Equal(t, int64(1000), val) -} - -func TestAndroidSuccess(t *testing.T) { - InitAppStatus() - addAndroidSuccess(1000) - - val := atomic.LoadInt64(&RushStatus.Android.PushSuccess) - - assert.Equal(t, int64(1000), val) -} - -func TestAddAndroidError(t *testing.T) { - InitAppStatus() - addAndroidError(1000) - - val := atomic.LoadInt64(&RushStatus.Android.PushError) - - assert.Equal(t, int64(1000), val) + val = StatStorage.GetTotalCount() + assert.Equal(t, int64(100), val) + val = StatStorage.GetIosSuccess() + assert.Equal(t, int64(200), val) + val = StatStorage.GetIosError() + assert.Equal(t, int64(300), val) + val = StatStorage.GetAndroidSuccess() + assert.Equal(t, int64(400), val) + val = StatStorage.GetAndroidError() + assert.Equal(t, int64(500), val) } func TestRedisServerSuccess(t *testing.T) { + PushConf.Stat.Engine = "redis" PushConf.Stat.Redis.Addr = "localhost:6379" - err := initRedis() + err := InitAppStatus() assert.NoError(t, err) } func TestRedisServerError(t *testing.T) { + PushConf.Stat.Engine = "redis" PushConf.Stat.Redis.Addr = "localhost:6370" - err := initRedis() + err := InitAppStatus() assert.Error(t, err) } @@ -73,22 +98,25 @@ func TestStatForRedisEngine(t *testing.T) { PushConf.Stat.Redis.Addr = "localhost:6379" InitAppStatus() - addTotalCount(10) - addIosSuccess(20) - addIosError(30) - addAndroidSuccess(40) - addAndroidError(50) + StatStorage.Init() + StatStorage.Reset() - val = getTotalCount() - assert.Equal(t, int64(10), val) - val = getIosSuccess() - assert.Equal(t, int64(20), val) - val = getIosError() - assert.Equal(t, int64(30), val) - val = getAndroidSuccess() - assert.Equal(t, int64(40), val) - val = getAndroidError() - assert.Equal(t, int64(50), val) + StatStorage.AddTotalCount(100) + StatStorage.AddIosSuccess(200) + StatStorage.AddIosError(300) + StatStorage.AddAndroidSuccess(400) + StatStorage.AddAndroidError(500) + + val = StatStorage.GetTotalCount() + assert.Equal(t, int64(100), val) + val = StatStorage.GetIosSuccess() + assert.Equal(t, int64(200), val) + val = StatStorage.GetIosError() + assert.Equal(t, int64(300), val) + val = StatStorage.GetAndroidSuccess() + assert.Equal(t, int64(400), val) + val = StatStorage.GetAndroidError() + assert.Equal(t, int64(500), val) } func TestDefaultEngine(t *testing.T) { @@ -96,22 +124,22 @@ func TestDefaultEngine(t *testing.T) { PushConf.Stat.Engine = "test" InitAppStatus() - addTotalCount(1) - addIosSuccess(2) - addIosError(3) - addAndroidSuccess(4) - addAndroidError(5) + StatStorage.AddTotalCount(100) + StatStorage.AddIosSuccess(200) + StatStorage.AddIosError(300) + StatStorage.AddAndroidSuccess(400) + StatStorage.AddAndroidError(500) - val = getTotalCount() - assert.Equal(t, int64(1), val) - val = getIosSuccess() - assert.Equal(t, int64(2), val) - val = getIosError() - assert.Equal(t, int64(3), val) - val = getAndroidSuccess() - assert.Equal(t, int64(4), val) - val = getAndroidError() - assert.Equal(t, int64(5), val) + val = StatStorage.GetTotalCount() + assert.Equal(t, int64(100), val) + val = StatStorage.GetIosSuccess() + assert.Equal(t, int64(200), val) + val = StatStorage.GetIosError() + assert.Equal(t, int64(300), val) + val = StatStorage.GetAndroidSuccess() + assert.Equal(t, int64(400), val) + val = StatStorage.GetAndroidError() + assert.Equal(t, int64(500), val) } func TestStatForBoltDBEngine(t *testing.T) { @@ -119,20 +147,22 @@ func TestStatForBoltDBEngine(t *testing.T) { PushConf.Stat.Engine = "boltdb" InitAppStatus() - addTotalCount(100) - addIosSuccess(200) - addIosError(300) - addAndroidSuccess(400) - addAndroidError(500) + StatStorage.Reset() - val = getTotalCount() + StatStorage.AddTotalCount(100) + StatStorage.AddIosSuccess(200) + StatStorage.AddIosError(300) + StatStorage.AddAndroidSuccess(400) + StatStorage.AddAndroidError(500) + + val = StatStorage.GetTotalCount() assert.Equal(t, int64(100), val) - val = getIosSuccess() + val = StatStorage.GetIosSuccess() assert.Equal(t, int64(200), val) - val = getIosError() + val = StatStorage.GetIosError() assert.Equal(t, int64(300), val) - val = getAndroidSuccess() + val = StatStorage.GetAndroidSuccess() assert.Equal(t, int64(400), val) - val = getAndroidError() + val = StatStorage.GetAndroidError() assert.Equal(t, int64(500), val) } diff --git a/gorush/storage.go b/gorush/storage.go index a17e64c..7c820dd 100644 --- a/gorush/storage.go +++ b/gorush/storage.go @@ -2,14 +2,16 @@ package gorush // Storage interface type Storage interface { - addTotalCount(int64) - addIosSuccess(int64) - addIosError(int64) - addAndroidSuccess(int64) - addAndroidError(int64) - getTotalCount() int64 - getIosSuccess() int64 - getIosError() int64 - getAndroidSuccess() int64 - getAndroidError() int64 + Init() error + Reset() + AddTotalCount(int64) + AddIosSuccess(int64) + AddIosError(int64) + AddAndroidSuccess(int64) + AddAndroidError(int64) + GetTotalCount() int64 + GetIosSuccess() int64 + GetIosError() int64 + GetAndroidSuccess() int64 + GetAndroidError() int64 } diff --git a/storage/boltdb/boltdb.go b/storage/boltdb/boltdb.go index 90e399f..4f9ba18 100644 --- a/storage/boltdb/boltdb.go +++ b/storage/boltdb/boltdb.go @@ -1,37 +1,40 @@ package boltdb import ( - "github.com/appleboy/gorush/gorush" + "github.com/appleboy/gorush/gorush/config" "github.com/asdine/storm" ) +// Stat variable for redis +const ( + TotalCountKey = "gorush-total-count" + IosSuccessKey = "gorush-ios-success-count" + IosErrorKey = "gorush-ios-error-count" + AndroidSuccessKey = "gorush-android-success-count" + AndroidErrorKey = "gorush-android-error-count" +) + // Storage implements the storage interface for gorush (https://github.com/appleboy/gorush) -func New(config gorush.ConfYaml, stat gorush.StatusApp) *Storage { +func New(config config.ConfYaml) *Storage { return &Storage{ - stat: stat, config: config, } } type Storage struct { - config gorush.ConfYaml - stat gorush.StatusApp + config config.ConfYaml } -func (s *Storage) initBoltDB() { - s.stat.TotalCount = s.getTotalCount() - s.stat.Ios.PushSuccess = s.getIosSuccess() - s.stat.Ios.PushError = s.getIosError() - s.stat.Android.PushSuccess = s.getAndroidSuccess() - s.stat.Android.PushError = s.getAndroidError() +func (s *Storage) Init() error { + return nil } -func (s *Storage) resetBoltDB() { - s.setBoltDB(gorush.TotalCountKey, 0) - s.setBoltDB(gorush.IosSuccessKey, 0) - s.setBoltDB(gorush.IosErrorKey, 0) - s.setBoltDB(gorush.AndroidSuccessKey, 0) - s.setBoltDB(gorush.AndroidErrorKey, 0) +func (s *Storage) Reset() { + s.setBoltDB(TotalCountKey, 0) + s.setBoltDB(IosSuccessKey, 0) + s.setBoltDB(IosErrorKey, 0) + s.setBoltDB(AndroidSuccessKey, 0) + s.setBoltDB(AndroidErrorKey, 0) } func (s *Storage) setBoltDB(key string, count int64) { @@ -46,62 +49,62 @@ func (s *Storage) getBoltDB(key string, count *int64) { defer db.Close() } -func (s *Storage) addTotalCount(count int64) { - total := s.getTotalCount() + count - s.setBoltDB(gorush.TotalCountKey, total) +func (s *Storage) AddTotalCount(count int64) { + total := s.GetTotalCount() + count + s.setBoltDB(TotalCountKey, total) } -func (s *Storage) addIosSuccess(count int64) { - total := s.getIosSuccess() + count - s.setBoltDB(gorush.IosSuccessKey, total) +func (s *Storage) AddIosSuccess(count int64) { + total := s.GetIosSuccess() + count + s.setBoltDB(IosSuccessKey, total) } -func (s *Storage) addIosError(count int64) { - total := s.getIosError() + count - s.setBoltDB(gorush.IosErrorKey, total) +func (s *Storage) AddIosError(count int64) { + total := s.GetIosError() + count + s.setBoltDB(IosErrorKey, total) } -func (s *Storage) addAndroidSuccess(count int64) { - total := s.getAndroidSuccess() + count - s.setBoltDB(gorush.AndroidSuccessKey, total) +func (s *Storage) AddAndroidSuccess(count int64) { + total := s.GetAndroidSuccess() + count + s.setBoltDB(AndroidSuccessKey, total) } -func (s *Storage) addAndroidError(count int64) { - total := s.getAndroidError() + count - s.setBoltDB(gorush.AndroidErrorKey, total) +func (s *Storage) AddAndroidError(count int64) { + total := s.GetAndroidError() + count + s.setBoltDB(AndroidErrorKey, total) } -func (s *Storage) getTotalCount() int64 { +func (s *Storage) GetTotalCount() int64 { var count int64 - s.getBoltDB(gorush.TotalCountKey, &count) + s.getBoltDB(TotalCountKey, &count) return count } -func (s *Storage) getIosSuccess() int64 { +func (s *Storage) GetIosSuccess() int64 { var count int64 - s.getBoltDB(gorush.IosSuccessKey, &count) + s.getBoltDB(IosSuccessKey, &count) return count } -func (s *Storage) getIosError() int64 { +func (s *Storage) GetIosError() int64 { var count int64 - s.getBoltDB(gorush.IosErrorKey, &count) + s.getBoltDB(IosErrorKey, &count) return count } -func (s *Storage) getAndroidSuccess() int64 { +func (s *Storage) GetAndroidSuccess() int64 { var count int64 - s.getBoltDB(gorush.AndroidSuccessKey, &count) + s.getBoltDB(AndroidSuccessKey, &count) return count } -func (s *Storage) getAndroidError() int64 { +func (s *Storage) GetAndroidError() int64 { var count int64 - s.getBoltDB(gorush.AndroidErrorKey, &count) + s.getBoltDB(AndroidErrorKey, &count) return count } diff --git a/storage/boltdb/boltdb_test.go b/storage/boltdb/boltdb_test.go index 61b28cb..b219d8a 100644 --- a/storage/boltdb/boltdb_test.go +++ b/storage/boltdb/boltdb_test.go @@ -1,7 +1,7 @@ package boltdb import ( - "github.com/appleboy/gorush/gorush" + c "github.com/appleboy/gorush/gorush/config" "github.com/stretchr/testify/assert" "testing" ) @@ -9,32 +9,32 @@ import ( func TestRedisEngine(t *testing.T) { var val int64 - config := gorush.BuildDefaultPushConf() + config := c.BuildDefaultPushConf() - boltDB := New(config, gorush.StatusApp{}) - boltDB.initBoltDB() - boltDB.resetBoltDB() + boltDB := New(config) + boltDB.Init() + boltDB.Reset() - boltDB.addTotalCount(10) - val = boltDB.getTotalCount() + boltDB.AddTotalCount(10) + val = boltDB.GetTotalCount() assert.Equal(t, int64(10), val) - boltDB.addTotalCount(10) - val = boltDB.getTotalCount() + boltDB.AddTotalCount(10) + val = boltDB.GetTotalCount() assert.Equal(t, int64(20), val) - boltDB.addIosSuccess(20) - val = boltDB.getIosSuccess() + boltDB.AddIosSuccess(20) + val = boltDB.GetIosSuccess() assert.Equal(t, int64(20), val) - boltDB.addIosError(30) - val = boltDB.getIosError() + boltDB.AddIosError(30) + val = boltDB.GetIosError() assert.Equal(t, int64(30), val) - boltDB.addAndroidSuccess(40) - val = boltDB.getAndroidSuccess() + boltDB.AddAndroidSuccess(40) + val = boltDB.GetAndroidSuccess() assert.Equal(t, int64(40), val) - boltDB.addAndroidError(50) - val = boltDB.getAndroidError() + boltDB.AddAndroidError(50) + val = boltDB.GetAndroidError() assert.Equal(t, int64(50), val) } diff --git a/storage/memory/memory.go b/storage/memory/memory.go index d9cdec0..edb58d2 100644 --- a/storage/memory/memory.go +++ b/storage/memory/memory.go @@ -1,66 +1,91 @@ package memory import ( - "github.com/appleboy/gorush/gorush" "sync/atomic" ) +// StatusApp is app status structure +type statApp struct { + 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"` +} + // Storage implements the storage interface for gorush (https://github.com/appleboy/gorush) -func New(stat gorush.StatusApp) *Storage { +func New() *Storage { return &Storage{ - stat: stat, + stat: &statApp{}, } } type Storage struct { - stat gorush.StatusApp + stat *statApp } -func (s *Storage) addTotalCount(count int64) { +func (s *Storage) Init() error { + return nil +} + +func (s *Storage) Reset() { +} + +func (s *Storage) AddTotalCount(count int64) { atomic.AddInt64(&s.stat.TotalCount, count) } -func (s *Storage) addIosSuccess(count int64) { +func (s *Storage) AddIosSuccess(count int64) { atomic.AddInt64(&s.stat.Ios.PushSuccess, count) } -func (s *Storage) addIosError(count int64) { +func (s *Storage) AddIosError(count int64) { atomic.AddInt64(&s.stat.Ios.PushError, count) } -func (s *Storage) addAndroidSuccess(count int64) { +func (s *Storage) AddAndroidSuccess(count int64) { atomic.AddInt64(&s.stat.Android.PushSuccess, count) } -func (s *Storage) addAndroidError(count int64) { +func (s *Storage) AddAndroidError(count int64) { atomic.AddInt64(&s.stat.Android.PushError, count) } -func (s *Storage) getTotalCount() int64 { +func (s *Storage) GetTotalCount() int64 { count := atomic.LoadInt64(&s.stat.TotalCount) return count } -func (s *Storage) getIosSuccess() int64 { +func (s *Storage) GetIosSuccess() int64 { count := atomic.LoadInt64(&s.stat.Ios.PushSuccess) return count } -func (s *Storage) getIosError() int64 { +func (s *Storage) GetIosError() int64 { count := atomic.LoadInt64(&s.stat.Ios.PushError) return count } -func (s *Storage) getAndroidSuccess() int64 { +func (s *Storage) GetAndroidSuccess() int64 { count := atomic.LoadInt64(&s.stat.Android.PushSuccess) return count } -func (s *Storage) getAndroidError() int64 { +func (s *Storage) GetAndroidError() int64 { count := atomic.LoadInt64(&s.stat.Android.PushError) return count diff --git a/storage/memory/memory_test.go b/storage/memory/memory_test.go index 54d670c..491dfd7 100644 --- a/storage/memory/memory_test.go +++ b/storage/memory/memory_test.go @@ -1,7 +1,6 @@ package memory import ( - "github.com/appleboy/gorush/gorush" "github.com/stretchr/testify/assert" "testing" ) @@ -9,25 +8,25 @@ import ( func TestMemoryEngine(t *testing.T) { var val int64 - memory := New(gorush.StatusApp{}) + memory := New() - memory.addTotalCount(1) - val = memory.getTotalCount() + memory.AddTotalCount(1) + val = memory.GetTotalCount() assert.Equal(t, int64(1), val) - memory.addIosSuccess(2) - val = memory.getIosSuccess() + memory.AddIosSuccess(2) + val = memory.GetIosSuccess() assert.Equal(t, int64(2), val) - memory.addIosError(3) - val = memory.getIosError() + memory.AddIosError(3) + val = memory.GetIosError() assert.Equal(t, int64(3), val) - memory.addAndroidSuccess(4) - val = memory.getAndroidSuccess() + memory.AddAndroidSuccess(4) + val = memory.GetAndroidSuccess() assert.Equal(t, int64(4), val) - memory.addAndroidError(5) - val = memory.getAndroidError() + memory.AddAndroidError(5) + val = memory.GetAndroidError() assert.Equal(t, int64(5), val) } diff --git a/storage/redis/redis.go b/storage/redis/redis.go index 01d3c61..cebe44f 100644 --- a/storage/redis/redis.go +++ b/storage/redis/redis.go @@ -1,33 +1,40 @@ package redis import ( - "github.com/appleboy/gorush/gorush" + "github.com/appleboy/gorush/gorush/config" "gopkg.in/redis.v3" "log" "strconv" ) +// Stat variable for redis +const ( + TotalCountKey = "gorush-total-count" + IosSuccessKey = "gorush-ios-success-count" + IosErrorKey = "gorush-ios-error-count" + AndroidSuccessKey = "gorush-android-success-count" + AndroidErrorKey = "gorush-android-error-count" +) + var RedisClient *redis.Client // Storage implements the storage interface for gorush (https://github.com/appleboy/gorush) -func New(config gorush.ConfYaml, stat gorush.StatusApp) *Storage { +func New(config config.ConfYaml) *Storage { return &Storage{ - stat: stat, config: config, } } -func getRedisInt64Result(key string, count *int64) { +func getInt64(key string, count *int64) { val, _ := RedisClient.Get(key).Result() *count, _ = strconv.ParseInt(val, 10, 64) } type Storage struct { - config gorush.ConfYaml - stat gorush.StatusApp + config config.ConfYaml } -func (s *Storage) initRedis() error { +func (s *Storage) Init() error { RedisClient = redis.NewClient(&redis.Options{ Addr: s.config.Stat.Redis.Addr, Password: s.config.Stat.Redis.Password, @@ -43,79 +50,73 @@ func (s *Storage) initRedis() error { return err } - s.stat.TotalCount = s.getTotalCount() - s.stat.Ios.PushSuccess = s.getIosSuccess() - s.stat.Ios.PushError = s.getIosError() - s.stat.Android.PushSuccess = s.getAndroidSuccess() - s.stat.Android.PushError = s.getAndroidError() - return nil } -func (s *Storage) resetRedis() { - RedisClient.Set(gorush.TotalCountKey, strconv.Itoa(0), 0) - RedisClient.Set(gorush.IosSuccessKey, strconv.Itoa(0), 0) - RedisClient.Set(gorush.IosErrorKey, strconv.Itoa(0), 0) - RedisClient.Set(gorush.AndroidSuccessKey, strconv.Itoa(0), 0) - RedisClient.Set(gorush.AndroidErrorKey, strconv.Itoa(0), 0) +func (s *Storage) Reset() { + RedisClient.Set(TotalCountKey, strconv.Itoa(0), 0) + RedisClient.Set(IosSuccessKey, strconv.Itoa(0), 0) + RedisClient.Set(IosErrorKey, strconv.Itoa(0), 0) + RedisClient.Set(AndroidSuccessKey, strconv.Itoa(0), 0) + RedisClient.Set(AndroidErrorKey, strconv.Itoa(0), 0) } -func (s *Storage) addTotalCount(count int64) { - total := s.getTotalCount() + count - RedisClient.Set(gorush.TotalCountKey, strconv.Itoa(int(total)), 0) +func (s *Storage) AddTotalCount(count int64) { + total := s.GetTotalCount() + count + RedisClient.Set(TotalCountKey, strconv.Itoa(int(total)), 0) } -func (s *Storage) addIosSuccess(count int64) { - total := s.getIosSuccess() + count - RedisClient.Set(gorush.IosSuccessKey, strconv.Itoa(int(total)), 0) +func (s *Storage) AddIosSuccess(count int64) { + total := s.GetIosSuccess() + count + RedisClient.Set(IosSuccessKey, strconv.Itoa(int(total)), 0) } -func (s *Storage) addIosError(count int64) { - total := s.getIosError() + count - RedisClient.Set(gorush.IosErrorKey, strconv.Itoa(int(total)), 0) +func (s *Storage) AddIosError(count int64) { + total := s.GetIosError() + count + RedisClient.Set(IosErrorKey, strconv.Itoa(int(total)), 0) } -func (s *Storage) addAndroidSuccess(count int64) { - total := s.getAndroidSuccess() + count - RedisClient.Set(gorush.AndroidSuccessKey, strconv.Itoa(int(total)), 0) +func (s *Storage) AddAndroidSuccess(count int64) { + total := s.GetAndroidSuccess() + count + RedisClient.Set(AndroidSuccessKey, strconv.Itoa(int(total)), 0) } -func (s *Storage) addAndroidError(count int64) { - total := s.getAndroidError() + count - RedisClient.Set(gorush.AndroidErrorKey, strconv.Itoa(int(total)), 0) +func (s *Storage) AddAndroidError(count int64) { + total := s.GetAndroidError() + count + RedisClient.Set(AndroidErrorKey, strconv.Itoa(int(total)), 0) } -func (s *Storage) getTotalCount() int64 { +func (s *Storage) GetTotalCount() int64 { var count int64 - getRedisInt64Result(gorush.TotalCountKey, &count) + getInt64(TotalCountKey, &count) return count } -func (s *Storage) getIosSuccess() int64 { +func (s *Storage) GetIosSuccess() int64 { var count int64 - getRedisInt64Result(gorush.IosSuccessKey, &count) + getInt64(IosSuccessKey, &count) return count } -func (s *Storage) getIosError() int64 { +func (s *Storage) GetIosError() int64 { var count int64 - getRedisInt64Result(gorush.IosErrorKey, &count) + getInt64(IosErrorKey, &count) return count } -func (s *Storage) getAndroidSuccess() int64 { +func (s *Storage) GetAndroidSuccess() int64 { var count int64 - getRedisInt64Result(gorush.AndroidSuccessKey, &count) + getInt64(AndroidSuccessKey, &count) return count } -func (s *Storage) getAndroidError() int64 { +func (s *Storage) GetAndroidError() int64 { var count int64 - getRedisInt64Result(gorush.AndroidErrorKey, &count) + getInt64(AndroidErrorKey, &count) return count } diff --git a/storage/redis/redis_test.go b/storage/redis/redis_test.go index 83abe66..2851708 100644 --- a/storage/redis/redis_test.go +++ b/storage/redis/redis_test.go @@ -1,17 +1,17 @@ package redis import ( - "github.com/appleboy/gorush/gorush" + c "github.com/appleboy/gorush/gorush/config" "github.com/stretchr/testify/assert" "testing" ) func TestRedisServerError(t *testing.T) { - config := gorush.BuildDefaultPushConf() + config := c.BuildDefaultPushConf() config.Stat.Redis.Addr = "localhost:6370" - redis := New(config, gorush.StatusApp{}) - err := redis.initRedis() + redis := New(config) + err := redis.Init() assert.Error(t, err) } @@ -19,32 +19,32 @@ func TestRedisServerError(t *testing.T) { func TestRedisEngine(t *testing.T) { var val int64 - config := gorush.BuildDefaultPushConf() + config := c.BuildDefaultPushConf() - redis := New(config, gorush.StatusApp{}) - redis.initRedis() - redis.resetRedis() + redis := New(config) + redis.Init() + redis.Reset() - redis.addTotalCount(10) - val = redis.getTotalCount() + redis.AddTotalCount(10) + val = redis.GetTotalCount() assert.Equal(t, int64(10), val) - redis.addTotalCount(10) - val = redis.getTotalCount() + redis.AddTotalCount(10) + val = redis.GetTotalCount() assert.Equal(t, int64(20), val) - redis.addIosSuccess(20) - val = redis.getIosSuccess() + redis.AddIosSuccess(20) + val = redis.GetIosSuccess() assert.Equal(t, int64(20), val) - redis.addIosError(30) - val = redis.getIosError() + redis.AddIosError(30) + val = redis.GetIosError() assert.Equal(t, int64(30), val) - redis.addAndroidSuccess(40) - val = redis.getAndroidSuccess() + redis.AddAndroidSuccess(40) + val = redis.GetAndroidSuccess() assert.Equal(t, int64(40), val) - redis.addAndroidError(50) - val = redis.getAndroidError() + redis.AddAndroidError(50) + val = redis.GetAndroidError() assert.Equal(t, int64(50), val) }