fix(config): change the order of overwriting

This commit is contained in:
hexxa 2022-01-29 14:08:07 +08:00 committed by Hexxa
parent 4d2231b7db
commit d0c0b80eff
3 changed files with 122 additions and 41 deletions

View file

@ -7,6 +7,10 @@ import (
"github.com/ihexxa/quickshare/src/db/userstore"
)
type DbConfig struct {
DbPath string `json:"dbPath" yaml:"dbPath"`
}
type FSConfig struct {
Root string `json:"root" yaml:"root"`
OpensLimit int `json:"opensLimit" yaml:"opensLimit"`
@ -60,6 +64,7 @@ type Config struct {
Users *UsersCfg `json:"users" yaml:"users"`
Workers *WorkerPoolCfg `json:"workers" yaml:"workers"`
Site *sitestore.SiteConfig `json:"site" yaml:"site"`
Db *DbConfig `json:"db" yaml:"db"`
}
func NewConfig() *Config {
@ -126,5 +131,8 @@ func DefaultConfigStruct() *Config {
},
},
},
Db: &DbConfig{
DbPath: "quickshare.db",
},
}
}

View file

@ -30,27 +30,26 @@ func LoadCfg(opts *Opts) (*gocfg.Cfg, error) {
return nil, err
}
if opts.DbPath != "" {
cfg, err = mergeDbConfig(cfg, opts.DbPath)
if err != nil {
return nil, err
}
} else {
_, err := os.Stat(boltdbpvd.DBName)
if err == nil {
cfg, err = mergeDbConfig(cfg, boltdbpvd.DBName)
} else if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
}
}
cfg, err = mergeConfigFiles(cfg, opts.Configs)
if err != nil {
return nil, err
}
dbPath := cfg.GrabString("Db.DbPath")
if opts.DbPath != "" {
dbPath = opts.DbPath
_, err := os.Stat(dbPath)
if err == nil {
cfg, err = mergeDbConfig(cfg, dbPath)
} else if err != nil {
if !os.IsNotExist(err) {
return nil, err
} else {
fmt.Printf("warning: Database does not exist in (%s), skipped", dbPath)
}
}
}
return mergeArgs(cfg, opts)
}
@ -102,6 +101,9 @@ func mergeArgs(cfg *gocfg.Cfg, opts *Opts) (*gocfg.Cfg, error) {
if opts.Port != 0 {
cfg.SetInt("Server.Port", opts.Port)
}
if opts.DbPath != "" {
cfg.SetString("Db.DbPath", opts.DbPath)
}
return cfg, nil
}

View file

@ -20,52 +20,49 @@ func TestLoadCfg(t *testing.T) {
DbPath: "",
Configs: []string{},
},
// default config + db
// default config + config_1
&Opts{
Host: "",
Port: 0,
DbPath: "testdata",
Configs: []string{},
},
// default config + db + config_1
&Opts{
Host: "",
Port: 0,
DbPath: "testdata",
DbPath: "",
Configs: []string{"testdata/config_1.yml"},
},
// default config + db + config_1 + config_2
// default config + config_1 + config_4
&Opts{
Host: "",
Port: 0,
DbPath: "testdata",
DbPath: "",
Configs: []string{"testdata/config_1.yml", "testdata/config_4.yml"},
},
// config partial overwrite
&Opts{
Host: "",
Port: 0,
DbPath: "testdata",
DbPath: "",
Configs: []string{
"testdata/config_1.yml",
"testdata/config_4.yml",
"testdata/config_partial_users.yml",
"testdata/config_partial_db.yml",
},
},
// default config + config_1 + config_4 + db_partial + args(db)
&Opts{
Host: "",
Port: 0,
DbPath: "testdata/test_quickshare.db",
Configs: []string{
"testdata/config_1.yml",
"testdata/config_4.yml",
"testdata/config_partial_users.yml",
"testdata/config_partial_db.yml",
},
},
// arg overwrite
}
cfg1 := DefaultConfigStruct()
cfgDefault := DefaultConfigStruct()
cfg2 := DefaultConfigStruct()
cfg2.Site.ClientCfg.SiteName = "Quickshare"
cfg2.Site.ClientCfg.SiteDesc = "Quickshare"
cfg2.Site.ClientCfg.Bg.Url = "test.png"
cfg2.Site.ClientCfg.Bg.Repeat = "no-repeat"
cfg2.Site.ClientCfg.Bg.Position = "top"
cfg2.Site.ClientCfg.Bg.Align = "scroll"
cfg3 := &Config{
cfg1 := &Config{
Fs: &FSConfig{
Root: "1",
OpensLimit: 1,
@ -125,6 +122,9 @@ func TestLoadCfg(t *testing.T) {
},
},
},
Db: &DbConfig{
DbPath: "1",
},
}
cfg4 := &Config{
@ -187,6 +187,9 @@ func TestLoadCfg(t *testing.T) {
},
},
},
Db: &DbConfig{
DbPath: "4",
},
}
cfg5 := &Config{
@ -249,14 +252,82 @@ func TestLoadCfg(t *testing.T) {
},
},
},
Db: &DbConfig{
DbPath: "5",
},
}
cfgWithDB := &Config{
Fs: &FSConfig{
Root: "4",
OpensLimit: 4,
OpenTTL: 4,
},
Users: &UsersCfg{
EnableAuth: true,
DefaultAdmin: "5",
DefaultAdminPwd: "5",
CookieTTL: 5,
CookieSecure: true,
CookieHttpOnly: true,
MinUserNameLen: 5,
MinPwdLen: 5,
CaptchaWidth: 5,
CaptchaHeight: 5,
CaptchaEnabled: true,
UploadSpeedLimit: 5,
DownloadSpeedLimit: 5,
SpaceLimit: 5,
LimiterCapacity: 5,
LimiterCyc: 5,
PredefinedUsers: []*userstore.UserCfg{
&userstore.UserCfg{
Name: "5",
Pwd: "5",
Role: "5",
},
},
},
Secrets: &Secrets{
TokenSecret: "4",
},
Server: &ServerCfg{
Debug: false,
Host: "4",
Port: 4,
ReadTimeout: 4,
WriteTimeout: 4,
MaxHeaderBytes: 4,
PublicPath: "4",
},
Workers: &WorkerPoolCfg{
QueueSize: 4,
SleepCyc: 4,
WorkerCount: 4,
},
Site: &sitestore.SiteConfig{
ClientCfg: &sitestore.ClientConfig{
SiteName: "Quickshare",
SiteDesc: "Quickshare",
Bg: &sitestore.BgConfig{
Url: "test.png",
Repeat: "no-repeat",
Position: "top",
Align: "scroll",
},
},
},
Db: &DbConfig{
DbPath: "testdata/test_quickshare.db",
},
}
expects := []*Config{
cfgDefault,
cfg1,
cfg2,
cfg3,
cfg4,
cfg5,
cfgWithDB,
}
testLoadCfg := func(t *testing.T) {