2016-05-02 11:42:21 +00:00
|
|
|
package config
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2016-04-14 12:34:23 +00:00
|
|
|
"runtime"
|
2016-10-03 05:42:29 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2016-03-26 05:59:56 +00:00
|
|
|
)
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// ConfYaml is config structure.
|
2016-03-26 05:59:56 +00:00
|
|
|
type ConfYaml struct {
|
|
|
|
Core SectionCore `yaml:"core"`
|
2016-04-13 06:59:28 +00:00
|
|
|
API SectionAPI `yaml:"api"`
|
2016-03-26 05:59:56 +00:00
|
|
|
Android SectionAndroid `yaml:"android"`
|
|
|
|
Ios SectionIos `yaml:"ios"`
|
2016-04-05 05:15:47 +00:00
|
|
|
Log SectionLog `yaml:"log"`
|
2016-06-26 15:39:48 +00:00
|
|
|
Stat SectionStat `yaml:"stat"`
|
2016-03-26 05:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionCore is sub section of config.
|
2016-03-26 05:59:56 +00:00
|
|
|
type SectionCore struct {
|
2016-08-19 13:16:10 +00:00
|
|
|
Port string `yaml:"port"`
|
2016-09-02 07:55:47 +00:00
|
|
|
MaxNotification int64 `yaml:"max_notification"`
|
|
|
|
WorkerNum int64 `yaml:"worker_num"`
|
|
|
|
QueueNum int64 `yaml:"queue_num"`
|
2016-08-19 13:16:10 +00:00
|
|
|
Mode string `yaml:"mode"`
|
|
|
|
SSL bool `yaml:"ssl"`
|
|
|
|
CertPath string `yaml:"cert_path"`
|
|
|
|
KeyPath string `yaml:"key_path"`
|
|
|
|
HTTPProxy string `yaml:"http_proxy"`
|
|
|
|
PID SectionPID `yaml:"pid"`
|
2016-03-26 05:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionAPI is sub section of config.
|
2016-04-13 06:59:28 +00:00
|
|
|
type SectionAPI struct {
|
2016-04-15 07:05:23 +00:00
|
|
|
PushURI string `yaml:"push_uri"`
|
|
|
|
StatGoURI string `yaml:"stat_go_uri"`
|
2016-04-15 02:24:39 +00:00
|
|
|
StatAppURI string `yaml:"stat_app_uri"`
|
2016-04-15 07:05:23 +00:00
|
|
|
ConfigURI string `yaml:"config_uri"`
|
2016-06-26 04:21:32 +00:00
|
|
|
SysStatURI string `yaml:"sys_stat_uri"`
|
2017-01-19 08:56:30 +00:00
|
|
|
MetricURI string `yaml:"metric_uri"`
|
2016-03-26 05:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionAndroid is sub section of config.
|
2016-03-26 05:59:56 +00:00
|
|
|
type SectionAndroid struct {
|
2016-10-23 08:22:49 +00:00
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
APIKey string `yaml:"apikey"`
|
|
|
|
MaxRetry int `yaml:"max_retry"`
|
2016-03-26 05:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionIos is sub section of config.
|
2016-03-26 05:59:56 +00:00
|
|
|
type SectionIos struct {
|
2016-05-27 06:53:54 +00:00
|
|
|
Enabled bool `yaml:"enabled"`
|
2016-06-12 11:09:41 +00:00
|
|
|
KeyPath string `yaml:"key_path"`
|
2016-05-30 07:53:16 +00:00
|
|
|
Password string `yaml:"password"`
|
2016-05-27 06:53:54 +00:00
|
|
|
Production bool `yaml:"production"`
|
2016-10-23 08:22:49 +00:00
|
|
|
MaxRetry int `yaml:"max_retry"`
|
2016-03-26 05:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionLog is sub section of config.
|
2016-04-05 05:15:47 +00:00
|
|
|
type SectionLog struct {
|
2016-04-07 06:16:59 +00:00
|
|
|
Format string `yaml:"format"`
|
2016-04-05 05:15:47 +00:00
|
|
|
AccessLog string `yaml:"access_log"`
|
|
|
|
AccessLevel string `yaml:"access_level"`
|
|
|
|
ErrorLog string `yaml:"error_log"`
|
|
|
|
ErrorLevel string `yaml:"error_level"`
|
2016-05-16 13:04:05 +00:00
|
|
|
HideToken bool `yaml:"hide_token"`
|
2016-04-05 05:15:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionStat is sub section of config.
|
2016-04-22 07:47:14 +00:00
|
|
|
type SectionStat struct {
|
2016-09-19 08:19:20 +00:00
|
|
|
Engine string `yaml:"engine"`
|
|
|
|
Redis SectionRedis `yaml:"redis"`
|
|
|
|
BoltDB SectionBoltDB `yaml:"boltdb"`
|
|
|
|
BuntDB SectionBuntDB `yaml:"buntdb"`
|
|
|
|
LevelDB SectionLevelDB `yaml:"leveldb"`
|
2016-04-22 07:47:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionRedis is sub section of config.
|
2016-04-22 07:47:14 +00:00
|
|
|
type SectionRedis struct {
|
|
|
|
Addr string `yaml:"addr"`
|
|
|
|
Password string `yaml:"password"`
|
2016-09-19 09:00:14 +00:00
|
|
|
DB int `yaml:"db"`
|
2016-04-22 07:47:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionBoltDB is sub section of config.
|
2016-04-23 07:13:57 +00:00
|
|
|
type SectionBoltDB struct {
|
2016-04-23 07:20:42 +00:00
|
|
|
Path string `yaml:"path"`
|
2016-04-23 07:13:57 +00:00
|
|
|
Bucket string `yaml:"bucket"`
|
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionBuntDB is sub section of config.
|
2016-08-02 07:35:28 +00:00
|
|
|
type SectionBuntDB struct {
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionLevelDB is sub section of config.
|
2016-09-19 08:19:20 +00:00
|
|
|
type SectionLevelDB struct {
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
}
|
|
|
|
|
2016-10-02 14:09:24 +00:00
|
|
|
// SectionPID is sub section of config.
|
2016-08-19 13:16:10 +00:00
|
|
|
type SectionPID struct {
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
Override bool `yaml:"override"`
|
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// BuildDefaultPushConf is default config setting.
|
2016-03-26 05:59:56 +00:00
|
|
|
func BuildDefaultPushConf() ConfYaml {
|
|
|
|
var conf ConfYaml
|
|
|
|
|
|
|
|
// Core
|
|
|
|
conf.Core.Port = "8088"
|
2016-09-02 07:55:47 +00:00
|
|
|
conf.Core.WorkerNum = int64(runtime.NumCPU())
|
|
|
|
conf.Core.QueueNum = int64(8192)
|
2016-04-01 01:53:10 +00:00
|
|
|
conf.Core.Mode = "release"
|
2016-03-28 06:40:48 +00:00
|
|
|
conf.Core.SSL = false
|
|
|
|
conf.Core.CertPath = "cert.pem"
|
|
|
|
conf.Core.KeyPath = "key.pem"
|
2016-09-02 07:55:47 +00:00
|
|
|
conf.Core.MaxNotification = int64(100)
|
2016-07-29 01:38:06 +00:00
|
|
|
conf.Core.HTTPProxy = ""
|
2016-08-19 13:16:10 +00:00
|
|
|
conf.Core.PID.Enabled = false
|
|
|
|
conf.Core.PID.Path = "gorush.pid"
|
|
|
|
conf.Core.PID.Override = false
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
// Api
|
2016-04-13 06:59:28 +00:00
|
|
|
conf.API.PushURI = "/api/push"
|
2016-04-15 02:24:39 +00:00
|
|
|
conf.API.StatGoURI = "/api/stat/go"
|
|
|
|
conf.API.StatAppURI = "/api/stat/app"
|
2016-04-15 07:05:23 +00:00
|
|
|
conf.API.ConfigURI = "/api/config"
|
2016-06-26 04:21:32 +00:00
|
|
|
conf.API.SysStatURI = "/sys/stats"
|
2017-01-19 08:56:30 +00:00
|
|
|
conf.API.MetricURI = "/metrics"
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
// Android
|
2016-03-27 13:07:11 +00:00
|
|
|
conf.Android.Enabled = false
|
2016-04-13 06:59:28 +00:00
|
|
|
conf.Android.APIKey = ""
|
2016-10-23 10:19:51 +00:00
|
|
|
conf.Android.MaxRetry = 0
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
// iOS
|
2016-03-27 13:07:11 +00:00
|
|
|
conf.Ios.Enabled = false
|
2016-06-12 11:09:41 +00:00
|
|
|
conf.Ios.KeyPath = "key.pem"
|
2016-05-30 07:53:16 +00:00
|
|
|
conf.Ios.Password = ""
|
2016-03-26 05:59:56 +00:00
|
|
|
conf.Ios.Production = false
|
2016-10-23 10:19:51 +00:00
|
|
|
conf.Ios.MaxRetry = 0
|
2016-03-26 05:59:56 +00:00
|
|
|
|
2016-04-05 05:15:47 +00:00
|
|
|
// log
|
2016-04-07 06:16:59 +00:00
|
|
|
conf.Log.Format = "string"
|
2016-04-05 05:15:47 +00:00
|
|
|
conf.Log.AccessLog = "stdout"
|
|
|
|
conf.Log.AccessLevel = "debug"
|
|
|
|
conf.Log.ErrorLog = "stderr"
|
|
|
|
conf.Log.ErrorLevel = "error"
|
2016-05-16 13:04:05 +00:00
|
|
|
conf.Log.HideToken = true
|
2016-04-05 05:15:47 +00:00
|
|
|
|
2016-04-22 07:47:14 +00:00
|
|
|
conf.Stat.Engine = "memory"
|
|
|
|
conf.Stat.Redis.Addr = "localhost:6379"
|
|
|
|
conf.Stat.Redis.Password = ""
|
2016-04-22 09:54:00 +00:00
|
|
|
conf.Stat.Redis.DB = 0
|
2016-04-22 07:47:14 +00:00
|
|
|
|
2016-09-19 13:52:40 +00:00
|
|
|
conf.Stat.BoltDB.Path = "bolt.db"
|
2016-04-23 07:13:57 +00:00
|
|
|
conf.Stat.BoltDB.Bucket = "gorush"
|
|
|
|
|
2016-09-19 13:52:40 +00:00
|
|
|
conf.Stat.BuntDB.Path = "bunt.db"
|
|
|
|
conf.Stat.LevelDB.Path = "level.db"
|
2016-08-02 07:35:28 +00:00
|
|
|
|
2016-03-26 05:59:56 +00:00
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// LoadConfYaml provide load yml config.
|
2016-03-26 05:59:56 +00:00
|
|
|
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)
|
2016-03-28 01:12:57 +00:00
|
|
|
|
2016-03-26 05:59:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
2017-01-25 08:29:35 +00:00
|
|
|
if config.Core.WorkerNum == int64(0) {
|
|
|
|
config.Core.WorkerNum = int64(runtime.NumCPU())
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Core.QueueNum == int64(0) {
|
|
|
|
config.Core.QueueNum = int64(8192)
|
|
|
|
}
|
|
|
|
|
2016-03-26 05:59:56 +00:00
|
|
|
return config, nil
|
|
|
|
}
|