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
Issuer: paopao-api
Expire: 86400
Search: # 搜索配置
ZincHost: http://127.0.0.1:4080
ZincIndex: paopao-data
ZincUser: admin
ZincPassword: admin
Zinc: # Zinc搜索配置
Host: http://127.0.0.1:4080
Index: paopao-data

@ -1,7 +1,9 @@
package main
import (
"flag"
"log"
"strings"
"sync"
"time"
@ -14,19 +16,42 @@ import (
"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() {
flagParse()
err := setupSetting()
if err != nil {
log.Fatalf("init.setupSetting err: %v", err)
}
err = setupLogger()
if err != nil {
log.Fatalf("init.setupLogger err: %v", err)
}
err = setupDBEngine()
if err != nil {
log.Fatalf("init.setupDBEngine err: %v", err)
}
client := zinc.NewClient(global.ZincSetting)
service.Initialize(global.DBEngine, client)
}
@ -38,6 +63,11 @@ func setupSetting() error {
}
global.Features = setting.FeaturesFrom("Features")
if len(features) > 0 {
if err = global.Features.Use(features, noDefaultFeatures); err != nil {
return err
}
}
objects := map[string]interface{}{
"App": &global.AppSetting,
@ -63,6 +93,12 @@ func setupSetting() error {
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 {
logger, err := logger.New()
if err != nil {

@ -33,6 +33,79 @@ func TestUseDefault(t *testing.T) {
"Sms": true,
"Sms = SmsJuhe": true,
"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 {
t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok)

@ -12,11 +12,6 @@ type Setting struct {
vp *viper.Viper
}
type LogType string
const LogFileType LogType = "file"
const LogZincType LogType = "zinc"
type LoggerFileSettingS struct {
SavePath string
FileName string
@ -30,17 +25,6 @@ type LoggerZincSettingS struct {
Password string
}
type LoggerSettingS struct {
LogType LogType
LogFileSavePath string
LogFileName string
LogFileExt string
LogZincHost string
LogZincIndex string
LogZincUser string
LogZincPassword string
}
type ServerSettingS struct {
RunMode string
HttpIp string
@ -84,20 +68,6 @@ type ZincSettingS struct {
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 {
UserName string
Password string
@ -194,23 +164,26 @@ func newFeatures(suites map[string][]string, kv map[string]string) *FeaturesSett
features := &FeaturesSettingS{
suites: suites,
kv: kv,
features: make(map[string]string),
}
features.UseDefault()
return features
}
func (f *FeaturesSettingS) UseDefault() {
defaultSuite := f.suites["default"]
f.Use(defaultSuite, true)
f.Use([]string{"default"}, true)
}
func (f *FeaturesSettingS) Use(suite []string, noDefault bool) error {
if noDefault {
if noDefault && len(f.features) != 0 {
f.features = make(map[string]string)
}
features := f.flatFeatures(suite)
for _, feature := range features {
feature = strings.ToLower(feature)
if len(feature) == 0 {
continue
}
f.features[feature] = f.kv[feature]
}
return nil

Loading…
Cancel
Save