control.go 1.4 KB

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