setup.go 3.5 KB

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