Skip to content

Commit

Permalink
Merge pull request #76 from appleboy/storage
Browse files Browse the repository at this point in the history
Fixed #75, #73 Rewrite boltdb, memory and redis engine.
  • Loading branch information
appleboy committed May 2, 2016
2 parents 8dfa69a + 1b041fb commit ec33827
Show file tree
Hide file tree
Showing 17 changed files with 353 additions and 482 deletions.
5 changes: 3 additions & 2 deletions gorush.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/gorush/config"
"log"
)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gorush/config.go → gorush/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gorush
package config

import (
"gopkg.in/yaml.v2"
Expand Down
2 changes: 1 addition & 1 deletion gorush/config_test.go → gorush/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gorush
package config

import (
"github.com/stretchr/testify/assert"
Expand Down
7 changes: 4 additions & 3 deletions gorush/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
11 changes: 6 additions & 5 deletions gorush/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gorush

import (
"github.com/Sirupsen/logrus"
"github.com/appleboy/gorush/gorush/config"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down Expand Up @@ -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())
Expand All @@ -45,31 +46,31 @@ func TestInitDefaultLog(t *testing.T) {
}

func TestAccessLevel(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()

PushConf.Log.AccessLevel = "invalid"

assert.NotNil(t, InitLog())
}

func TestErrorLevel(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()

PushConf.Log.ErrorLevel = "invalid"

assert.NotNil(t, InitLog())
}

func TestAccessLogPath(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()

PushConf.Log.AccessLog = "logs/access.log"

assert.NotNil(t, InitLog())
}

func TestErrorLogPath(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()

PushConf.Log.ErrorLog = "logs/error.log"

Expand Down
12 changes: 6 additions & 6 deletions gorush/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func queueNotification(req RequestPush) int {
count += len(notification.Tokens)
}

addTotalCount(int64(count))
StatStorage.AddTotalCount(int64(count))

return count
}
Expand Down Expand Up @@ -315,21 +315,21 @@ func PushToIOS(req PushNotification) bool {
// apns server error
LogPush(FailedPush, token, req, err)
isError = true
addIosError(1)
StatStorage.AddIosError(1)
continue
}

if res.StatusCode != 200 {
// 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)
}
}

Expand Down Expand Up @@ -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 != "" {
Expand Down
36 changes: 19 additions & 17 deletions gorush/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -13,7 +14,7 @@ import (
)

func TestDisabledAndroidIosConf(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()

err := CheckPushConf()

Expand All @@ -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 = ""
Expand All @@ -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 = ""
Expand All @@ -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"
Expand Down Expand Up @@ -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"},
Expand All @@ -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"
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -324,7 +326,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
}

func TestSenMultipleNotifications(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()

InitWorkers(2, 2)

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion gorush/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,7 +16,7 @@ import (
var goVersion = runtime.Version()

func initTest() {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Core.Mode = "test"
}

Expand Down
Loading

0 comments on commit ec33827

Please sign in to comment.