ai-gateway/model/option.go

214 lines
7.7 KiB
Go
Raw Normal View History

2023-04-22 12:39:27 +00:00
package model
import (
2023-04-22 13:14:09 +00:00
"one-api/common"
2024-01-21 15:18:32 +00:00
"one-api/common/config"
2024-01-21 14:56:20 +00:00
"one-api/common/logger"
2023-04-22 12:39:27 +00:00
"strconv"
"strings"
"time"
2023-04-22 12:39:27 +00:00
)
type Option struct {
Key string `json:"key" gorm:"primaryKey"`
Value string `json:"value"`
}
func AllOption() ([]*Option, error) {
var options []*Option
var err error
err = DB.Find(&options).Error
return options, err
}
func InitOptionMap() {
2024-01-21 15:18:32 +00:00
config.OptionMapRWMutex.Lock()
config.OptionMap = make(map[string]string)
config.OptionMap["PasswordLoginEnabled"] = strconv.FormatBool(config.PasswordLoginEnabled)
config.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(config.PasswordRegisterEnabled)
config.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(config.EmailVerificationEnabled)
config.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(config.GitHubOAuthEnabled)
config.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(config.WeChatAuthEnabled)
config.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(config.TurnstileCheckEnabled)
config.OptionMap["RegisterEnabled"] = strconv.FormatBool(config.RegisterEnabled)
config.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(config.AutomaticDisableChannelEnabled)
config.OptionMap["AutomaticEnableChannelEnabled"] = strconv.FormatBool(config.AutomaticEnableChannelEnabled)
config.OptionMap["ApproximateTokenEnabled"] = strconv.FormatBool(config.ApproximateTokenEnabled)
config.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(config.LogConsumeEnabled)
config.OptionMap["DisplayInCurrencyEnabled"] = strconv.FormatBool(config.DisplayInCurrencyEnabled)
config.OptionMap["DisplayTokenStatEnabled"] = strconv.FormatBool(config.DisplayTokenStatEnabled)
config.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(config.ChannelDisableThreshold, 'f', -1, 64)
config.OptionMap["EmailDomainRestrictionEnabled"] = strconv.FormatBool(config.EmailDomainRestrictionEnabled)
config.OptionMap["EmailDomainWhitelist"] = strings.Join(config.EmailDomainWhitelist, ",")
config.OptionMap["SMTPServer"] = ""
config.OptionMap["SMTPFrom"] = ""
config.OptionMap["SMTPPort"] = strconv.Itoa(config.SMTPPort)
config.OptionMap["SMTPAccount"] = ""
config.OptionMap["SMTPToken"] = ""
config.OptionMap["Notice"] = ""
config.OptionMap["About"] = ""
config.OptionMap["HomePageContent"] = ""
config.OptionMap["Footer"] = config.Footer
config.OptionMap["SystemName"] = config.SystemName
config.OptionMap["Logo"] = config.Logo
config.OptionMap["ServerAddress"] = ""
config.OptionMap["GitHubClientId"] = ""
config.OptionMap["GitHubClientSecret"] = ""
config.OptionMap["WeChatServerAddress"] = ""
config.OptionMap["WeChatServerToken"] = ""
config.OptionMap["WeChatAccountQRCodeImageURL"] = ""
config.OptionMap["TurnstileSiteKey"] = ""
config.OptionMap["TurnstileSecretKey"] = ""
config.OptionMap["QuotaForNewUser"] = strconv.Itoa(config.QuotaForNewUser)
config.OptionMap["QuotaForInviter"] = strconv.Itoa(config.QuotaForInviter)
config.OptionMap["QuotaForInvitee"] = strconv.Itoa(config.QuotaForInvitee)
config.OptionMap["QuotaRemindThreshold"] = strconv.Itoa(config.QuotaRemindThreshold)
config.OptionMap["PreConsumedQuota"] = strconv.Itoa(config.PreConsumedQuota)
config.OptionMap["ModelRatio"] = common.ModelRatio2JSONString()
config.OptionMap["GroupRatio"] = common.GroupRatio2JSONString()
config.OptionMap["TopUpLink"] = config.TopUpLink
config.OptionMap["ChatLink"] = config.ChatLink
config.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(config.QuotaPerUnit, 'f', -1, 64)
config.OptionMap["RetryTimes"] = strconv.Itoa(config.RetryTimes)
config.OptionMap["Theme"] = config.Theme
config.OptionMapRWMutex.Unlock()
loadOptionsFromDatabase()
}
func loadOptionsFromDatabase() {
2023-04-22 12:39:27 +00:00
options, _ := AllOption()
for _, option := range options {
err := updateOptionMap(option.Key, option.Value)
if err != nil {
2024-01-21 14:56:20 +00:00
logger.SysError("failed to update option map: " + err.Error())
}
2023-04-22 12:39:27 +00:00
}
}
func SyncOptions(frequency int) {
for {
time.Sleep(time.Duration(frequency) * time.Second)
2024-01-21 14:56:20 +00:00
logger.SysLog("syncing options from database")
loadOptionsFromDatabase()
}
}
2023-04-22 12:39:27 +00:00
func UpdateOption(key string, value string) error {
// Save to database first
option := Option{
Key: key,
}
// https://gorm.io/docs/update.html#Save-All-Fields
DB.FirstOrCreate(&option, Option{Key: key})
option.Value = value
// Save is a combination function.
// If save value does not contain primary key, it will execute Create,
// otherwise it will execute Update (with all fields).
DB.Save(&option)
// Update OptionMap
return updateOptionMap(key, value)
2023-04-22 12:39:27 +00:00
}
func updateOptionMap(key string, value string) (err error) {
2024-01-21 15:18:32 +00:00
config.OptionMapRWMutex.Lock()
defer config.OptionMapRWMutex.Unlock()
config.OptionMap[key] = value
2023-04-22 12:39:27 +00:00
if strings.HasSuffix(key, "Enabled") {
boolValue := value == "true"
switch key {
case "PasswordRegisterEnabled":
2024-01-21 15:18:32 +00:00
config.PasswordRegisterEnabled = boolValue
2023-04-22 12:39:27 +00:00
case "PasswordLoginEnabled":
2024-01-21 15:18:32 +00:00
config.PasswordLoginEnabled = boolValue
2023-04-22 12:39:27 +00:00
case "EmailVerificationEnabled":
2024-01-21 15:18:32 +00:00
config.EmailVerificationEnabled = boolValue
2023-04-22 12:39:27 +00:00
case "GitHubOAuthEnabled":
2024-01-21 15:18:32 +00:00
config.GitHubOAuthEnabled = boolValue
2023-04-22 12:39:27 +00:00
case "WeChatAuthEnabled":
2024-01-21 15:18:32 +00:00
config.WeChatAuthEnabled = boolValue
2023-04-22 12:39:27 +00:00
case "TurnstileCheckEnabled":
2024-01-21 15:18:32 +00:00
config.TurnstileCheckEnabled = boolValue
2023-04-22 12:39:27 +00:00
case "RegisterEnabled":
2024-01-21 15:18:32 +00:00
config.RegisterEnabled = boolValue
case "EmailDomainRestrictionEnabled":
2024-01-21 15:18:32 +00:00
config.EmailDomainRestrictionEnabled = boolValue
case "AutomaticDisableChannelEnabled":
2024-01-21 15:18:32 +00:00
config.AutomaticDisableChannelEnabled = boolValue
case "AutomaticEnableChannelEnabled":
2024-01-21 15:18:32 +00:00
config.AutomaticEnableChannelEnabled = boolValue
case "ApproximateTokenEnabled":
2024-01-21 15:18:32 +00:00
config.ApproximateTokenEnabled = boolValue
case "LogConsumeEnabled":
2024-01-21 15:18:32 +00:00
config.LogConsumeEnabled = boolValue
2023-06-20 12:09:17 +00:00
case "DisplayInCurrencyEnabled":
2024-01-21 15:18:32 +00:00
config.DisplayInCurrencyEnabled = boolValue
case "DisplayTokenStatEnabled":
2024-01-21 15:18:32 +00:00
config.DisplayTokenStatEnabled = boolValue
2023-04-22 12:39:27 +00:00
}
}
switch key {
case "EmailDomainWhitelist":
2024-01-21 15:18:32 +00:00
config.EmailDomainWhitelist = strings.Split(value, ",")
2023-04-22 12:39:27 +00:00
case "SMTPServer":
2024-01-21 15:18:32 +00:00
config.SMTPServer = value
2023-05-12 03:44:38 +00:00
case "SMTPPort":
intValue, _ := strconv.Atoi(value)
2024-01-21 15:18:32 +00:00
config.SMTPPort = intValue
2023-04-22 12:39:27 +00:00
case "SMTPAccount":
2024-01-21 15:18:32 +00:00
config.SMTPAccount = value
case "SMTPFrom":
2024-01-21 15:18:32 +00:00
config.SMTPFrom = value
2023-04-22 12:39:27 +00:00
case "SMTPToken":
2024-01-21 15:18:32 +00:00
config.SMTPToken = value
2023-04-22 12:39:27 +00:00
case "ServerAddress":
2024-01-21 15:18:32 +00:00
config.ServerAddress = value
2023-04-22 12:39:27 +00:00
case "GitHubClientId":
2024-01-21 15:18:32 +00:00
config.GitHubClientId = value
2023-04-22 12:39:27 +00:00
case "GitHubClientSecret":
2024-01-21 15:18:32 +00:00
config.GitHubClientSecret = value
2023-04-22 12:39:27 +00:00
case "Footer":
2024-01-21 15:18:32 +00:00
config.Footer = value
case "SystemName":
2024-01-21 15:18:32 +00:00
config.SystemName = value
case "Logo":
2024-01-21 15:18:32 +00:00
config.Logo = value
2023-04-22 12:39:27 +00:00
case "WeChatServerAddress":
2024-01-21 15:18:32 +00:00
config.WeChatServerAddress = value
2023-04-22 12:39:27 +00:00
case "WeChatServerToken":
2024-01-21 15:18:32 +00:00
config.WeChatServerToken = value
2023-04-22 12:39:27 +00:00
case "WeChatAccountQRCodeImageURL":
2024-01-21 15:18:32 +00:00
config.WeChatAccountQRCodeImageURL = value
2023-04-22 12:39:27 +00:00
case "TurnstileSiteKey":
2024-01-21 15:18:32 +00:00
config.TurnstileSiteKey = value
2023-04-22 12:39:27 +00:00
case "TurnstileSecretKey":
2024-01-21 15:18:32 +00:00
config.TurnstileSecretKey = value
case "QuotaForNewUser":
2024-01-21 15:18:32 +00:00
config.QuotaForNewUser, _ = strconv.Atoi(value)
2023-06-17 10:12:58 +00:00
case "QuotaForInviter":
2024-01-21 15:18:32 +00:00
config.QuotaForInviter, _ = strconv.Atoi(value)
2023-06-17 10:12:58 +00:00
case "QuotaForInvitee":
2024-01-21 15:18:32 +00:00
config.QuotaForInvitee, _ = strconv.Atoi(value)
case "QuotaRemindThreshold":
2024-01-21 15:18:32 +00:00
config.QuotaRemindThreshold, _ = strconv.Atoi(value)
case "PreConsumedQuota":
2024-01-21 15:18:32 +00:00
config.PreConsumedQuota, _ = strconv.Atoi(value)
2023-07-15 11:06:51 +00:00
case "RetryTimes":
2024-01-21 15:18:32 +00:00
config.RetryTimes, _ = strconv.Atoi(value)
case "ModelRatio":
err = common.UpdateModelRatioByJSONString(value)
case "GroupRatio":
err = common.UpdateGroupRatioByJSONString(value)
2023-04-27 08:32:21 +00:00
case "TopUpLink":
2024-01-21 15:18:32 +00:00
config.TopUpLink = value
case "ChatLink":
2024-01-21 15:18:32 +00:00
config.ChatLink = value
case "ChannelDisableThreshold":
2024-01-21 15:18:32 +00:00
config.ChannelDisableThreshold, _ = strconv.ParseFloat(value, 64)
2023-06-20 12:09:17 +00:00
case "QuotaPerUnit":
2024-01-21 15:18:32 +00:00
config.QuotaPerUnit, _ = strconv.ParseFloat(value, 64)
2024-01-07 09:53:05 +00:00
case "Theme":
2024-01-21 15:18:32 +00:00
config.Theme = value
2023-04-22 12:39:27 +00:00
}
return err
2023-04-22 12:39:27 +00:00
}