host.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package controller
  2. import (
  3. "strconv"
  4. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  5. "github.com/mhsanaei/3x-ui/v3/internal/web/middleware"
  6. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type HostController struct {
  10. hostService service.HostService
  11. }
  12. func NewHostController(g *gin.RouterGroup) *HostController {
  13. a := &HostController{}
  14. a.initRouter(g)
  15. return a
  16. }
  17. func (a *HostController) initRouter(g *gin.RouterGroup) {
  18. g.GET("/list", a.list)
  19. g.GET("/get/:groupId", a.get)
  20. g.GET("/byInbound/:inboundId", a.byInbound)
  21. g.GET("/tags", a.tags)
  22. g.POST("/add", a.add)
  23. g.POST("/update/:groupId", a.update)
  24. g.POST("/del/:groupId", a.del)
  25. g.POST("/setEnable/:groupId", a.setEnable)
  26. g.POST("/reorder", a.reorder)
  27. g.POST("/bulk/add", a.add)
  28. g.POST("/bulk/setEnable", a.bulkSetEnable)
  29. g.POST("/bulk/del", a.bulkDel)
  30. }
  31. func (a *HostController) list(c *gin.Context) {
  32. hosts, err := a.hostService.GetHosts()
  33. if err != nil {
  34. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.list"), err)
  35. return
  36. }
  37. jsonObj(c, hosts, nil)
  38. }
  39. func (a *HostController) get(c *gin.Context) {
  40. groupId := c.Param("groupId")
  41. h, err := a.hostService.GetHostGroup(groupId)
  42. if err != nil {
  43. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.obtain"), err)
  44. return
  45. }
  46. jsonObj(c, h, nil)
  47. }
  48. func (a *HostController) byInbound(c *gin.Context) {
  49. inboundId, err := strconv.Atoi(c.Param("inboundId"))
  50. if err != nil {
  51. jsonMsg(c, I18nWeb(c, "get"), err)
  52. return
  53. }
  54. hosts, err := a.hostService.GetHostsByInbound(inboundId)
  55. if err != nil {
  56. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.list"), err)
  57. return
  58. }
  59. jsonObj(c, hosts, nil)
  60. }
  61. func (a *HostController) tags(c *gin.Context) {
  62. tags, err := a.hostService.GetAllTags()
  63. if err != nil {
  64. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.list"), err)
  65. return
  66. }
  67. jsonObj(c, tags, nil)
  68. }
  69. func (a *HostController) add(c *gin.Context) {
  70. req, ok := middleware.BindJSONAndValidate[entity.HostGroup](c)
  71. if !ok {
  72. return
  73. }
  74. created, err := a.hostService.AddHostGroup(req)
  75. if err != nil {
  76. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.add"), err)
  77. return
  78. }
  79. jsonMsgObj(c, I18nWeb(c, "pages.hosts.toasts.add"), created, nil)
  80. }
  81. func (a *HostController) update(c *gin.Context) {
  82. groupId := c.Param("groupId")
  83. req, ok := middleware.BindJSONAndValidate[entity.HostGroup](c)
  84. if !ok {
  85. return
  86. }
  87. updated, err := a.hostService.UpdateHostGroup(groupId, req)
  88. if err != nil {
  89. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  90. return
  91. }
  92. jsonMsgObj(c, I18nWeb(c, "pages.hosts.toasts.update"), updated, nil)
  93. }
  94. func (a *HostController) del(c *gin.Context) {
  95. groupId := c.Param("groupId")
  96. if err := a.hostService.DeleteHostGroup(groupId); err != nil {
  97. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), err)
  98. return
  99. }
  100. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), nil)
  101. }
  102. func (a *HostController) setEnable(c *gin.Context) {
  103. groupId := c.Param("groupId")
  104. body := struct {
  105. Enable bool `json:"enable" form:"enable"`
  106. }{}
  107. if err := c.ShouldBind(&body); err != nil {
  108. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  109. return
  110. }
  111. if err := a.hostService.SetHostGroupEnable(groupId, body.Enable); err != nil {
  112. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  113. return
  114. }
  115. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), nil)
  116. }
  117. func (a *HostController) reorder(c *gin.Context) {
  118. var req struct {
  119. Ids []string `json:"ids" form:"ids"`
  120. }
  121. if err := c.ShouldBindJSON(&req); err != nil {
  122. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  123. return
  124. }
  125. if err := a.hostService.ReorderHostGroups(req.Ids); err != nil {
  126. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  127. return
  128. }
  129. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), nil)
  130. }
  131. func (a *HostController) bulkSetEnable(c *gin.Context) {
  132. var req struct {
  133. Ids []string `json:"ids" form:"ids"`
  134. Enable bool `json:"enable" form:"enable"`
  135. }
  136. if err := c.ShouldBindJSON(&req); err != nil {
  137. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  138. return
  139. }
  140. if err := a.hostService.SetHostsGroupEnable(req.Ids, req.Enable); err != nil {
  141. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  142. return
  143. }
  144. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), nil)
  145. }
  146. func (a *HostController) bulkDel(c *gin.Context) {
  147. var req struct {
  148. Ids []string `json:"ids" form:"ids"`
  149. }
  150. if err := c.ShouldBindJSON(&req); err != nil {
  151. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), err)
  152. return
  153. }
  154. if err := a.hostService.DeleteHostsGroup(req.Ids); err != nil {
  155. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), err)
  156. return
  157. }
  158. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), nil)
  159. }