control.go 913 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package nginx
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/nginx"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "os"
  7. )
  8. func Reload(c *gin.Context) {
  9. output := nginx.Reload()
  10. c.JSON(http.StatusOK, gin.H{
  11. "message": output,
  12. "level": nginx.GetLogLevel(output),
  13. })
  14. }
  15. func Test(c *gin.Context) {
  16. output := nginx.TestConf()
  17. c.JSON(http.StatusOK, gin.H{
  18. "message": output,
  19. "level": nginx.GetLogLevel(output),
  20. })
  21. }
  22. func Restart(c *gin.Context) {
  23. c.JSON(http.StatusOK, gin.H{
  24. "message": "ok",
  25. })
  26. go nginx.Restart()
  27. }
  28. func Status(c *gin.Context) {
  29. pidPath := nginx.GetPIDPath()
  30. lastOutput := nginx.GetLastOutput()
  31. running := true
  32. if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id
  33. running = false
  34. }
  35. c.JSON(http.StatusOK, gin.H{
  36. "running": running,
  37. "message": lastOutput,
  38. "level": nginx.GetLogLevel(lastOutput),
  39. })
  40. }