diff --git a/storage/redis/redis.go b/storage/redis/redis.go new file mode 100644 index 0000000..5deb3ac --- /dev/null +++ b/storage/redis/redis.go @@ -0,0 +1,116 @@ +package redis + +import ( + "github.com/appleboy/gorush/gorush" + "gopkg.in/redis.v3" + "log" + "strconv" +) + +const ( + gorushTotalCount = "gorush-total-count" + gorushIosSuccess = "gorush-ios-success-count" + gorushIosError = "gorush-ios-error-count" + gorushAndroidSuccess = "gorush-android-success-count" + gorushAndroidError = "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 { + return &Storage{ + stat: stat, + config: config, + } +} + +func getRedisInt64Result(key string, count *int64) { + val, _ := RedisClient.Get(key).Result() + *count, _ = strconv.ParseInt(val, 10, 64) +} + +type Storage struct { + config gorush.ConfYaml + stat gorush.StatusApp +} + +func (s *Storage) initRedis() error { + RedisClient = redis.NewClient(&redis.Options{ + Addr: s.config.Stat.Redis.Addr, + Password: s.config.Stat.Redis.Password, + DB: s.config.Stat.Redis.DB, + }) + + _, err := RedisClient.Ping().Result() + + if err != nil { + // redis server error + log.Println("Can't connect redis server: " + err.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) addTotalCount(count int64) { + RedisClient.Set(gorushTotalCount, strconv.Itoa(int(count)), 0) +} + +func (s *Storage) addIosSuccess(count int64) { + RedisClient.Set(gorushIosSuccess, strconv.Itoa(int(count)), 0) +} + +func (s *Storage) addIosError(count int64) { + RedisClient.Set(gorushIosError, strconv.Itoa(int(count)), 0) +} + +func (s *Storage) addAndroidSuccess(count int64) { + RedisClient.Set(gorushAndroidSuccess, strconv.Itoa(int(count)), 0) +} + +func (s *Storage) addAndroidError(count int64) { + RedisClient.Set(gorushAndroidError, strconv.Itoa(int(count)), 0) +} + +func (s *Storage) getTotalCount() int64 { + var count int64 + getRedisInt64Result(gorushTotalCount, &count) + + return count +} + +func (s *Storage) getIosSuccess() int64 { + var count int64 + getRedisInt64Result(gorushIosSuccess, &count) + + return count +} + +func (s *Storage) getIosError() int64 { + var count int64 + getRedisInt64Result(gorushIosError, &count) + + return count +} + +func (s *Storage) getAndroidSuccess() int64 { + var count int64 + getRedisInt64Result(gorushAndroidSuccess, &count) + + return count +} + +func (s *Storage) getAndroidError() int64 { + var count int64 + getRedisInt64Result(gorushAndroidError, &count) + + return count +} diff --git a/storage/redis/redis_test.go b/storage/redis/redis_test.go new file mode 100644 index 0000000..1cb7e32 --- /dev/null +++ b/storage/redis/redis_test.go @@ -0,0 +1,46 @@ +package redis + +import ( + "github.com/appleboy/gorush/gorush" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestRedisServerError(t *testing.T) { + config := gorush.BuildDefaultPushConf() + config.Stat.Redis.Addr = "localhost:6370" + + redis := New(config, gorush.StatusApp{}) + err := redis.initRedis() + + assert.Error(t, err) +} + +func TestRedisEngine(t *testing.T) { + var val int64 + + config := gorush.BuildDefaultPushConf() + + redis := New(config, gorush.StatusApp{}) + redis.initRedis() + + redis.addTotalCount(1) + val = redis.getTotalCount() + assert.Equal(t, int64(1), val) + + redis.addIosSuccess(2) + val = redis.getIosSuccess() + assert.Equal(t, int64(2), val) + + redis.addIosError(3) + val = redis.getIosError() + assert.Equal(t, int64(3), val) + + redis.addAndroidSuccess(4) + val = redis.getAndroidSuccess() + assert.Equal(t, int64(4), val) + + redis.addAndroidError(5) + val = redis.getAndroidError() + assert.Equal(t, int64(5), val) +}