api.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package controller
  2. import (
  3. "github.com/mhsanaei/3x-ui/v2/web/service"
  4. "github.com/gin-gonic/gin"
  5. )
  6. // APIController handles the main API routes for the 3x-ui panel, including inbounds and server management.
  7. type APIController struct {
  8. BaseController
  9. inboundController *InboundController
  10. serverController *ServerController
  11. Tgbot service.Tgbot
  12. }
  13. // NewAPIController creates a new APIController instance and initializes its routes.
  14. func NewAPIController(g *gin.RouterGroup) *APIController {
  15. a := &APIController{}
  16. a.initRouter(g)
  17. return a
  18. }
  19. // initRouter sets up the API routes for inbounds, server, and other endpoints.
  20. func (a *APIController) initRouter(g *gin.RouterGroup) {
  21. // Main API group
  22. api := g.Group("/panel/api")
  23. api.Use(a.checkLogin)
  24. // Inbounds API
  25. inbounds := api.Group("/inbounds")
  26. a.inboundController = NewInboundController(inbounds)
  27. // Server API
  28. server := api.Group("/server")
  29. a.serverController = NewServerController(server)
  30. // Extra routes
  31. api.GET("/backuptotgbot", a.BackuptoTgbot)
  32. }
  33. // BackuptoTgbot sends a backup of the panel data to Telegram bot admins.
  34. func (a *APIController) BackuptoTgbot(c *gin.Context) {
  35. a.Tgbot.SendBackupToAdmins()
  36. }