fix(config): add config loading functions and tests, also fix config issues

This commit is contained in:
hexxa 2022-01-28 22:11:27 +08:00 committed by Hexxa
parent 67da1ea0eb
commit 32a003d789
11 changed files with 42 additions and 69 deletions

View file

@ -30,7 +30,7 @@ English | [简体中文](./docs/README_zh-cn.md)
- File Management - File Management
- Support uploading, downloading, creating, deleting and moving files and folders - Support uploading, downloading, creating, deleting and moving files and folders
- Resumable uploading and downloading - Resumable uploading and downloading
- Only browser is needed - Finish operations in browser
- Share directories to others, including anonymous - Share directories to others, including anonymous
- Scan QR codes to visit sharing folders - Scan QR codes to visit sharing folders
- Upload hundreds of files at once - Upload hundreds of files at once

View file

@ -2,69 +2,36 @@ package main
import ( import (
"fmt" "fmt"
"strings" "os"
"github.com/ihexxa/gocfg"
goflags "github.com/jessevdk/go-flags" goflags "github.com/jessevdk/go-flags"
"github.com/ihexxa/quickshare/src/server" serverPkg "github.com/ihexxa/quickshare/src/server"
) )
var opts struct { var opts = &serverPkg.Opts{}
Host string `short:"h" long:"host" description:"server hostname"`
Port int `short:"p" long:"port" description:"server port"`
Debug bool `short:"d" long:"debug" description:"debug mode"`
Configs []string `short:"c" description:"config path"`
}
func main() { func main() {
_, err := goflags.Parse(&opts) _, err := goflags.Parse(opts)
if err != nil {
panic(err)
}
defaultCfg, err := server.DefaultConfig()
if err != nil { if err != nil {
panic(err) panic(err)
} }
cfg, err := gocfg.New(server.NewConfig()).Load(gocfg.JSONStr(defaultCfg)) cfg, err := serverPkg.LoadCfg(opts)
if err != nil { if err != nil {
panic(err) fmt.Printf("failed to load config: %s", err)
} os.Exit(1)
if len(opts.Configs) > 0 {
for _, configPath := range opts.Configs {
if strings.HasSuffix(configPath, ".yml") || strings.HasSuffix(configPath, ".yaml") {
cfg, err = cfg.Load(gocfg.YAML(configPath))
} else if strings.HasSuffix(configPath, ".json") {
cfg, err = cfg.Load(gocfg.JSON(configPath))
} else {
panic(fmt.Sprintf("unknown config file type (.yml .yaml .json are supported): %s", configPath))
} }
srv, err := serverPkg.NewServer(cfg)
if err != nil { if err != nil {
panic(err) fmt.Printf("failed to new server: %s", err)
} os.Exit(1)
}
}
if opts.Host != "" {
cfg.SetString("Server.Host", opts.Host)
}
if opts.Port != 0 {
cfg.SetInt("Server.Port", opts.Port)
}
if opts.Debug {
cfg.SetBool("Server.Debug", opts.Debug)
}
srv, err := server.NewServer(cfg)
if err != nil {
panic(err)
} }
err = srv.Start() err = srv.Start()
if err != nil { if err != nil {
panic(err) fmt.Printf("failed to start server: %s", err)
os.Exit(1)
} }
} }

View file

@ -9,7 +9,7 @@ server:
host: "0.0.0.0" host: "0.0.0.0"
port: 8686 port: 8686
readTimeout: 2000 readTimeout: 2000
writerTimeout: 86400000 # 1 day writeTimeout: 86400000 # 1 day
maxHeaderBytes: 512 maxHeaderBytes: 512
publicPath: "/quickshare/public" publicPath: "/quickshare/public"
users: users:

View file

@ -9,7 +9,7 @@ server:
host: "127.0.0.1" host: "127.0.0.1"
port: 8686 port: 8686
readTimeout: 2000 readTimeout: 2000
writerTimeout: 86400000 # 1 day writeTimeout: 86400000 # 1 day
maxHeaderBytes: 512 maxHeaderBytes: 512
publicPath: "public" publicPath: "public"
users: users:

View file

@ -9,7 +9,7 @@ server:
host: "0.0.0.0" host: "0.0.0.0"
port: 8686 port: 8686
readTimeout: 2000 readTimeout: 2000
writerTimeout: 86400000 # 1 day writeTimeout: 86400000 # 1 day
maxHeaderBytes: 512 maxHeaderBytes: 512
publicPath: "/quickshare/public" publicPath: "/quickshare/public"
users: users:

View file

@ -9,7 +9,7 @@ server:
host: "0.0.0.0" host: "0.0.0.0"
port: 8686 port: 8686
readTimeout: 2000 readTimeout: 2000
writerTimeout: 86400000 # 1 day writeTimeout: 86400000 # 1 day
maxHeaderBytes: 512 maxHeaderBytes: 512
publicPath: "public" publicPath: "public"
users: users:

2
go.mod
View file

@ -10,7 +10,7 @@ require (
github.com/gin-gonic/gin v1.7.3 github.com/gin-gonic/gin v1.7.3
github.com/go-playground/validator/v10 v10.9.0 // indirect github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/ihexxa/gocfg v0.0.0-20210914021417-6ba19520f0ff github.com/ihexxa/gocfg v0.0.0-20220128082538-9e607ebed51b
github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d
github.com/jessevdk/go-flags v1.4.0 github.com/jessevdk/go-flags v1.4.0
github.com/json-iterator/go v1.1.11 // indirect github.com/json-iterator/go v1.1.11 // indirect

2
go.sum
View file

@ -50,6 +50,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/ihexxa/gocfg v0.0.0-20210914021417-6ba19520f0ff h1:rsaVb8KIlg3gd7CEPTV/Qi5cf6i4C12EmbnsRMjGxkY= github.com/ihexxa/gocfg v0.0.0-20210914021417-6ba19520f0ff h1:rsaVb8KIlg3gd7CEPTV/Qi5cf6i4C12EmbnsRMjGxkY=
github.com/ihexxa/gocfg v0.0.0-20210914021417-6ba19520f0ff/go.mod h1:oqDTq1ywx4Qi9DdhFwwMHoPCYv6Txrfj2SY5WWcgiJs= github.com/ihexxa/gocfg v0.0.0-20210914021417-6ba19520f0ff/go.mod h1:oqDTq1ywx4Qi9DdhFwwMHoPCYv6Txrfj2SY5WWcgiJs=
github.com/ihexxa/gocfg v0.0.0-20220128082538-9e607ebed51b h1:pzx3BC3+v38POQg9sJWhfs/a4xFaJfC0Ar4RRVnFIMo=
github.com/ihexxa/gocfg v0.0.0-20220128082538-9e607ebed51b/go.mod h1:oqDTq1ywx4Qi9DdhFwwMHoPCYv6Txrfj2SY5WWcgiJs=
github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d h1:+v33khYHVDPEuuWO/JE1IzhoIu5uNvEcSs5GmXc5Sjw= github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d h1:+v33khYHVDPEuuWO/JE1IzhoIu5uNvEcSs5GmXc5Sjw=
github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d/go.mod h1:rhOAe/52S/J1fq1VnXvKX8FHuo65I+IcYUozW4M7+wE= github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d/go.mod h1:rhOAe/52S/J1fq1VnXvKX8FHuo65I+IcYUozW4M7+wE=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=

View file

@ -31,20 +31,20 @@ type ISiteStore interface {
} }
type ClientConfig struct { type ClientConfig struct {
SiteName string `json:"siteName"` SiteName string `json:"siteName" yaml:"siteName"`
SiteDesc string `json:"siteDesc"` SiteDesc string `json:"siteDesc" yaml:"siteDesc"`
Bg *BgConfig `json:"bg"` Bg *BgConfig `json:"bg" yaml:"bg"`
} }
type BgConfig struct { type BgConfig struct {
Url string `json:"url"` Url string `json:"url" yaml:"url"`
Repeat string `json:"repeat"` Repeat string `json:"repeat" yaml:"repeat"`
Position string `json:"position"` Position string `json:"position" yaml:"position"`
Align string `json:"align"` Align string `json:"align" yaml:"align"`
} }
type SiteConfig struct { type SiteConfig struct {
ClientCfg *ClientConfig `json:"clientCfg"` ClientCfg *ClientConfig `json:"clientCfg" yaml:"clientCfg"`
} }
type SiteStore struct { type SiteStore struct {

View file

@ -16,6 +16,7 @@ import (
var ( var (
ErrBucketNotFound = errors.New("bucket not found") ErrBucketNotFound = errors.New("bucket not found")
DBName = "quickshare.db"
) )
type BoltPvd struct { type BoltPvd struct {
@ -25,7 +26,7 @@ type BoltPvd struct {
} }
func New(dbPath string, maxStrLen int) *BoltPvd { func New(dbPath string, maxStrLen int) *BoltPvd {
boltPath := path.Join(path.Clean(dbPath), "quickshare.db") boltPath := path.Join(path.Clean(dbPath), DBName)
db, err := bolt.Open(boltPath, 0600, &bolt.Options{Timeout: 1 * time.Second}) db, err := bolt.Open(boltPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil { if err != nil {
panic(err) panic(err)

View file

@ -67,10 +67,15 @@ func NewConfig() *Config {
} }
func DefaultConfig() (string, error) { func DefaultConfig() (string, error) {
defaultCfg := &Config{ cfgBytes, err := json.Marshal(DefaultConfigStruct())
return string(cfgBytes), err
}
func DefaultConfigStruct() *Config {
return &Config{
Fs: &FSConfig{ Fs: &FSConfig{
Root: "root", Root: "root",
OpensLimit: 128, OpensLimit: 1024,
OpenTTL: 60, // 1 min OpenTTL: 60, // 1 min
}, },
Users: &UsersCfg{ Users: &UsersCfg{
@ -81,15 +86,16 @@ func DefaultConfig() (string, error) {
CookieSecure: false, CookieSecure: false,
CookieHttpOnly: true, CookieHttpOnly: true,
MinUserNameLen: 4, MinUserNameLen: 4,
MinPwdLen: 6, MinPwdLen: 8,
CaptchaWidth: 256, CaptchaWidth: 256,
CaptchaHeight: 60, CaptchaHeight: 60,
CaptchaEnabled: true, CaptchaEnabled: true,
UploadSpeedLimit: 100 * 1024, // B UploadSpeedLimit: 1024 * 1024, // B
DownloadSpeedLimit: 100 * 1024, // B DownloadSpeedLimit: 1024 * 1024, // B
SpaceLimit: 1024 * 1024 * 100, // 100MB SpaceLimit: 1024 * 1024 * 100, // 100MB
LimiterCapacity: 1000, LimiterCapacity: 1000,
LimiterCyc: 1000, // 1s LimiterCyc: 1000, // 1s
PredefinedUsers: []*userstore.UserCfg{},
}, },
Secrets: &Secrets{ Secrets: &Secrets{
TokenSecret: "", TokenSecret: "",
@ -121,7 +127,4 @@ func DefaultConfig() (string, error) {
}, },
}, },
} }
cfgBytes, err := json.Marshal(defaultCfg)
return string(cfgBytes), err
} }