2016-05-02 11:42:21 +00:00
|
|
|
package config
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"io/ioutil"
|
2016-04-14 12:34:23 +00:00
|
|
|
"runtime"
|
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-04-13 06:59:28 +00:00
|
|
|
// SectionCore is sub seciont 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-04-13 06:59:28 +00:00
|
|
|
// SectionAPI is sub seciont of config.
|
|
|
|
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"`
|
2016-03-26 05:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// SectionAndroid is sub seciont of config.
|
2016-03-26 05:59:56 +00:00
|
|
|
type SectionAndroid struct {
|
2016-03-26 16:03:17 +00:00
|
|
|
Enabled bool `yaml:"enabled"`
|
2016-04-13 06:59:28 +00:00
|
|
|
APIKey string `yaml:"apikey"`
|
2016-03-26 05:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// SectionIos is sub seciont 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-03-26 05:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// SectionLog is sub seciont 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-04-22 07:47:14 +00:00
|
|
|
// SectionStat is sub seciont of config.
|
|
|
|
type SectionStat struct {
|
2016-04-23 07:46:17 +00:00
|
|
|
Engine string `yaml:"engine"`
|
2016-04-23 07:20:42 +00:00
|
|
|
Redis SectionRedis `yaml:"redis"`
|
2016-04-23 07:13:57 +00:00
|
|
|
BoltDB SectionBoltDB `yaml:"boltdb"`
|
2016-08-02 07:35:28 +00:00
|
|
|
BuntDB SectionBuntDB `yaml:"buntdb"`
|
2016-04-22 07:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SectionRedis is sub seciont of config.
|
|
|
|
type SectionRedis struct {
|
|
|
|
Addr string `yaml:"addr"`
|
|
|
|
Password string `yaml:"password"`
|
2016-04-22 09:54:00 +00:00
|
|
|
DB int64 `yaml:"db"`
|
2016-04-22 07:47:14 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 07:13:57 +00:00
|
|
|
// SectionBoltDB is sub seciont of config.
|
|
|
|
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-08-02 07:35:28 +00:00
|
|
|
// SectionBuntDB is sub seciont of config.
|
|
|
|
type SectionBuntDB struct {
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
}
|
|
|
|
|
2016-08-19 13:16:10 +00:00
|
|
|
// SectionPID is sub seciont of config.
|
|
|
|
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"
|
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-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-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-04-23 07:13:57 +00:00
|
|
|
conf.Stat.BoltDB.Path = "gorush.db"
|
|
|
|
conf.Stat.BoltDB.Bucket = "gorush"
|
|
|
|
|
2016-08-02 07:35:28 +00:00
|
|
|
conf.Stat.BuntDB.Path = "gorush.db"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|