control.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package nginx
  2. import (
  3. "net/http"
  4. "github.com/0xJacky/Nginx-UI/internal/nginx"
  5. "github.com/gin-gonic/gin"
  6. "github.com/uozi-tech/cosy"
  7. )
  8. // Reload reloads the nginx
  9. func Reload(c *gin.Context) {
  10. output, err := nginx.Reload()
  11. if err != nil {
  12. cosy.ErrHandler(c, err)
  13. return
  14. }
  15. c.JSON(http.StatusOK, gin.H{
  16. "message": output,
  17. "level": nginx.GetLogLevel(output),
  18. })
  19. }
  20. // TestConfig tests the nginx config
  21. func TestConfig(c *gin.Context) {
  22. output, err := nginx.TestConfig()
  23. if err != nil {
  24. cosy.ErrHandler(c, err)
  25. return
  26. }
  27. c.JSON(http.StatusOK, gin.H{
  28. "message": output,
  29. "level": nginx.GetLogLevel(output),
  30. })
  31. }
  32. // Restart restarts the nginx
  33. func Restart(c *gin.Context) {
  34. c.JSON(http.StatusOK, gin.H{
  35. "message": "ok",
  36. })
  37. go nginx.Restart()
  38. }
  39. // Status returns the status of the nginx
  40. func Status(c *gin.Context) {
  41. lastOutput, err := nginx.GetLastOutput()
  42. if err != nil {
  43. cosy.ErrHandler(c, err)
  44. return
  45. }
  46. running := nginx.IsNginxRunning()
  47. c.JSON(http.StatusOK, gin.H{
  48. "running": running,
  49. "message": lastOutput,
  50. "level": nginx.GetLogLevel(lastOutput),
  51. })
  52. }