mirror of
https://github.com/yusing/godoxy.git
synced 2026-01-11 22:30:47 +01:00
fix(config): fix error and logging handling
This commit is contained in:
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -33,6 +34,24 @@ Make sure you rename it back before next time you start.`
|
||||
You may run "ls-config" to show or dump the current config.`
|
||||
)
|
||||
|
||||
func logNotifyError(action string, err error) {
|
||||
gperr.LogError("config "+action+" error", err)
|
||||
notif.Notify(¬if.LogMessage{
|
||||
Level: zerolog.ErrorLevel,
|
||||
Title: fmt.Sprintf("Config %s error", action),
|
||||
Body: notif.ErrorBody(err),
|
||||
})
|
||||
}
|
||||
|
||||
func logNotifyWarn(action string, err error) {
|
||||
gperr.LogWarn("config "+action+" error", err)
|
||||
notif.Notify(¬if.LogMessage{
|
||||
Level: zerolog.WarnLevel,
|
||||
Title: fmt.Sprintf("Config %s warning", action),
|
||||
Body: notif.ErrorBody(err),
|
||||
})
|
||||
}
|
||||
|
||||
func Load() error {
|
||||
if HasState() {
|
||||
panic(errors.New("config already loaded"))
|
||||
@@ -40,9 +59,15 @@ func Load() error {
|
||||
state := NewState()
|
||||
cfgWatcher = watcher.NewConfigFileWatcher(common.ConfigFileName)
|
||||
|
||||
err := errors.Join(state.InitFromFileOrExit(common.ConfigPath), state.StartProviders())
|
||||
initErr := state.InitFromFile(common.ConfigPath)
|
||||
if errors.Is(initErr, fs.ErrNotExist) {
|
||||
// log only
|
||||
log.Warn().Msg("config file not found, using default config")
|
||||
initErr = nil
|
||||
}
|
||||
err := errors.Join(initErr, state.StartProviders())
|
||||
if err != nil {
|
||||
notifyError("init", err)
|
||||
logNotifyError("init", err)
|
||||
}
|
||||
SetState(state)
|
||||
|
||||
@@ -51,24 +76,16 @@ func Load() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func notifyError(action string, err error) {
|
||||
notif.Notify(¬if.LogMessage{
|
||||
Level: zerolog.ErrorLevel,
|
||||
Title: fmt.Sprintf("Config %s Error", action),
|
||||
Body: notif.ErrorBody(err),
|
||||
})
|
||||
}
|
||||
|
||||
func Reload() gperr.Error {
|
||||
// avoid race between config change and API reload request
|
||||
reloadMu.Lock()
|
||||
defer reloadMu.Unlock()
|
||||
|
||||
newState := NewState()
|
||||
err := newState.InitFromFileOrExit(common.ConfigPath)
|
||||
err := newState.InitFromFile(common.ConfigPath)
|
||||
if err != nil {
|
||||
newState.Task().FinishAndWait(err)
|
||||
notifyError("reload", err)
|
||||
logNotifyError("reload", err)
|
||||
return gperr.New(ansi.Warning("using last config")).With(err)
|
||||
}
|
||||
|
||||
@@ -82,7 +99,7 @@ func Reload() gperr.Error {
|
||||
|
||||
if err := newState.StartProviders(); err != nil {
|
||||
gperr.LogWarn("start providers error", err)
|
||||
notifyError("start providers", err)
|
||||
logNotifyError("start providers", err)
|
||||
return nil // continue
|
||||
}
|
||||
StartProxyServers()
|
||||
@@ -107,10 +124,10 @@ func OnConfigChange(ev []events.Event) {
|
||||
// just reload once and check the last event
|
||||
switch ev[len(ev)-1].Action {
|
||||
case events.ActionFileRenamed:
|
||||
log.Warn().Msg(cfgRenameWarn)
|
||||
logNotifyWarn("rename", errors.New(cfgRenameWarn))
|
||||
return
|
||||
case events.ActionFileDeleted:
|
||||
log.Warn().Msg(cfgDeleteWarn)
|
||||
logNotifyWarn("delete", errors.New(cfgDeleteWarn))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/goccy/go-yaml"
|
||||
"github.com/puzpuzpuz/xsync/v4"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/yusing/godoxy/agent/pkg/agent"
|
||||
"github.com/yusing/godoxy/internal/acl"
|
||||
"github.com/yusing/godoxy/internal/autocert"
|
||||
@@ -91,16 +90,11 @@ func Value() *config.Config {
|
||||
return config.ActiveConfig.Load()
|
||||
}
|
||||
|
||||
func (state *state) InitFromFileOrExit(filename string) error {
|
||||
func (state *state) InitFromFile(filename string) error {
|
||||
data, err := os.ReadFile(common.ConfigPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Warn().Msg("config file not found, using default config")
|
||||
state.Config = *config.DefaultConfig()
|
||||
return nil
|
||||
} else {
|
||||
gperr.LogFatal("config init error", err)
|
||||
}
|
||||
state.Config = *config.DefaultConfig()
|
||||
return err
|
||||
}
|
||||
return state.Init(data)
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ import (
|
||||
)
|
||||
|
||||
type State interface {
|
||||
// InitFromFileOrExit logs and exits on critical errors, non-critical ones will be returned
|
||||
InitFromFileOrExit(filename string) error
|
||||
InitFromFile(filename string) error
|
||||
Init(data []byte) error
|
||||
|
||||
Task() *task.Task
|
||||
|
||||
Reference in New Issue
Block a user