2016-05-02 11:42:21 +00:00
|
|
|
package config
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-23 03:06:30 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-03-26 05:59:56 +00:00
|
|
|
"io/ioutil"
|
2016-04-14 12:34:23 +00:00
|
|
|
"runtime"
|
2017-10-23 03:06:30 +00:00
|
|
|
"strings"
|
2016-10-03 05:42:29 +00:00
|
|
|
|
2017-10-23 03:06:30 +00:00
|
|
|
"github.com/spf13/viper"
|
2016-03-26 05:59:56 +00:00
|
|
|
)
|
|
|
|
|
2017-10-23 03:06:30 +00:00
|
|
|
var defaultConf = []byte(`
|
|
|
|
core:
|
|
|
|
enabled: true # enabale httpd server
|
|
|
|
address: "" # ip address to bind (default: any)
|
2020-02-05 15:23:16 +00:00
|
|
|
shutdown_timeout: 30 # default is 30 second
|
2017-10-23 03:06:30 +00:00
|
|
|
port: "8088" # ignore this port number if auto_tls is enabled (listen 443).
|
|
|
|
worker_num: 0 # default worker number is runtime.NumCPU()
|
|
|
|
queue_num: 0 # default queue number is 8192
|
|
|
|
max_notification: 100
|
|
|
|
sync: false # set true if you need get error message from fail push notification in API response.
|
2019-09-06 07:48:42 +00:00
|
|
|
feedback_hook_url: "" # set webhook url if you need get error message asynchronously from fail push notification in API response.
|
2017-10-23 03:06:30 +00:00
|
|
|
mode: "release"
|
|
|
|
ssl: false
|
|
|
|
cert_path: "cert.pem"
|
|
|
|
key_path: "key.pem"
|
2018-02-18 09:12:51 +00:00
|
|
|
cert_base64: ""
|
|
|
|
key_base64: ""
|
2019-12-07 23:30:24 +00:00
|
|
|
http_proxy: ""
|
2017-10-23 03:06:30 +00:00
|
|
|
pid:
|
|
|
|
enabled: false
|
|
|
|
path: "gorush.pid"
|
|
|
|
override: true
|
|
|
|
auto_tls:
|
|
|
|
enabled: false # Automatically install TLS certificates from Let's Encrypt.
|
|
|
|
folder: ".cache" # folder for storing TLS certificates
|
|
|
|
host: "" # which domains the Let's Encrypt will attempt
|
|
|
|
|
|
|
|
grpc:
|
|
|
|
enabled: false # enabale gRPC server
|
|
|
|
port: 9000
|
|
|
|
|
|
|
|
api:
|
|
|
|
push_uri: "/api/push"
|
|
|
|
stat_go_uri: "/api/stat/go"
|
|
|
|
stat_app_uri: "/api/stat/app"
|
|
|
|
config_uri: "/api/config"
|
|
|
|
sys_stat_uri: "/sys/stats"
|
|
|
|
metric_uri: "/metrics"
|
2017-10-26 02:19:58 +00:00
|
|
|
health_uri: "/healthz"
|
2017-10-23 03:06:30 +00:00
|
|
|
|
|
|
|
android:
|
|
|
|
enabled: true
|
|
|
|
apikey: "YOUR_API_KEY"
|
|
|
|
max_retry: 0 # resend fail notification, default value zero is disabled
|
|
|
|
|
|
|
|
ios:
|
|
|
|
enabled: false
|
2018-06-20 01:34:49 +00:00
|
|
|
key_path: ""
|
2018-02-18 09:12:51 +00:00
|
|
|
key_base64: "" # load iOS key from base64 input
|
|
|
|
key_type: "pem" # could be pem, p12 or p8 type
|
2017-10-23 03:06:30 +00:00
|
|
|
password: "" # certificate password, default as empty string.
|
|
|
|
production: false
|
|
|
|
max_retry: 0 # resend fail notification, default value zero is disabled
|
2017-10-25 08:49:23 +00:00
|
|
|
key_id: "" # KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
|
|
|
|
team_id: "" # TeamID from developer account (View Account -> Membership)
|
2017-10-23 03:06:30 +00:00
|
|
|
|
|
|
|
log:
|
|
|
|
format: "string" # string or json
|
|
|
|
access_log: "stdout" # stdout: output to console, or define log path like "log/access_log"
|
|
|
|
access_level: "debug"
|
|
|
|
error_log: "stderr" # stderr: output to console, or define log path like "log/error_log"
|
|
|
|
error_level: "error"
|
|
|
|
hide_token: true
|
|
|
|
|
|
|
|
stat:
|
|
|
|
engine: "memory" # support memory, redis, boltdb, buntdb or leveldb
|
|
|
|
redis:
|
|
|
|
addr: "localhost:6379"
|
|
|
|
password: ""
|
|
|
|
db: 0
|
|
|
|
boltdb:
|
|
|
|
path: "bolt.db"
|
|
|
|
bucket: "gorush"
|
|
|
|
buntdb:
|
|
|
|
path: "bunt.db"
|
|
|
|
leveldb:
|
|
|
|
path: "level.db"
|
|
|
|
`)
|
|
|
|
|
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"`
|
2017-07-24 10:58:30 +00:00
|
|
|
GRPC SectionGRPC `yaml:"grpc"`
|
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 {
|
2017-07-27 03:19:36 +00:00
|
|
|
Enabled bool `yaml:"enabled"`
|
2017-07-31 08:31:07 +00:00
|
|
|
Address string `yaml:"address"`
|
2020-02-05 15:23:16 +00:00
|
|
|
ShutdownTimeout int64 `yaml:"shutdown_timeout"`
|
2017-04-05 04:03:43 +00:00
|
|
|
Port string `yaml:"port"`
|
|
|
|
MaxNotification int64 `yaml:"max_notification"`
|
|
|
|
WorkerNum int64 `yaml:"worker_num"`
|
|
|
|
QueueNum int64 `yaml:"queue_num"`
|
|
|
|
Mode string `yaml:"mode"`
|
2017-04-06 07:00:49 +00:00
|
|
|
Sync bool `yaml:"sync"`
|
2017-04-05 04:03:43 +00:00
|
|
|
SSL bool `yaml:"ssl"`
|
|
|
|
CertPath string `yaml:"cert_path"`
|
|
|
|
KeyPath string `yaml:"key_path"`
|
2018-02-18 09:12:51 +00:00
|
|
|
CertBase64 string `yaml:"cert_base64"`
|
|
|
|
KeyBase64 string `yaml:"key_base64"`
|
2017-04-05 04:03:43 +00:00
|
|
|
HTTPProxy string `yaml:"http_proxy"`
|
2019-09-06 07:48:42 +00:00
|
|
|
FeedbackURL string `yaml:"feedback_hook_url"`
|
2017-04-05 04:03:43 +00:00
|
|
|
PID SectionPID `yaml:"pid"`
|
|
|
|
AutoTLS SectionAutoTLS `yaml:"auto_tls"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SectionAutoTLS support Let's Encrypt setting.
|
|
|
|
type SectionAutoTLS struct {
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
Folder string `yaml:"folder"`
|
|
|
|
Host string `yaml:"host"`
|
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"`
|
2017-10-26 02:19:58 +00:00
|
|
|
HealthURI string `yaml:"health_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"`
|
2018-02-18 09:12:51 +00:00
|
|
|
KeyBase64 string `yaml:"key_base64"`
|
|
|
|
KeyType string `yaml:"key_type"`
|
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"`
|
2017-10-25 08:49:23 +00:00
|
|
|
KeyID string `yaml:"key_id"`
|
|
|
|
TeamID string `yaml:"team_id"`
|
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"`
|
|
|
|
}
|
|
|
|
|
2017-07-24 10:58:30 +00:00
|
|
|
// SectionGRPC is sub section of config.
|
|
|
|
type SectionGRPC struct {
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
Port string `yaml:"port"`
|
|
|
|
}
|
|
|
|
|
2017-10-23 03:06:30 +00:00
|
|
|
// LoadConf load config from file and read in environment variables that match
|
|
|
|
func LoadConf(confPath string) (ConfYaml, error) {
|
2016-03-26 05:59:56 +00:00
|
|
|
var conf ConfYaml
|
|
|
|
|
2017-10-23 03:06:30 +00:00
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
viper.SetEnvPrefix("gorush") // will be uppercased automatically
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
|
|
|
|
if confPath != "" {
|
|
|
|
content, err := ioutil.ReadFile(confPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return conf, err
|
|
|
|
}
|
|
|
|
|
2018-08-15 06:56:52 +00:00
|
|
|
if err := viper.ReadConfig(bytes.NewBuffer(content)); err != nil {
|
|
|
|
return conf, err
|
|
|
|
}
|
2017-10-23 03:06:30 +00:00
|
|
|
} else {
|
|
|
|
// Search config in home directory with name ".gorush" (without extension).
|
|
|
|
viper.AddConfigPath("/etc/gorush/")
|
|
|
|
viper.AddConfigPath("$HOME/.gorush")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
|
|
} else {
|
|
|
|
// load default config
|
2018-08-15 06:56:52 +00:00
|
|
|
if err := viper.ReadConfig(bytes.NewBuffer(defaultConf)); err != nil {
|
|
|
|
return conf, err
|
|
|
|
}
|
2017-10-23 03:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 05:59:56 +00:00
|
|
|
// Core
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Core.Address = viper.GetString("core.address")
|
|
|
|
conf.Core.Port = viper.GetString("core.port")
|
2020-02-05 15:23:16 +00:00
|
|
|
conf.Core.ShutdownTimeout = int64(viper.GetInt("core.shutdown_timeout"))
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Core.Enabled = viper.GetBool("core.enabled")
|
|
|
|
conf.Core.WorkerNum = int64(viper.GetInt("core.worker_num"))
|
|
|
|
conf.Core.QueueNum = int64(viper.GetInt("core.queue_num"))
|
|
|
|
conf.Core.Mode = viper.GetString("core.mode")
|
|
|
|
conf.Core.Sync = viper.GetBool("core.sync")
|
2019-09-06 07:48:42 +00:00
|
|
|
conf.Core.FeedbackURL = viper.GetString("core.feedback_hook_url")
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Core.SSL = viper.GetBool("core.ssl")
|
|
|
|
conf.Core.CertPath = viper.GetString("core.cert_path")
|
|
|
|
conf.Core.KeyPath = viper.GetString("core.key_path")
|
2018-02-18 09:12:51 +00:00
|
|
|
conf.Core.CertBase64 = viper.GetString("core.cert_base64")
|
|
|
|
conf.Core.KeyBase64 = viper.GetString("core.key_base64")
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Core.MaxNotification = int64(viper.GetInt("core.max_notification"))
|
|
|
|
conf.Core.HTTPProxy = viper.GetString("core.http_proxy")
|
|
|
|
conf.Core.PID.Enabled = viper.GetBool("core.pid.enabled")
|
|
|
|
conf.Core.PID.Path = viper.GetString("core.pid.path")
|
|
|
|
conf.Core.PID.Override = viper.GetBool("core.pid.override")
|
|
|
|
conf.Core.AutoTLS.Enabled = viper.GetBool("core.auto_tls.enabled")
|
|
|
|
conf.Core.AutoTLS.Folder = viper.GetString("core.auto_tls.folder")
|
|
|
|
conf.Core.AutoTLS.Host = viper.GetString("core.auto_tls.host")
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
// Api
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.API.PushURI = viper.GetString("api.push_uri")
|
|
|
|
conf.API.StatGoURI = viper.GetString("api.stat_go_uri")
|
|
|
|
conf.API.StatAppURI = viper.GetString("api.stat_app_uri")
|
|
|
|
conf.API.ConfigURI = viper.GetString("api.config_uri")
|
|
|
|
conf.API.SysStatURI = viper.GetString("api.sys_stat_uri")
|
|
|
|
conf.API.MetricURI = viper.GetString("api.metric_uri")
|
2017-10-26 02:19:58 +00:00
|
|
|
conf.API.HealthURI = viper.GetString("api.health_uri")
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
// Android
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Android.Enabled = viper.GetBool("android.enabled")
|
|
|
|
conf.Android.APIKey = viper.GetString("android.apikey")
|
|
|
|
conf.Android.MaxRetry = viper.GetInt("android.max_retry")
|
2016-03-26 05:59:56 +00:00
|
|
|
|
|
|
|
// iOS
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Ios.Enabled = viper.GetBool("ios.enabled")
|
|
|
|
conf.Ios.KeyPath = viper.GetString("ios.key_path")
|
2018-02-18 09:12:51 +00:00
|
|
|
conf.Ios.KeyBase64 = viper.GetString("ios.key_base64")
|
|
|
|
conf.Ios.KeyType = viper.GetString("ios.key_type")
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Ios.Password = viper.GetString("ios.password")
|
|
|
|
conf.Ios.Production = viper.GetBool("ios.production")
|
|
|
|
conf.Ios.MaxRetry = viper.GetInt("ios.max_retry")
|
2017-10-25 08:49:23 +00:00
|
|
|
conf.Ios.KeyID = viper.GetString("ios.key_id")
|
|
|
|
conf.Ios.TeamID = viper.GetString("ios.team_id")
|
2016-03-26 05:59:56 +00:00
|
|
|
|
2016-04-05 05:15:47 +00:00
|
|
|
// log
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Log.Format = viper.GetString("log.format")
|
|
|
|
conf.Log.AccessLog = viper.GetString("log.access_log")
|
|
|
|
conf.Log.AccessLevel = viper.GetString("log.access_level")
|
|
|
|
conf.Log.ErrorLog = viper.GetString("log.error_log")
|
|
|
|
conf.Log.ErrorLevel = viper.GetString("log.error_level")
|
|
|
|
conf.Log.HideToken = viper.GetBool("log.hide_token")
|
2016-04-05 05:15:47 +00:00
|
|
|
|
2017-07-24 10:58:30 +00:00
|
|
|
// Stat Engine
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.Stat.Engine = viper.GetString("stat.engine")
|
|
|
|
conf.Stat.Redis.Addr = viper.GetString("stat.redis.addr")
|
|
|
|
conf.Stat.Redis.Password = viper.GetString("stat.redis.password")
|
|
|
|
conf.Stat.Redis.DB = viper.GetInt("stat.redis.db")
|
|
|
|
conf.Stat.BoltDB.Path = viper.GetString("stat.boltdb.path")
|
|
|
|
conf.Stat.BoltDB.Bucket = viper.GetString("stat.boltdb.bucket")
|
|
|
|
conf.Stat.BuntDB.Path = viper.GetString("stat.buntdb.path")
|
|
|
|
conf.Stat.LevelDB.Path = viper.GetString("stat.leveldb.path")
|
2016-08-02 07:35:28 +00:00
|
|
|
|
2017-07-24 10:58:30 +00:00
|
|
|
// gRPC Server
|
2017-10-23 03:06:30 +00:00
|
|
|
conf.GRPC.Enabled = viper.GetBool("grpc.enabled")
|
|
|
|
conf.GRPC.Port = viper.GetString("grpc.port")
|
2016-03-26 05:59:56 +00:00
|
|
|
|
2017-10-23 03:06:30 +00:00
|
|
|
if conf.Core.WorkerNum == int64(0) {
|
|
|
|
conf.Core.WorkerNum = int64(runtime.NumCPU())
|
2017-01-25 08:29:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 03:06:30 +00:00
|
|
|
if conf.Core.QueueNum == int64(0) {
|
|
|
|
conf.Core.QueueNum = int64(8192)
|
2017-01-25 08:29:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 03:06:30 +00:00
|
|
|
return conf, nil
|
2016-03-26 05:59:56 +00:00
|
|
|
}
|