status.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Implementation of GetDetailedStatus API
  2. // This feature is designed to address Issue #850, providing Nginx load monitoring functionality similar to BT Panel
  3. // Returns detailed Nginx status information, including request statistics, connections, worker processes, and other data
  4. package nginx
  5. import (
  6. "net/http"
  7. "strings"
  8. "github.com/0xJacky/Nginx-UI/internal/nginx"
  9. "github.com/0xJacky/Nginx-UI/internal/performance"
  10. "github.com/gin-gonic/gin"
  11. "github.com/uozi-tech/cosy"
  12. )
  13. // NginxPerformanceInfo stores Nginx performance-related information
  14. type NginxPerformanceInfo struct {
  15. // Basic status information
  16. performance.StubStatusData
  17. // Process-related information
  18. performance.NginxProcessInfo
  19. // Configuration information
  20. performance.NginxConfigInfo
  21. }
  22. // GetDetailStatus retrieves detailed Nginx status information
  23. func GetDetailStatus(c *gin.Context) {
  24. response := performance.GetPerformanceData()
  25. c.JSON(http.StatusOK, response)
  26. }
  27. // CheckStubStatus gets Nginx stub_status module status
  28. func CheckStubStatus(c *gin.Context) {
  29. stubStatus := performance.GetStubStatus()
  30. c.JSON(http.StatusOK, stubStatus)
  31. }
  32. // ToggleStubStatus enables or disables stub_status module
  33. func ToggleStubStatus(c *gin.Context) {
  34. var json struct {
  35. Enable bool `json:"enable"`
  36. }
  37. if !cosy.BindAndValid(c, &json) {
  38. return
  39. }
  40. stubStatus := performance.GetStubStatus()
  41. // If current status matches desired status, no action needed
  42. if stubStatus.Enabled == json.Enable {
  43. c.JSON(http.StatusOK, stubStatus)
  44. return
  45. }
  46. var err error
  47. if json.Enable {
  48. err = performance.EnableStubStatus()
  49. } else {
  50. err = performance.DisableStubStatus()
  51. }
  52. if err != nil {
  53. cosy.ErrHandler(c, err)
  54. return
  55. }
  56. // Reload Nginx configuration
  57. reloadOutput, err := nginx.Reload()
  58. if err != nil {
  59. cosy.ErrHandler(c, err)
  60. return
  61. }
  62. if len(reloadOutput) > 0 && (strings.Contains(strings.ToLower(reloadOutput), "error") ||
  63. strings.Contains(strings.ToLower(reloadOutput), "failed")) {
  64. cosy.ErrHandler(c, cosy.WrapErrorWithParams(nginx.ErrReloadFailed, reloadOutput))
  65. return
  66. }
  67. // Check status after operation
  68. newStubStatus := performance.GetStubStatus()
  69. c.JSON(http.StatusOK, newStubStatus)
  70. }