From d0c0b80effcebdb0f5fff744a2f9b328f85eda78 Mon Sep 17 00:00:00 2001 From: hexxa Date: Sat, 29 Jan 2022 14:08:07 +0800 Subject: [PATCH] fix(config): change the order of overwriting --- src/server/config.go | 8 +++ src/server/config_load.go | 34 ++++----- src/server/config_load_test.go | 121 ++++++++++++++++++++++++++------- 3 files changed, 122 insertions(+), 41 deletions(-) diff --git a/src/server/config.go b/src/server/config.go index beb6d13..5f2b22c 100644 --- a/src/server/config.go +++ b/src/server/config.go @@ -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", + }, } } diff --git a/src/server/config_load.go b/src/server/config_load.go index 4ace96b..3d485e9 100644 --- a/src/server/config_load.go +++ b/src/server/config_load.go @@ -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 } diff --git a/src/server/config_load_test.go b/src/server/config_load_test.go index 87d863f..b0b6811 100644 --- a/src/server/config_load_test.go +++ b/src/server/config_load_test.go @@ -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) {