option.go 8.2 KB

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