fix(config): add config loading functions and tests, also fix config issues
This commit is contained in:
parent
67da1ea0eb
commit
32a003d789
11 changed files with 42 additions and 69 deletions
|
@ -2,69 +2,36 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"os"
|
||||
|
||||
"github.com/ihexxa/gocfg"
|
||||
goflags "github.com/jessevdk/go-flags"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/server"
|
||||
serverPkg "github.com/ihexxa/quickshare/src/server"
|
||||
)
|
||||
|
||||
var opts struct {
|
||||
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"`
|
||||
}
|
||||
var opts = &serverPkg.Opts{}
|
||||
|
||||
func main() {
|
||||
_, err := goflags.Parse(&opts)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defaultCfg, err := server.DefaultConfig()
|
||||
_, err := goflags.Parse(opts)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cfg, err := gocfg.New(server.NewConfig()).Load(gocfg.JSONStr(defaultCfg))
|
||||
cfg, err := serverPkg.LoadCfg(opts)
|
||||
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))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
srv, err := serverPkg.NewServer(cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fmt.Printf("failed to new server: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = srv.Start()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fmt.Printf("failed to start server: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue