option.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package model
  2. import (
  3. "one-api/common"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. type Option struct {
  9. Key string `json:"key" gorm:"primaryKey"`
  10. Value string `json:"value"`
  11. }
  12. func AllOption() ([]*Option, error) {
  13. var options []*Option
  14. var err error
  15. err = DB.Find(&options).Error
  16. return options, err
  17. }
  18. func InitOptionMap() {
  19. common.OptionMapRWMutex.Lock()
  20. common.OptionMap = make(map[string]string)
  21. common.OptionMap["FileUploadPermission"] = strconv.Itoa(common.FileUploadPermission)
  22. common.OptionMap["FileDownloadPermission"] = strconv.Itoa(common.FileDownloadPermission)
  23. common.OptionMap["ImageUploadPermission"] = strconv.Itoa(common.ImageUploadPermission)
  24. common.OptionMap["ImageDownloadPermission"] = strconv.Itoa(common.ImageDownloadPermission)
  25. common.OptionMap["PasswordLoginEnabled"] = strconv.FormatBool(common.PasswordLoginEnabled)
  26. common.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(common.PasswordRegisterEnabled)
  27. common.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(common.EmailVerificationEnabled)
  28. common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
  29. common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
  30. common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
  31. common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
  32. common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled)
  33. common.OptionMap["ApproximateTokenEnabled"] = strconv.FormatBool(common.ApproximateTokenEnabled)
  34. common.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(common.LogConsumeEnabled)
  35. common.OptionMap["DisplayInCurrencyEnabled"] = strconv.FormatBool(common.DisplayInCurrencyEnabled)
  36. common.OptionMap["DisplayTokenStatEnabled"] = strconv.FormatBool(common.DisplayTokenStatEnabled)
  37. common.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(common.ChannelDisableThreshold, 'f', -1, 64)
  38. common.OptionMap["EmailDomainRestrictionEnabled"] = strconv.FormatBool(common.EmailDomainRestrictionEnabled)
  39. common.OptionMap["EmailDomainWhitelist"] = strings.Join(common.EmailDomainWhitelist, ",")
  40. common.OptionMap["SMTPServer"] = ""
  41. common.OptionMap["SMTPFrom"] = ""
  42. common.OptionMap["SMTPPort"] = strconv.Itoa(common.SMTPPort)
  43. common.OptionMap["SMTPAccount"] = ""
  44. common.OptionMap["SMTPToken"] = ""
  45. common.OptionMap["Notice"] = ""
  46. common.OptionMap["About"] = ""
  47. common.OptionMap["HomePageContent"] = ""
  48. common.OptionMap["Footer"] = common.Footer
  49. common.OptionMap["SystemName"] = common.SystemName
  50. common.OptionMap["Logo"] = common.Logo
  51. common.OptionMap["ServerAddress"] = ""
  52. common.OptionMap["GitHubClientId"] = ""
  53. common.OptionMap["GitHubClientSecret"] = ""
  54. common.OptionMap["WeChatServerAddress"] = ""
  55. common.OptionMap["WeChatServerToken"] = ""
  56. common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
  57. common.OptionMap["TurnstileSiteKey"] = ""
  58. common.OptionMap["TurnstileSecretKey"] = ""
  59. common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
  60. common.OptionMap["QuotaForInviter"] = strconv.Itoa(common.QuotaForInviter)
  61. common.OptionMap["QuotaForInvitee"] = strconv.Itoa(common.QuotaForInvitee)
  62. common.OptionMap["QuotaRemindThreshold"] = strconv.Itoa(common.QuotaRemindThreshold)
  63. common.OptionMap["PreConsumedQuota"] = strconv.Itoa(common.PreConsumedQuota)
  64. common.OptionMap["ModelRatio"] = common.ModelRatio2JSONString()
  65. common.OptionMap["GroupRatio"] = common.GroupRatio2JSONString()
  66. common.OptionMap["TopUpLink"] = common.TopUpLink
  67. common.OptionMap["ChatLink"] = common.ChatLink
  68. common.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(common.QuotaPerUnit, 'f', -1, 64)
  69. common.OptionMap["RetryTimes"] = strconv.Itoa(common.RetryTimes)
  70. common.OptionMapRWMutex.Unlock()
  71. loadOptionsFromDatabase()
  72. }
  73. func loadOptionsFromDatabase() {
  74. options, _ := AllOption()
  75. for _, option := range options {
  76. err := updateOptionMap(option.Key, option.Value)
  77. if err != nil {
  78. common.SysError("failed to update option map: " + err.Error())
  79. }
  80. }
  81. }
  82. func SyncOptions(frequency int) {
  83. for {
  84. time.Sleep(time.Duration(frequency) * time.Second)
  85. common.SysLog("syncing options from database")
  86. loadOptionsFromDatabase()
  87. }
  88. }
  89. func UpdateOption(key string, value string) error {
  90. // Save to database first
  91. option := Option{
  92. Key: key,
  93. }
  94. // https://gorm.io/docs/update.html#Save-All-Fields
  95. DB.FirstOrCreate(&option, Option{Key: key})
  96. option.Value = value
  97. // Save is a combination function.
  98. // If save value does not contain primary key, it will execute Create,
  99. // otherwise it will execute Update (with all fields).
  100. DB.Save(&option)
  101. // Update OptionMap
  102. return updateOptionMap(key, value)
  103. }
  104. func updateOptionMap(key string, value string) (err error) {
  105. common.OptionMapRWMutex.Lock()
  106. defer common.OptionMapRWMutex.Unlock()
  107. common.OptionMap[key] = value
  108. if strings.HasSuffix(key, "Permission") {
  109. intValue, _ := strconv.Atoi(value)
  110. switch key {
  111. case "FileUploadPermission":
  112. common.FileUploadPermission = intValue
  113. case "FileDownloadPermission":
  114. common.FileDownloadPermission = intValue
  115. case "ImageUploadPermission":
  116. common.ImageUploadPermission = intValue
  117. case "ImageDownloadPermission":
  118. common.ImageDownloadPermission = intValue
  119. }
  120. }
  121. if strings.HasSuffix(key, "Enabled") {
  122. boolValue := value == "true"
  123. switch key {
  124. case "PasswordRegisterEnabled":
  125. common.PasswordRegisterEnabled = boolValue
  126. case "PasswordLoginEnabled":
  127. common.PasswordLoginEnabled = boolValue
  128. case "EmailVerificationEnabled":
  129. common.EmailVerificationEnabled = boolValue
  130. case "GitHubOAuthEnabled":
  131. common.GitHubOAuthEnabled = boolValue
  132. case "WeChatAuthEnabled":
  133. common.WeChatAuthEnabled = boolValue
  134. case "TurnstileCheckEnabled":
  135. common.TurnstileCheckEnabled = boolValue
  136. case "RegisterEnabled":
  137. common.RegisterEnabled = boolValue
  138. case "EmailDomainRestrictionEnabled":
  139. common.EmailDomainRestrictionEnabled = boolValue
  140. case "AutomaticDisableChannelEnabled":
  141. common.AutomaticDisableChannelEnabled = boolValue
  142. case "ApproximateTokenEnabled":
  143. common.ApproximateTokenEnabled = boolValue
  144. case "LogConsumeEnabled":
  145. common.LogConsumeEnabled = boolValue
  146. case "DisplayInCurrencyEnabled":
  147. common.DisplayInCurrencyEnabled = boolValue
  148. case "DisplayTokenStatEnabled":
  149. common.DisplayTokenStatEnabled = boolValue
  150. }
  151. }
  152. switch key {
  153. case "EmailDomainWhitelist":
  154. common.EmailDomainWhitelist = strings.Split(value, ",")
  155. case "SMTPServer":
  156. common.SMTPServer = value
  157. case "SMTPPort":
  158. intValue, _ := strconv.Atoi(value)
  159. common.SMTPPort = intValue
  160. case "SMTPAccount":
  161. common.SMTPAccount = value
  162. case "SMTPFrom":
  163. common.SMTPFrom = value
  164. case "SMTPToken":
  165. common.SMTPToken = value
  166. case "ServerAddress":
  167. common.ServerAddress = value
  168. case "GitHubClientId":
  169. common.GitHubClientId = value
  170. case "GitHubClientSecret":
  171. common.GitHubClientSecret = value
  172. case "Footer":
  173. common.Footer = value
  174. case "SystemName":
  175. common.SystemName = value
  176. case "Logo":
  177. common.Logo = value
  178. case "WeChatServerAddress":
  179. common.WeChatServerAddress = value
  180. case "WeChatServerToken":
  181. common.WeChatServerToken = value
  182. case "WeChatAccountQRCodeImageURL":
  183. common.WeChatAccountQRCodeImageURL = value
  184. case "TurnstileSiteKey":
  185. common.TurnstileSiteKey = value
  186. case "TurnstileSecretKey":
  187. common.TurnstileSecretKey = value
  188. case "QuotaForNewUser":
  189. common.QuotaForNewUser, _ = strconv.Atoi(value)
  190. case "QuotaForInviter":
  191. common.QuotaForInviter, _ = strconv.Atoi(value)
  192. case "QuotaForInvitee":
  193. common.QuotaForInvitee, _ = strconv.Atoi(value)
  194. case "QuotaRemindThreshold":
  195. common.QuotaRemindThreshold, _ = strconv.Atoi(value)
  196. case "PreConsumedQuota":
  197. common.PreConsumedQuota, _ = strconv.Atoi(value)
  198. case "RetryTimes":
  199. common.RetryTimes, _ = strconv.Atoi(value)
  200. case "ModelRatio":
  201. err = common.UpdateModelRatioByJSONString(value)
  202. case "GroupRatio":
  203. err = common.UpdateGroupRatioByJSONString(value)
  204. case "TopUpLink":
  205. common.TopUpLink = value
  206. case "ChatLink":
  207. common.ChatLink = value
  208. case "ChannelDisableThreshold":
  209. common.ChannelDisableThreshold, _ = strconv.ParseFloat(value, 64)
  210. case "QuotaPerUnit":
  211. common.QuotaPerUnit, _ = strconv.ParseFloat(value, 64)
  212. }
  213. return err
  214. }