|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
@ -16,6 +17,19 @@ type LogType string
|
|
|
|
|
const LogFileType LogType = "file"
|
|
|
|
|
const LogZincType LogType = "zinc"
|
|
|
|
|
|
|
|
|
|
type LoggerFileSettingS struct {
|
|
|
|
|
SavePath string
|
|
|
|
|
FileName string
|
|
|
|
|
FileExt string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type LoggerZincSettingS struct {
|
|
|
|
|
Host string
|
|
|
|
|
Index string
|
|
|
|
|
User string
|
|
|
|
|
Password string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type LoggerSettingS struct {
|
|
|
|
|
LogType LogType
|
|
|
|
|
LogFileSavePath string
|
|
|
|
@ -44,22 +58,30 @@ type AppSettingS struct {
|
|
|
|
|
MaxPageSize int
|
|
|
|
|
IsShastaTestnet bool
|
|
|
|
|
TronApiKeys []string
|
|
|
|
|
SmsJuheKey string
|
|
|
|
|
SmsJuheTplID string
|
|
|
|
|
SmsJuheTplVal string
|
|
|
|
|
AlipayAppID string
|
|
|
|
|
AlipayPrivateKey string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RuntimeSettingS struct {
|
|
|
|
|
DisablePhoneVerify bool
|
|
|
|
|
type AlipaySettingS struct {
|
|
|
|
|
AppID string
|
|
|
|
|
PrivateKey string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SmsJuheSettings struct {
|
|
|
|
|
Key string
|
|
|
|
|
TplID string
|
|
|
|
|
TplVal string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SearchSettingS struct {
|
|
|
|
|
ZincHost string
|
|
|
|
|
ZincIndex string
|
|
|
|
|
ZincUser string
|
|
|
|
|
ZincPassword string
|
|
|
|
|
type FeaturesSettingS struct {
|
|
|
|
|
kv map[string]string
|
|
|
|
|
suites map[string][]string
|
|
|
|
|
features map[string]string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ZincSettingS struct {
|
|
|
|
|
Host string
|
|
|
|
|
Index string
|
|
|
|
|
User string
|
|
|
|
|
Password string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DatabaseSettingS struct {
|
|
|
|
@ -75,6 +97,20 @@ type DatabaseSettingS struct {
|
|
|
|
|
MaxIdleConns int
|
|
|
|
|
MaxOpenConns int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MySQLSettingS struct {
|
|
|
|
|
UserName string
|
|
|
|
|
Password string
|
|
|
|
|
Host string
|
|
|
|
|
DBName string
|
|
|
|
|
TablePrefix string
|
|
|
|
|
Charset string
|
|
|
|
|
ParseTime bool
|
|
|
|
|
LogLevel logger.LogLevel
|
|
|
|
|
MaxIdleConns int
|
|
|
|
|
MaxOpenConns int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AliossSettingS struct {
|
|
|
|
|
AliossAccessKeyID string
|
|
|
|
|
AliossAccessKeySecret string
|
|
|
|
@ -83,6 +119,14 @@ type AliossSettingS struct {
|
|
|
|
|
AliossDomain string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AliOSSSettingS struct {
|
|
|
|
|
AccessKeyID string
|
|
|
|
|
AccessKeySecret string
|
|
|
|
|
Endpoint string
|
|
|
|
|
Bucket string
|
|
|
|
|
Domain string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RedisSettingS struct {
|
|
|
|
|
Host string
|
|
|
|
|
Password string
|
|
|
|
@ -117,3 +161,92 @@ func (s *Setting) ReadSection(k string, v interface{}) error {
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Setting) Unmarshal(objects map[string]interface{}) error {
|
|
|
|
|
for k, v := range objects {
|
|
|
|
|
err := s.vp.UnmarshalKey(k, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Setting) FeaturesFrom(k string) *FeaturesSettingS {
|
|
|
|
|
sub := s.vp.Sub(k)
|
|
|
|
|
keys := sub.AllKeys()
|
|
|
|
|
|
|
|
|
|
suites := make(map[string][]string)
|
|
|
|
|
kv := make(map[string]string, len(keys))
|
|
|
|
|
for _, key := range sub.AllKeys() {
|
|
|
|
|
val := sub.Get(key)
|
|
|
|
|
switch v := val.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
kv[key] = v
|
|
|
|
|
case []interface{}:
|
|
|
|
|
suites[key] = sub.GetStringSlice(key)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newFeatures(suites, kv)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newFeatures(suites map[string][]string, kv map[string]string) *FeaturesSettingS {
|
|
|
|
|
features := &FeaturesSettingS{
|
|
|
|
|
suites: suites,
|
|
|
|
|
kv: kv,
|
|
|
|
|
}
|
|
|
|
|
features.UseDefault()
|
|
|
|
|
return features
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *FeaturesSettingS) UseDefault() {
|
|
|
|
|
defaultSuite := f.suites["default"]
|
|
|
|
|
f.Use(defaultSuite, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *FeaturesSettingS) Use(suite []string, noDefault bool) error {
|
|
|
|
|
if noDefault {
|
|
|
|
|
f.features = make(map[string]string)
|
|
|
|
|
}
|
|
|
|
|
features := f.flatFeatures(suite)
|
|
|
|
|
for _, feature := range features {
|
|
|
|
|
feature = strings.ToLower(feature)
|
|
|
|
|
f.features[feature] = f.kv[feature]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *FeaturesSettingS) flatFeatures(suite []string) []string {
|
|
|
|
|
features := make([]string, 0, len(suite)+10)
|
|
|
|
|
for s := suite[:]; len(s) > 0; s = s[:len(s)-1] {
|
|
|
|
|
if items, exist := f.suites[s[0]]; exist {
|
|
|
|
|
s = append(s, items...)
|
|
|
|
|
}
|
|
|
|
|
features = append(features, s[0])
|
|
|
|
|
s[0] = s[len(s)-1]
|
|
|
|
|
}
|
|
|
|
|
return features
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cfg get value by key if exsit
|
|
|
|
|
func (f *FeaturesSettingS) Cfg(key string) (string, bool) {
|
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
|
value, exist := f.features[key]
|
|
|
|
|
return value, exist
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CfgIf check expression is true. if expression just have a string like
|
|
|
|
|
// `Sms` is mean `Sms` whether define in suite feature settings. expression like
|
|
|
|
|
// `Sms = SmsJuhe` is mean whether `Sms` define in suite feature settings and value
|
|
|
|
|
// is `SmsJuhe``
|
|
|
|
|
func (f *FeaturesSettingS) CfgIf(expression string) bool {
|
|
|
|
|
kv := strings.Split(expression, "=")
|
|
|
|
|
key := strings.Trim(strings.ToLower(kv[0]), " ")
|
|
|
|
|
v, ok := f.features[key]
|
|
|
|
|
if len(kv) == 2 && ok && strings.Trim(kv[1], " ") == v {
|
|
|
|
|
return true
|
|
|
|
|
} else if len(kv) == 1 && ok {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|