option.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.OptionMap["NormalPrice"] = strconv.FormatFloat(common.NormalPrice, 'f', -1, 64)
  71. common.OptionMap["StablePrice"] = strconv.FormatFloat(common.StablePrice, 'f', -1, 64)
  72. common.OptionMap["BasePrice"] = strconv.FormatFloat(common.BasePrice, 'f', -1, 64)
  73. common.OptionMapRWMutex.Unlock()
  74. loadOptionsFromDatabase()
  75. }
  76. func loadOptionsFromDatabase() {
  77. options, _ := AllOption()
  78. for _, option := range options {
  79. err := updateOptionMap(option.Key, option.Value)
  80. if err != nil {
  81. common.SysError("failed to update option map: " + err.Error())
  82. }
  83. }
  84. }
  85. func SyncOptions(frequency int) {
  86. for {
  87. time.Sleep(time.Duration(frequency) * time.Second)
  88. common.SysLog("syncing options from database")
  89. loadOptionsFromDatabase()
  90. }
  91. }
  92. func UpdateOption(key string, value string) error {
  93. // Save to database first
  94. option := Option{
  95. Key: key,
  96. }
  97. // https://gorm.io/docs/update.html#Save-All-Fields
  98. DB.FirstOrCreate(&option, Option{Key: key})
  99. option.Value = value
  100. // Save is a combination function.
  101. // If save value does not contain primary key, it will execute Create,
  102. // otherwise it will execute Update (with all fields).
  103. DB.Save(&option)
  104. // Update OptionMap
  105. return updateOptionMap(key, value)
  106. }
  107. func updateOptionMap(key string, value string) (err error) {
  108. common.OptionMapRWMutex.Lock()
  109. defer common.OptionMapRWMutex.Unlock()
  110. common.OptionMap[key] = value
  111. if strings.HasSuffix(key, "Permission") {
  112. intValue, _ := strconv.Atoi(value)
  113. switch key {
  114. case "FileUploadPermission":
  115. common.FileUploadPermission = intValue
  116. case "FileDownloadPermission":
  117. common.FileDownloadPermission = intValue
  118. case "ImageUploadPermission":
  119. common.ImageUploadPermission = intValue
  120. case "ImageDownloadPermission":
  121. common.ImageDownloadPermission = intValue
  122. }
  123. }
  124. if strings.HasSuffix(key, "Enabled") {
  125. boolValue := value == "true"
  126. switch key {
  127. case "PasswordRegisterEnabled":
  128. common.PasswordRegisterEnabled = boolValue
  129. case "PasswordLoginEnabled":
  130. common.PasswordLoginEnabled = boolValue
  131. case "EmailVerificationEnabled":
  132. common.EmailVerificationEnabled = boolValue
  133. case "GitHubOAuthEnabled":
  134. common.GitHubOAuthEnabled = boolValue
  135. case "WeChatAuthEnabled":
  136. common.WeChatAuthEnabled = boolValue
  137. case "TurnstileCheckEnabled":
  138. common.TurnstileCheckEnabled = boolValue
  139. case "RegisterEnabled":
  140. common.RegisterEnabled = boolValue
  141. case "EmailDomainRestrictionEnabled":
  142. common.EmailDomainRestrictionEnabled = boolValue
  143. case "AutomaticDisableChannelEnabled":
  144. common.AutomaticDisableChannelEnabled = boolValue
  145. case "ApproximateTokenEnabled":
  146. common.ApproximateTokenEnabled = boolValue
  147. case "LogConsumeEnabled":
  148. common.LogConsumeEnabled = boolValue
  149. case "DisplayInCurrencyEnabled":
  150. common.DisplayInCurrencyEnabled = boolValue
  151. case "DisplayTokenStatEnabled":
  152. common.DisplayTokenStatEnabled = boolValue
  153. }
  154. }
  155. switch key {
  156. case "EmailDomainWhitelist":
  157. common.EmailDomainWhitelist = strings.Split(value, ",")
  158. case "SMTPServer":
  159. common.SMTPServer = value
  160. case "SMTPPort":
  161. intValue, _ := strconv.Atoi(value)
  162. common.SMTPPort = intValue
  163. case "SMTPAccount":
  164. common.SMTPAccount = value
  165. case "SMTPFrom":
  166. common.SMTPFrom = value
  167. case "SMTPToken":
  168. common.SMTPToken = value
  169. case "ServerAddress":
  170. common.ServerAddress = value
  171. case "GitHubClientId":
  172. common.GitHubClientId = value
  173. case "GitHubClientSecret":
  174. common.GitHubClientSecret = value
  175. case "Footer":
  176. common.Footer = value
  177. case "SystemName":
  178. common.SystemName = value
  179. case "Logo":
  180. common.Logo = value
  181. case "WeChatServerAddress":
  182. common.WeChatServerAddress = value
  183. case "WeChatServerToken":
  184. common.WeChatServerToken = value
  185. case "WeChatAccountQRCodeImageURL":
  186. common.WeChatAccountQRCodeImageURL = value
  187. case "TurnstileSiteKey":
  188. common.TurnstileSiteKey = value
  189. case "TurnstileSecretKey":
  190. common.TurnstileSecretKey = value
  191. case "QuotaForNewUser":
  192. common.QuotaForNewUser, _ = strconv.Atoi(value)
  193. case "QuotaForInviter":
  194. common.QuotaForInviter, _ = strconv.Atoi(value)
  195. case "QuotaForInvitee":
  196. common.QuotaForInvitee, _ = strconv.Atoi(value)
  197. case "QuotaRemindThreshold":
  198. common.QuotaRemindThreshold, _ = strconv.Atoi(value)
  199. case "PreConsumedQuota":
  200. common.PreConsumedQuota, _ = strconv.Atoi(value)
  201. case "RetryTimes":
  202. common.RetryTimes, _ = strconv.Atoi(value)
  203. case "ModelRatio":
  204. err = common.UpdateModelRatioByJSONString(value)
  205. case "GroupRatio":
  206. err = common.UpdateGroupRatioByJSONString(value)
  207. case "TopUpLink":
  208. common.TopUpLink = value
  209. case "ChatLink":
  210. common.ChatLink = value
  211. case "NormalPrice":
  212. common.NormalPrice, _ = strconv.ParseFloat(value, 64)
  213. case "BasePrice":
  214. common.BasePrice, _ = strconv.ParseFloat(value, 64)
  215. case "StablePrice":
  216. common.StablePrice, _ = strconv.ParseFloat(value, 64)
  217. case "ChannelDisableThreshold":
  218. common.ChannelDisableThreshold, _ = strconv.ParseFloat(value, 64)
  219. case "QuotaPerUnit":
  220. common.QuotaPerUnit, _ = strconv.ParseFloat(value, 64)
  221. }
  222. return err
  223. }