mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
refactor: improved init flow in main
This commit is contained in:
parent
fd223c7542
commit
1e39d0b186
3 changed files with 58 additions and 49 deletions
27
cmd/main.go
27
cmd/main.go
|
@ -28,18 +28,21 @@ import (
|
||||||
|
|
||||||
var rawLogger = log.New(os.Stdout, "", 0)
|
var rawLogger = log.New(os.Stdout, "", 0)
|
||||||
|
|
||||||
func init() {
|
func parallel(fns ...func()) {
|
||||||
var out io.Writer = os.Stderr
|
var wg sync.WaitGroup
|
||||||
if common.EnableLogStreaming {
|
for _, fn := range fns {
|
||||||
out = zerolog.MultiLevelWriter(out, v1.GetMemLogger())
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
fn()
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
logging.InitLogger(out)
|
wg.Wait()
|
||||||
// logging.AddHook(v1.GetMemLogger())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
initProfiling()
|
initProfiling()
|
||||||
args := common.GetArgs()
|
args := pkg.GetArgs(common.MainServerCommandValidator{})
|
||||||
|
|
||||||
switch args.Command {
|
switch args.Command {
|
||||||
case common.CommandReload:
|
case common.CommandReload:
|
||||||
|
@ -76,7 +79,11 @@ func main() {
|
||||||
if args.Command == common.CommandStart {
|
if args.Command == common.CommandStart {
|
||||||
logging.Info().Msgf("GoDoxy version %s", pkg.GetVersion())
|
logging.Info().Msgf("GoDoxy version %s", pkg.GetVersion())
|
||||||
logging.Trace().Msg("trace enabled")
|
logging.Trace().Msg("trace enabled")
|
||||||
// logging.AddHook(notif.GetDispatcher())
|
parallel(
|
||||||
|
internal.InitIconListCache,
|
||||||
|
homepage.InitOverridesConfig,
|
||||||
|
favicon.InitIconCache,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
logging.DiscardLogger()
|
logging.DiscardLogger()
|
||||||
}
|
}
|
||||||
|
@ -121,10 +128,6 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go internal.InitIconListCache()
|
|
||||||
go homepage.InitOverridesConfig()
|
|
||||||
go favicon.InitIconCache()
|
|
||||||
|
|
||||||
cfg.Start(&config.StartServersOptions{
|
cfg.Start(&config.StartServersOptions{
|
||||||
Proxy: true,
|
Proxy: true,
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,15 +1,5 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Args struct {
|
|
||||||
Command string
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CommandStart = ""
|
CommandStart = ""
|
||||||
CommandValidate = "validate"
|
CommandValidate = "validate"
|
||||||
|
@ -22,33 +12,20 @@ const (
|
||||||
CommandDebugListMTrace = "debug-ls-mtrace"
|
CommandDebugListMTrace = "debug-ls-mtrace"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ValidCommands = []string{
|
type MainServerCommandValidator struct{}
|
||||||
CommandStart,
|
|
||||||
CommandValidate,
|
|
||||||
CommandListConfigs,
|
|
||||||
CommandListRoutes,
|
|
||||||
CommandListIcons,
|
|
||||||
CommandReload,
|
|
||||||
CommandDebugListEntries,
|
|
||||||
CommandDebugListProviders,
|
|
||||||
CommandDebugListMTrace,
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateArg(arg string) error {
|
func (v MainServerCommandValidator) IsCommandValid(cmd string) bool {
|
||||||
for _, v := range ValidCommands {
|
switch cmd {
|
||||||
if arg == v {
|
case CommandStart,
|
||||||
return nil
|
CommandValidate,
|
||||||
}
|
CommandListConfigs,
|
||||||
|
CommandListRoutes,
|
||||||
|
CommandListIcons,
|
||||||
|
CommandReload,
|
||||||
|
CommandDebugListEntries,
|
||||||
|
CommandDebugListProviders,
|
||||||
|
CommandDebugListMTrace:
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
return fmt.Errorf("invalid command %q", arg)
|
return false
|
||||||
}
|
|
||||||
|
|
||||||
func GetArgs() Args {
|
|
||||||
var args Args
|
|
||||||
flag.Parse()
|
|
||||||
args.Command = flag.Arg(0)
|
|
||||||
if err := validateArg(args.Command); err != nil {
|
|
||||||
log.Fatalf("invalid command: %s", err)
|
|
||||||
}
|
|
||||||
return args
|
|
||||||
}
|
}
|
||||||
|
|
29
pkg/args.go
Normal file
29
pkg/args.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Args struct {
|
||||||
|
Command string
|
||||||
|
Args []string
|
||||||
|
}
|
||||||
|
CommandValidator interface {
|
||||||
|
IsCommandValid(cmd string) bool
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetArgs(validator CommandValidator) Args {
|
||||||
|
var args Args
|
||||||
|
flag.Parse()
|
||||||
|
args.Command = flag.Arg(0)
|
||||||
|
if !validator.IsCommandValid(args.Command) {
|
||||||
|
log.Fatalf("invalid command: %s", args.Command)
|
||||||
|
}
|
||||||
|
if len(flag.Args()) > 1 {
|
||||||
|
args.Args = flag.Args()[1:]
|
||||||
|
}
|
||||||
|
return args
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue