mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 17:28:53 +02:00
fix(config): fix error and logging handling
This commit is contained in:
@@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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.`
|
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 {
|
func Load() error {
|
||||||
if HasState() {
|
if HasState() {
|
||||||
panic(errors.New("config already loaded"))
|
panic(errors.New("config already loaded"))
|
||||||
@@ -40,9 +59,15 @@ func Load() error {
|
|||||||
state := NewState()
|
state := NewState()
|
||||||
cfgWatcher = watcher.NewConfigFileWatcher(common.ConfigFileName)
|
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 {
|
if err != nil {
|
||||||
notifyError("init", err)
|
logNotifyError("init", err)
|
||||||
}
|
}
|
||||||
SetState(state)
|
SetState(state)
|
||||||
|
|
||||||
@@ -51,24 +76,16 @@ func Load() error {
|
|||||||
return nil
|
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 {
|
func Reload() gperr.Error {
|
||||||
// avoid race between config change and API reload request
|
// avoid race between config change and API reload request
|
||||||
reloadMu.Lock()
|
reloadMu.Lock()
|
||||||
defer reloadMu.Unlock()
|
defer reloadMu.Unlock()
|
||||||
|
|
||||||
newState := NewState()
|
newState := NewState()
|
||||||
err := newState.InitFromFileOrExit(common.ConfigPath)
|
err := newState.InitFromFile(common.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
newState.Task().FinishAndWait(err)
|
newState.Task().FinishAndWait(err)
|
||||||
notifyError("reload", err)
|
logNotifyError("reload", err)
|
||||||
return gperr.New(ansi.Warning("using last config")).With(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 {
|
if err := newState.StartProviders(); err != nil {
|
||||||
gperr.LogWarn("start providers error", err)
|
gperr.LogWarn("start providers error", err)
|
||||||
notifyError("start providers", err)
|
logNotifyError("start providers", err)
|
||||||
return nil // continue
|
return nil // continue
|
||||||
}
|
}
|
||||||
StartProxyServers()
|
StartProxyServers()
|
||||||
@@ -107,10 +124,10 @@ func OnConfigChange(ev []events.Event) {
|
|||||||
// just reload once and check the last event
|
// just reload once and check the last event
|
||||||
switch ev[len(ev)-1].Action {
|
switch ev[len(ev)-1].Action {
|
||||||
case events.ActionFileRenamed:
|
case events.ActionFileRenamed:
|
||||||
log.Warn().Msg(cfgRenameWarn)
|
logNotifyWarn("rename", errors.New(cfgRenameWarn))
|
||||||
return
|
return
|
||||||
case events.ActionFileDeleted:
|
case events.ActionFileDeleted:
|
||||||
log.Warn().Msg(cfgDeleteWarn)
|
logNotifyWarn("delete", errors.New(cfgDeleteWarn))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/puzpuzpuz/xsync/v4"
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/yusing/godoxy/agent/pkg/agent"
|
"github.com/yusing/godoxy/agent/pkg/agent"
|
||||||
"github.com/yusing/godoxy/internal/acl"
|
"github.com/yusing/godoxy/internal/acl"
|
||||||
"github.com/yusing/godoxy/internal/autocert"
|
"github.com/yusing/godoxy/internal/autocert"
|
||||||
@@ -91,16 +90,11 @@ func Value() *config.Config {
|
|||||||
return config.ActiveConfig.Load()
|
return config.ActiveConfig.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *state) InitFromFileOrExit(filename string) error {
|
func (state *state) InitFromFile(filename string) error {
|
||||||
data, err := os.ReadFile(common.ConfigPath)
|
data, err := os.ReadFile(common.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
state.Config = *config.DefaultConfig()
|
||||||
log.Warn().Msg("config file not found, using default config")
|
return err
|
||||||
state.Config = *config.DefaultConfig()
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
gperr.LogFatal("config init error", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return state.Init(data)
|
return state.Init(data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type State interface {
|
type State interface {
|
||||||
// InitFromFileOrExit logs and exits on critical errors, non-critical ones will be returned
|
InitFromFile(filename string) error
|
||||||
InitFromFileOrExit(filename string) error
|
|
||||||
Init(data []byte) error
|
Init(data []byte) error
|
||||||
|
|
||||||
Task() *task.Task
|
Task() *task.Task
|
||||||
|
|||||||
Reference in New Issue
Block a user