You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/internal/conf/settting.go

273 lines
5.1 KiB

package conf
2 years ago
import (
"fmt"
"strings"
2 years ago
"time"
"github.com/spf13/viper"
"gorm.io/gorm/logger"
)
type Setting struct {
vp *viper.Viper
}
type LoggerFileSettingS struct {
SavePath string
FileName string
FileExt string
Level string
}
type LoggerZincSettingS struct {
Host string
Index string
User string
Password string
Level string
}
2 years ago
type ServerSettingS struct {
RunMode string
HttpIp string
HttpPort string
ReadTimeout time.Duration
WriteTimeout time.Duration
}
type AppSettingS struct {
BarkToken string
MaxCommentCount int64
AttachmentIncomeRate float64
DefaultContextTimeout time.Duration
DefaultPageSize int
MaxPageSize int
IsShastaTestnet bool
TronApiKeys []string
}
type SimpleCacheIndexSettingS struct {
MaxIndexSize int
CheckTickDuration int
ExpireTickDuration int
ActionQPS int
}
type AlipaySettingS struct {
AppID string
PrivateKey string
}
type SmsJuheSettings struct {
Key string
TplID string
TplVal 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
2 years ago
}
type DatabaseSetingS struct {
TablePrefix string
LogLevel logger.LogLevel
}
type MySQLSettingS struct {
UserName string
Password string
Host string
DBName string
Charset string
ParseTime bool
MaxIdleConns int
MaxOpenConns int
}
type Sqlite3SettingS struct {
Path string
}
type MinIOSettingS struct {
AccessKey string
SecretKey string
Secure bool
Endpoint string
Bucket string
Domain string
}
type S3SettingS struct {
AccessKey string
SecretKey string
Secure bool
Endpoint string
Bucket string
Domain string
2 years ago
}
type AliOSSSettingS struct {
AccessKeyID string
AccessKeySecret string
Endpoint string
Bucket string
Domain string
}
type LocalOSSSettingS struct {
SavePath string
Secure bool
Bucket string
Domain string
}
2 years ago
type RedisSettingS struct {
Host string
Password string
DB int
}
type JWTSettingS struct {
Secret string
Issuer string
Expire time.Duration
}
func NewSetting() (*Setting, error) {
vp := viper.New()
vp.SetConfigName("config")
vp.AddConfigPath(".")
vp.AddConfigPath("configs/")
vp.SetConfigType("yaml")
err := vp.ReadInConfig()
if err != nil {
return nil, err
}
return &Setting{vp}, nil
}
func (s *Setting) ReadSection(k string, v interface{}) error {
err := s.vp.UnmarshalKey(k, v)
if err != nil {
return err
}
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: make(map[string]string),
}
features.UseDefault()
return features
}
func (f *FeaturesSettingS) UseDefault() {
f.Use([]string{"default"}, true)
}
func (f *FeaturesSettingS) Use(suite []string, noDefault bool) error {
if noDefault && len(f.features) != 0 {
f.features = make(map[string]string)
}
features := f.flatFeatures(suite)
for _, feature := range features {
if len(feature) == 0 {
continue
}
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] {
item := strings.TrimSpace(strings.ToLower(s[0]))
if len(item) > 0 {
if items, exist := f.suites[item]; exist {
s = append(s, items...)
}
features = append(features, item)
}
s[0] = s[len(s)-1]
}
return features
}
// Cfg get value by key if exist
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
}
func (s *MySQLSettingS) Dsn() string {
return fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s&parseTime=%t&loc=Local",
s.UserName,
s.Password,
s.Host,
s.DBName,
s.Charset,
s.ParseTime,
)
}