setup.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 password
  71. if req.Password != req.ConfirmPassword {
  72. c.JSON(400, gin.H{
  73. "success": false,
  74. "message": "两次输入的密码不一致",
  75. })
  76. return
  77. }
  78. if len(req.Password) < 8 {
  79. c.JSON(400, gin.H{
  80. "success": false,
  81. "message": "密码长度至少为8个字符",
  82. })
  83. return
  84. }
  85. // Create root user
  86. hashedPassword, err := common.Password2Hash(req.Password)
  87. if err != nil {
  88. c.JSON(500, gin.H{
  89. "success": false,
  90. "message": "系统错误: " + err.Error(),
  91. })
  92. return
  93. }
  94. rootUser := model.User{
  95. Username: req.Username,
  96. Password: hashedPassword,
  97. Role: common.RoleRootUser,
  98. Status: common.UserStatusEnabled,
  99. DisplayName: "Root User",
  100. AccessToken: nil,
  101. Quota: 100000000,
  102. }
  103. err = model.DB.Create(&rootUser).Error
  104. if err != nil {
  105. c.JSON(500, gin.H{
  106. "success": false,
  107. "message": "创建管理员账号失败: " + err.Error(),
  108. })
  109. return
  110. }
  111. }
  112. // Set operation modes
  113. operation_setting.SelfUseModeEnabled = req.SelfUseModeEnabled
  114. operation_setting.DemoSiteEnabled = req.DemoSiteEnabled
  115. // Save operation modes to database for persistence
  116. err = model.UpdateOption("SelfUseModeEnabled", boolToString(req.SelfUseModeEnabled))
  117. if err != nil {
  118. c.JSON(500, gin.H{
  119. "success": false,
  120. "message": "保存自用模式设置失败: " + err.Error(),
  121. })
  122. return
  123. }
  124. err = model.UpdateOption("DemoSiteEnabled", boolToString(req.DemoSiteEnabled))
  125. if err != nil {
  126. c.JSON(500, gin.H{
  127. "success": false,
  128. "message": "保存演示站点模式设置失败: " + err.Error(),
  129. })
  130. return
  131. }
  132. // Update setup status
  133. constant.Setup = true
  134. setup := model.Setup{
  135. Version: common.Version,
  136. InitializedAt: time.Now().Unix(),
  137. }
  138. err = model.DB.Create(&setup).Error
  139. if err != nil {
  140. c.JSON(500, gin.H{
  141. "success": false,
  142. "message": "系统初始化失败: " + err.Error(),
  143. })
  144. return
  145. }
  146. c.JSON(200, gin.H{
  147. "success": true,
  148. "message": "系统初始化成功",
  149. })
  150. }
  151. func boolToString(b bool) string {
  152. if b {
  153. return "true"
  154. }
  155. return "false"
  156. }