setup.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "one-api/common"
  5. "one-api/constant"
  6. "one-api/model"
  7. "one-api/setting/operation_setting"
  8. "time"
  9. )
  10. type Setup struct {
  11. Status bool `json:"status"`
  12. RootInit bool `json:"root_init"`
  13. DatabaseType string `json:"database_type"`
  14. }
  15. type SetupRequest struct {
  16. Username string `json:"username"`
  17. Password string `json:"password"`
  18. ConfirmPassword string `json:"confirmPassword"`
  19. SelfUseModeEnabled bool `json:"SelfUseModeEnabled"`
  20. DemoSiteEnabled bool `json:"DemoSiteEnabled"`
  21. }
  22. func GetSetup(c *gin.Context) {
  23. setup := Setup{
  24. Status: constant.Setup,
  25. }
  26. if constant.Setup {
  27. c.JSON(200, gin.H{
  28. "success": true,
  29. "data": setup,
  30. })
  31. return
  32. }
  33. setup.RootInit = model.RootUserExists()
  34. if common.UsingMySQL {
  35. setup.DatabaseType = "mysql"
  36. }
  37. if common.UsingPostgreSQL {
  38. setup.DatabaseType = "postgres"
  39. }
  40. if common.UsingSQLite {
  41. setup.DatabaseType = "sqlite"
  42. }
  43. c.JSON(200, gin.H{
  44. "success": true,
  45. "data": setup,
  46. })
  47. }
  48. func PostSetup(c *gin.Context) {
  49. // Check if setup is already completed
  50. if constant.Setup {
  51. c.JSON(400, gin.H{
  52. "success": false,
  53. "message": "系统已经初始化完成",
  54. })
  55. return
  56. }
  57. // Check if root user already exists
  58. rootExists := model.RootUserExists()
  59. var req SetupRequest
  60. err := c.ShouldBindJSON(&req)
  61. if err != nil {
  62. c.JSON(400, gin.H{
  63. "success": false,
  64. "message": "请求参数有误",
  65. })
  66. return
  67. }
  68. // If root doesn't exist, validate and create admin account
  69. if !rootExists {
  70. // Validate username length: max 12 characters to align with model.User validation
  71. if len(req.Username) > 12 {
  72. c.JSON(400, gin.H{
  73. "success": false,
  74. "message": "用户名长度不能超过12个字符",
  75. })
  76. return
  77. }
  78. // Validate password
  79. if req.Password != req.ConfirmPassword {
  80. c.JSON(400, gin.H{
  81. "success": false,
  82. "message": "两次输入的密码不一致",
  83. })
  84. return
  85. }
  86. if len(req.Password) < 8 {
  87. c.JSON(400, gin.H{
  88. "success": false,
  89. "message": "密码长度至少为8个字符",
  90. })
  91. return
  92. }
  93. // Create root user
  94. hashedPassword, err := common.Password2Hash(req.Password)
  95. if err != nil {
  96. c.JSON(500, gin.H{
  97. "success": false,
  98. "message": "系统错误: " + err.Error(),
  99. })
  100. return
  101. }
  102. rootUser := model.User{
  103. Username: req.Username,
  104. Password: hashedPassword,
  105. Role: common.RoleRootUser,
  106. Status: common.UserStatusEnabled,
  107. DisplayName: "Root User",
  108. AccessToken: nil,
  109. Quota: 100000000,
  110. }
  111. err = model.DB.Create(&rootUser).Error
  112. if err != nil {
  113. c.JSON(500, gin.H{
  114. "success": false,
  115. "message": "创建管理员账号失败: " + err.Error(),
  116. })
  117. return
  118. }
  119. }
  120. // Set operation modes
  121. operation_setting.SelfUseModeEnabled = req.SelfUseModeEnabled
  122. operation_setting.DemoSiteEnabled = req.DemoSiteEnabled
  123. // Save operation modes to database for persistence
  124. err = model.UpdateOption("SelfUseModeEnabled", boolToString(req.SelfUseModeEnabled))
  125. if err != nil {
  126. c.JSON(500, gin.H{
  127. "success": false,
  128. "message": "保存自用模式设置失败: " + err.Error(),
  129. })
  130. return
  131. }
  132. err = model.UpdateOption("DemoSiteEnabled", boolToString(req.DemoSiteEnabled))
  133. if err != nil {
  134. c.JSON(500, gin.H{
  135. "success": false,
  136. "message": "保存演示站点模式设置失败: " + err.Error(),
  137. })
  138. return
  139. }
  140. // Update setup status
  141. constant.Setup = true
  142. setup := model.Setup{
  143. Version: common.Version,
  144. InitializedAt: time.Now().Unix(),
  145. }
  146. err = model.DB.Create(&setup).Error
  147. if err != nil {
  148. c.JSON(500, gin.H{
  149. "success": false,
  150. "message": "系统初始化失败: " + err.Error(),
  151. })
  152. return
  153. }
  154. c.JSON(200, gin.H{
  155. "success": true,
  156. "message": "系统初始化成功",
  157. })
  158. }
  159. func boolToString(b bool) string {
  160. if b {
  161. return "true"
  162. }
  163. return "false"
  164. }