1
0

host.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package controller
  2. import (
  3. "strconv"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  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. // HostController exposes CRUD + ordering for Host override endpoints under
  10. // /panel/api/hosts. Thin HTTP layer over HostService; mirrors NodeController.
  11. type HostController struct {
  12. hostService service.HostService
  13. }
  14. func NewHostController(g *gin.RouterGroup) *HostController {
  15. a := &HostController{}
  16. a.initRouter(g)
  17. return a
  18. }
  19. func (a *HostController) initRouter(g *gin.RouterGroup) {
  20. g.GET("/list", a.list)
  21. g.GET("/get/:id", a.get)
  22. g.GET("/byInbound/:inboundId", a.byInbound)
  23. g.GET("/tags", a.tags)
  24. g.POST("/add", a.add)
  25. g.POST("/update/:id", a.update)
  26. g.POST("/del/:id", a.del)
  27. g.POST("/setEnable/:id", a.setEnable)
  28. g.POST("/reorder", a.reorder)
  29. g.POST("/bulk/setEnable", a.bulkSetEnable)
  30. g.POST("/bulk/del", a.bulkDel)
  31. }
  32. func (a *HostController) list(c *gin.Context) {
  33. hosts, err := a.hostService.GetHosts()
  34. if err != nil {
  35. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.list"), err)
  36. return
  37. }
  38. jsonObj(c, hosts, nil)
  39. }
  40. func (a *HostController) get(c *gin.Context) {
  41. id, err := strconv.Atoi(c.Param("id"))
  42. if err != nil {
  43. jsonMsg(c, I18nWeb(c, "get"), err)
  44. return
  45. }
  46. h, err := a.hostService.GetHost(id)
  47. if err != nil {
  48. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.obtain"), err)
  49. return
  50. }
  51. jsonObj(c, h, nil)
  52. }
  53. func (a *HostController) byInbound(c *gin.Context) {
  54. inboundId, err := strconv.Atoi(c.Param("inboundId"))
  55. if err != nil {
  56. jsonMsg(c, I18nWeb(c, "get"), err)
  57. return
  58. }
  59. hosts, err := a.hostService.GetHostsByInbound(inboundId)
  60. if err != nil {
  61. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.list"), err)
  62. return
  63. }
  64. jsonObj(c, hosts, nil)
  65. }
  66. func (a *HostController) tags(c *gin.Context) {
  67. tags, err := a.hostService.GetAllTags()
  68. if err != nil {
  69. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.list"), err)
  70. return
  71. }
  72. jsonObj(c, tags, nil)
  73. }
  74. func (a *HostController) add(c *gin.Context) {
  75. h, ok := middleware.BindAndValidate[model.Host](c)
  76. if !ok {
  77. return
  78. }
  79. created, err := a.hostService.AddHost(h)
  80. if err != nil {
  81. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.add"), err)
  82. return
  83. }
  84. jsonMsgObj(c, I18nWeb(c, "pages.hosts.toasts.add"), created, nil)
  85. }
  86. func (a *HostController) update(c *gin.Context) {
  87. id, err := strconv.Atoi(c.Param("id"))
  88. if err != nil {
  89. jsonMsg(c, I18nWeb(c, "get"), err)
  90. return
  91. }
  92. h, ok := middleware.BindAndValidate[model.Host](c)
  93. if !ok {
  94. return
  95. }
  96. updated, err := a.hostService.UpdateHost(id, h)
  97. if err != nil {
  98. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  99. return
  100. }
  101. jsonMsgObj(c, I18nWeb(c, "pages.hosts.toasts.update"), updated, nil)
  102. }
  103. func (a *HostController) del(c *gin.Context) {
  104. id, err := strconv.Atoi(c.Param("id"))
  105. if err != nil {
  106. jsonMsg(c, I18nWeb(c, "get"), err)
  107. return
  108. }
  109. if err := a.hostService.DeleteHost(id); err != nil {
  110. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), err)
  111. return
  112. }
  113. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), nil)
  114. }
  115. func (a *HostController) setEnable(c *gin.Context) {
  116. id, err := strconv.Atoi(c.Param("id"))
  117. if err != nil {
  118. jsonMsg(c, I18nWeb(c, "get"), err)
  119. return
  120. }
  121. body := struct {
  122. Enable bool `json:"enable" form:"enable"`
  123. }{}
  124. if err := c.ShouldBind(&body); err != nil {
  125. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  126. return
  127. }
  128. if err := a.hostService.SetHostEnable(id, body.Enable); err != nil {
  129. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  130. return
  131. }
  132. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), nil)
  133. }
  134. func (a *HostController) reorder(c *gin.Context) {
  135. var req struct {
  136. Ids []int `json:"ids" form:"ids"`
  137. }
  138. if err := c.ShouldBindJSON(&req); err != nil {
  139. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  140. return
  141. }
  142. if err := a.hostService.ReorderHosts(req.Ids); err != nil {
  143. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  144. return
  145. }
  146. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), nil)
  147. }
  148. func (a *HostController) bulkSetEnable(c *gin.Context) {
  149. var req struct {
  150. Ids []int `json:"ids" form:"ids"`
  151. Enable bool `json:"enable" form:"enable"`
  152. }
  153. if err := c.ShouldBindJSON(&req); err != nil {
  154. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  155. return
  156. }
  157. if err := a.hostService.SetHostsEnable(req.Ids, req.Enable); err != nil {
  158. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), err)
  159. return
  160. }
  161. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.update"), nil)
  162. }
  163. func (a *HostController) bulkDel(c *gin.Context) {
  164. var req struct {
  165. Ids []int `json:"ids" form:"ids"`
  166. }
  167. if err := c.ShouldBindJSON(&req); err != nil {
  168. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), err)
  169. return
  170. }
  171. if err := a.hostService.DeleteHosts(req.Ids); err != nil {
  172. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), err)
  173. return
  174. }
  175. jsonMsg(c, I18nWeb(c, "pages.hosts.toasts.delete"), nil)
  176. }