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

147
config/config/config.go Normal file
View File

@@ -0,0 +1,147 @@
package config
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

@@ -0,0 +1,45 @@
package config
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)
}