optimize #62 add command-line support for custom features

pull/64/head
alimy 3 years ago
parent 89dd735e61
commit 3907a0dbb9

@ -36,11 +36,6 @@ JWT: # 鉴权加密
Secret: 18a6413dc4fe394c66345ebe501b2f26 Secret: 18a6413dc4fe394c66345ebe501b2f26
Issuer: paopao-api Issuer: paopao-api
Expire: 86400 Expire: 86400
Search: # 搜索配置
ZincHost: http://127.0.0.1:4080
ZincIndex: paopao-data
ZincUser: admin
ZincPassword: admin
Zinc: # Zinc搜索配置 Zinc: # Zinc搜索配置
Host: http://127.0.0.1:4080 Host: http://127.0.0.1:4080
Index: paopao-data Index: paopao-data

@ -1,7 +1,9 @@
package main package main
import ( import (
"flag"
"log" "log"
"strings"
"sync" "sync"
"time" "time"
@ -14,19 +16,42 @@ import (
"github.com/rocboss/paopao-ce/pkg/zinc" "github.com/rocboss/paopao-ce/pkg/zinc"
) )
var (
noDefaultFeatures bool
features suites
)
type suites []string
func (s *suites) String() string {
return strings.Join(*s, ",")
}
func (s *suites) Set(value string) error {
for _, item := range strings.Split(value, ",") {
*s = append(*s, strings.TrimSpace(item))
}
return nil
}
func init() { func init() {
flagParse()
err := setupSetting() err := setupSetting()
if err != nil { if err != nil {
log.Fatalf("init.setupSetting err: %v", err) log.Fatalf("init.setupSetting err: %v", err)
} }
err = setupLogger() err = setupLogger()
if err != nil { if err != nil {
log.Fatalf("init.setupLogger err: %v", err) log.Fatalf("init.setupLogger err: %v", err)
} }
err = setupDBEngine() err = setupDBEngine()
if err != nil { if err != nil {
log.Fatalf("init.setupDBEngine err: %v", err) log.Fatalf("init.setupDBEngine err: %v", err)
} }
client := zinc.NewClient(global.ZincSetting) client := zinc.NewClient(global.ZincSetting)
service.Initialize(global.DBEngine, client) service.Initialize(global.DBEngine, client)
} }
@ -38,6 +63,11 @@ func setupSetting() error {
} }
global.Features = setting.FeaturesFrom("Features") global.Features = setting.FeaturesFrom("Features")
if len(features) > 0 {
if err = global.Features.Use(features, noDefaultFeatures); err != nil {
return err
}
}
objects := map[string]interface{}{ objects := map[string]interface{}{
"App": &global.AppSetting, "App": &global.AppSetting,
@ -63,6 +93,12 @@ func setupSetting() error {
return nil return nil
} }
func flagParse() {
flag.BoolVar(&noDefaultFeatures, "no-default-features", false, "whether use default features")
flag.Var(&features, "features", "use special features")
flag.Parse()
}
func setupLogger() error { func setupLogger() error {
logger, err := logger.New() logger, err := logger.New()
if err != nil { if err != nil {

@ -33,6 +33,79 @@ func TestUseDefault(t *testing.T) {
"Sms": true, "Sms": true,
"Sms = SmsJuhe": true, "Sms = SmsJuhe": true,
"SmsJuhe": false, "SmsJuhe": false,
"default": true,
} {
if ok := features.CfgIf(exp); res != ok {
t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok)
}
}
}
func TestUse(t *testing.T) {
suites := map[string][]string{
"default": {"Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LogZinc"},
"develop": {"Zinc", "MySQL", "AliOSS", "LogFile"},
"slim": {"Zinc", "MySQL", "Redis", "AliOSS", "LogFile"},
}
kv := map[string]string{
"sms": "SmsJuhe",
}
features := newFeatures(suites, kv)
features.Use([]string{"develop"}, true)
for _, data := range []struct {
key string
expect string
exist bool
}{
{"Sms", "", false},
{"Alipay", "", false},
{"Zinc", "", true},
{"Redis", "", false},
{"Database", "", false},
} {
if v, ok := features.Cfg(data.key); ok != data.exist || v != data.expect {
t.Errorf("key: %s expect: %s exist: %t got v: %s ok: %t", data.key, data.expect, data.exist, v, ok)
}
}
for exp, res := range map[string]bool{
"Sms": false,
"Sms = SmsJuhe": false,
"SmsJuhe": false,
"default": false,
"develop": true,
} {
if ok := features.CfgIf(exp); res != ok {
t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok)
}
}
features.UseDefault()
features.Use([]string{"slim", "", "demo"}, false)
for _, data := range []struct {
key string
expect string
exist bool
}{
{"Sms", "SmsJuhe", true},
{"Alipay", "", true},
{"Zinc", "", true},
{"Redis", "", true},
{"Database", "", false},
{"demo", "", true},
} {
if v, ok := features.Cfg(data.key); ok != data.exist || v != data.expect {
t.Errorf("key: %s expect: %s exist: %t got v: %s ok: %t", data.key, data.expect, data.exist, v, ok)
}
}
for exp, res := range map[string]bool{
"Sms": true,
"Sms = SmsJuhe": true,
"SmsJuhe": false,
"default": true,
"develop": false,
"slim": true,
"demo": true,
} { } {
if ok := features.CfgIf(exp); res != ok { if ok := features.CfgIf(exp); res != ok {
t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok) t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok)

@ -12,11 +12,6 @@ type Setting struct {
vp *viper.Viper vp *viper.Viper
} }
type LogType string
const LogFileType LogType = "file"
const LogZincType LogType = "zinc"
type LoggerFileSettingS struct { type LoggerFileSettingS struct {
SavePath string SavePath string
FileName string FileName string
@ -30,17 +25,6 @@ type LoggerZincSettingS struct {
Password string Password string
} }
type LoggerSettingS struct {
LogType LogType
LogFileSavePath string
LogFileName string
LogFileExt string
LogZincHost string
LogZincIndex string
LogZincUser string
LogZincPassword string
}
type ServerSettingS struct { type ServerSettingS struct {
RunMode string RunMode string
HttpIp string HttpIp string
@ -84,20 +68,6 @@ type ZincSettingS struct {
Password string Password string
} }
type DatabaseSettingS struct {
DBType string
UserName string
Password string
Host string
DBName string
TablePrefix string
Charset string
ParseTime bool
LogLevel logger.LogLevel
MaxIdleConns int
MaxOpenConns int
}
type MySQLSettingS struct { type MySQLSettingS struct {
UserName string UserName string
Password string Password string
@ -192,25 +162,28 @@ func (s *Setting) FeaturesFrom(k string) *FeaturesSettingS {
func newFeatures(suites map[string][]string, kv map[string]string) *FeaturesSettingS { func newFeatures(suites map[string][]string, kv map[string]string) *FeaturesSettingS {
features := &FeaturesSettingS{ features := &FeaturesSettingS{
suites: suites, suites: suites,
kv: kv, kv: kv,
features: make(map[string]string),
} }
features.UseDefault() features.UseDefault()
return features return features
} }
func (f *FeaturesSettingS) UseDefault() { func (f *FeaturesSettingS) UseDefault() {
defaultSuite := f.suites["default"] f.Use([]string{"default"}, true)
f.Use(defaultSuite, true)
} }
func (f *FeaturesSettingS) Use(suite []string, noDefault bool) error { func (f *FeaturesSettingS) Use(suite []string, noDefault bool) error {
if noDefault { if noDefault && len(f.features) != 0 {
f.features = make(map[string]string) f.features = make(map[string]string)
} }
features := f.flatFeatures(suite) features := f.flatFeatures(suite)
for _, feature := range features { for _, feature := range features {
feature = strings.ToLower(feature) feature = strings.ToLower(feature)
if len(feature) == 0 {
continue
}
f.features[feature] = f.kv[feature] f.features[feature] = f.kv[feature]
} }
return nil return nil

Loading…
Cancel
Save