integrate redis engine.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2016-05-02 19:42:21 +08:00
parent 8df5c63860
commit 03ab8eeac7
12 changed files with 131 additions and 112 deletions

View File

@@ -1,147 +0,0 @@
package gorush
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"runtime"
)
// ConfYaml is config structure.
type ConfYaml struct {
Core SectionCore `yaml:"core"`
API SectionAPI `yaml:"api"`
Android SectionAndroid `yaml:"android"`
Ios SectionIos `yaml:"ios"`
Log SectionLog `yaml:"log"`
Stat SectionStat `yaml:stat`
}
// SectionCore is sub seciont of config.
type SectionCore struct {
Port string `yaml:"port"`
MaxNotification int `yaml:"max_notification"`
WorkerNum int `yaml:"worker_num"`
QueueNum int `yaml:"queue_num"`
Mode string `yaml:"mode"`
SSL bool `yaml:"ssl"`
CertPath string `yaml:"cert_path"`
KeyPath string `yaml:"key_path"`
}
// SectionAPI is sub seciont of config.
type SectionAPI struct {
PushURI string `yaml:"push_uri"`
StatGoURI string `yaml:"stat_go_uri"`
StatAppURI string `yaml:"stat_app_uri"`
ConfigURI string `yaml:"config_uri"`
}
// SectionAndroid is sub seciont of config.
type SectionAndroid struct {
Enabled bool `yaml:"enabled"`
APIKey string `yaml:"apikey"`
}
// SectionIos is sub seciont of config.
type SectionIos struct {
Enabled bool `yaml:"enabled"`
PemCertPath string `yaml:"pem_cert_path"`
PemKeyPath string `yaml:"pem_key_path"`
Production bool `yaml:"production"`
}
// SectionLog is sub seciont of config.
type SectionLog struct {
Format string `yaml:"format"`
AccessLog string `yaml:"access_log"`
AccessLevel string `yaml:"access_level"`
ErrorLog string `yaml:"error_log"`
ErrorLevel string `yaml:"error_level"`
}
// SectionStat is sub seciont of config.
type SectionStat struct {
Engine string `yaml:"engine"`
Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"`
}
// SectionRedis is sub seciont of config.
type SectionRedis struct {
Addr string `yaml:"addr"`
Password string `yaml:"password"`
DB int64 `yaml:"db"`
}
// SectionBoltDB is sub seciont of config.
type SectionBoltDB struct {
Path string `yaml:"path"`
Bucket string `yaml:"bucket"`
}
// BuildDefaultPushConf is default config setting.
func BuildDefaultPushConf() ConfYaml {
var conf ConfYaml
// Core
conf.Core.Port = "8088"
conf.Core.WorkerNum = runtime.NumCPU()
conf.Core.QueueNum = 8192
conf.Core.Mode = "release"
conf.Core.SSL = false
conf.Core.CertPath = "cert.pem"
conf.Core.KeyPath = "key.pem"
conf.Core.MaxNotification = 100
// Api
conf.API.PushURI = "/api/push"
conf.API.StatGoURI = "/api/stat/go"
conf.API.StatAppURI = "/api/stat/app"
conf.API.ConfigURI = "/api/config"
// Android
conf.Android.Enabled = false
conf.Android.APIKey = ""
// iOS
conf.Ios.Enabled = false
conf.Ios.PemCertPath = "cert.pem"
conf.Ios.PemKeyPath = "key.pem"
conf.Ios.Production = false
// log
conf.Log.Format = "string"
conf.Log.AccessLog = "stdout"
conf.Log.AccessLevel = "debug"
conf.Log.ErrorLog = "stderr"
conf.Log.ErrorLevel = "error"
conf.Stat.Engine = "memory"
conf.Stat.Redis.Addr = "localhost:6379"
conf.Stat.Redis.Password = ""
conf.Stat.Redis.DB = 0
conf.Stat.BoltDB.Path = "gorush.db"
conf.Stat.BoltDB.Bucket = "gorush"
return conf
}
// LoadConfYaml provide load yml config.
func LoadConfYaml(confPath string) (ConfYaml, error) {
var config ConfYaml
configFile, err := ioutil.ReadFile(confPath)
if err != nil {
return config, err
}
err = yaml.Unmarshal([]byte(configFile), &config)
if err != nil {
return config, err
}
return config, nil
}

View File

@@ -1,45 +0,0 @@
package gorush
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"os"
"testing"
)
// Test file is missing
func TestMissingFile(t *testing.T) {
filename := "test"
_, err := LoadConfYaml(filename)
assert.NotNil(t, err)
}
// Test wrong json format
func TestWrongYAMLormat(t *testing.T) {
content := []byte(`Wrong format`)
filename := "tempfile"
if err := ioutil.WriteFile(filename, content, 0644); err != nil {
log.Fatalf("WriteFile %s: %v", filename, err)
}
// clean up
defer os.Remove(filename)
// parse JSON format error
_, err := LoadConfYaml(filename)
assert.NotNil(t, err)
}
// Test config file.
func TestReadConfig(t *testing.T) {
config, err := LoadConfYaml("../config/config.yml")
assert.Nil(t, err)
assert.Equal(t, "8088", config.Core.Port)
assert.True(t, config.Android.Enabled)
}

View File

@@ -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

View File

@@ -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"

View File

@@ -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,7 +218,7 @@ 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"
@@ -235,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"
@@ -251,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")
@@ -267,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")
@@ -287,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")
@@ -305,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")
@@ -325,7 +326,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
}
func TestSenMultipleNotifications(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
InitWorkers(2, 2)
@@ -360,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"
@@ -393,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"
@@ -426,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"
@@ -436,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"
@@ -446,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
@@ -521,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")

View File

@@ -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"
}

View File

@@ -2,6 +2,7 @@ package gorush
import (
"github.com/appleboy/gorush/gorush/storage/memory"
"github.com/appleboy/gorush/gorush/storage/redis"
"github.com/gin-gonic/gin"
"net/http"
)
@@ -32,8 +33,9 @@ func InitAppStatus() {
switch PushConf.Stat.Engine {
case "memory":
StatStorage = memory.New()
// case "redis":
// initRedis()
case "redis":
StatStorage = redis.New(PushConf)
StatStorage.Init()
// case "boltdb":
// initBoltDB()
default:

View File

@@ -90,29 +90,32 @@ func TestStatForMemoryEngine(t *testing.T) {
// assert.Error(t, err)
// }
// func TestStatForRedisEngine(t *testing.T) {
// var val int64
// PushConf.Stat.Engine = "redis"
// PushConf.Stat.Redis.Addr = "localhost:6379"
// InitAppStatus()
func TestStatForRedisEngine(t *testing.T) {
var val int64
PushConf.Stat.Engine = "redis"
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) {
// var val int64

View File

@@ -2,6 +2,8 @@ package gorush
// Storage interface
type Storage interface {
Init() error
Reset()
AddTotalCount(int64)
AddIosSuccess(int64)
AddIosError(int64)