兼容配置导出工具的加载实现

This commit is contained in:
kercylan98 2023-05-19 13:47:49 +08:00
parent c9b53b9aed
commit defeac306a
1 changed files with 78 additions and 0 deletions

78
config/config.go Normal file
View File

@ -0,0 +1,78 @@
package config
import (
jsonIter "github.com/json-iterator/go"
"github.com/kercylan98/minotaur/utils/log"
"github.com/kercylan98/minotaur/utils/timer"
"go.uber.org/zap"
"os"
"path/filepath"
"sync"
"time"
)
type LoadHandle func(handle func(filename string, config any) error)
type RefreshHandle func()
const (
tickerLoadRefresh = "_tickerLoadRefresh"
)
var (
cLoadDir string
cTicker *timer.Ticker
cInterval time.Duration
cLoadHandle LoadHandle
cRefreshHandle RefreshHandle
json = jsonIter.ConfigCompatibleWithStandardLibrary
mutex sync.Mutex
)
func Init(loadDir string, loadHandle LoadHandle, refreshHandle RefreshHandle) {
cLoadDir = loadDir
cLoadHandle = loadHandle
cRefreshHandle = refreshHandle
}
func Load() {
mutex.Lock()
if cTicker != nil {
WithTickerLoad(cTicker, cInterval)
} else {
cLoadHandle(func(filename string, config any) error {
bytes, err := os.ReadFile(filepath.Join(cLoadDir, filename))
if err != nil {
return err
}
if err = json.Unmarshal(bytes, &config); err == nil {
log.Error("Config", zap.String("Name", filename), zap.Bool("LoadSuccess", true))
}
return err
})
}
mutex.Unlock()
}
func WithTickerLoad(ticker *timer.Ticker, interval time.Duration) {
if ticker != cTicker && cTicker != nil {
cTicker.StopTimer(tickerLoadRefresh)
}
cTicker = ticker
cInterval = interval
cTicker.Loop(tickerLoadRefresh, timer.Instantly, cInterval, timer.Forever, func() {
Load()
Refresh()
})
}
func StopTickerLoad() {
if cTicker != nil {
cTicker.StopTimer(tickerLoadRefresh)
}
}
func Refresh() {
mutex.Lock()
cRefreshHandle()
mutex.Unlock()
}