feat: now slave server can sync options with master server (close #88)

This commit is contained in:
JustSong
2023-05-18 12:48:20 +08:00
parent 3c6834a79c
commit 2d39a135f2
3 changed files with 39 additions and 8 deletions

View File

@@ -4,6 +4,7 @@ import (
"one-api/common"
"strconv"
"strings"
"time"
)
type Option struct {
@@ -59,6 +60,10 @@ func InitOptionMap() {
common.OptionMap["ModelRatio"] = common.ModelRatio2JSONString()
common.OptionMap["TopUpLink"] = common.TopUpLink
common.OptionMapRWMutex.Unlock()
loadOptionsFromDatabase()
}
func loadOptionsFromDatabase() {
options, _ := AllOption()
for _, option := range options {
err := updateOptionMap(option.Key, option.Value)
@@ -68,6 +73,14 @@ func InitOptionMap() {
}
}
func SyncOptions(frequency int) {
for {
time.Sleep(time.Duration(frequency) * time.Second)
common.SysLog("Syncing options from database")
loadOptionsFromDatabase()
}
}
func UpdateOption(key string, value string) error {
// Save to database first
option := Option{