nginx.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package nginx
  2. import (
  3. "github.com/0xJacky/Nginx-UI/api"
  4. "github.com/0xJacky/Nginx-UI/internal/nginx"
  5. "github.com/gin-gonic/gin"
  6. "net/http"
  7. "os"
  8. )
  9. func BuildNginxConfig(c *gin.Context) {
  10. var ngxConf nginx.NgxConfig
  11. if !api.BindAndValid(c, &ngxConf) {
  12. return
  13. }
  14. c.Set("maybe_error", "nginx_config_syntax_error")
  15. c.JSON(http.StatusOK, gin.H{
  16. "content": ngxConf.BuildConfig(),
  17. })
  18. }
  19. func TokenizeNginxConfig(c *gin.Context) {
  20. var json struct {
  21. Content string `json:"content" binding:"required"`
  22. }
  23. if !api.BindAndValid(c, &json) {
  24. return
  25. }
  26. c.Set("maybe_error", "nginx_config_syntax_error")
  27. ngxConfig := nginx.ParseNgxConfigByContent(json.Content)
  28. c.JSON(http.StatusOK, ngxConfig)
  29. }
  30. func FormatNginxConfig(c *gin.Context) {
  31. var json struct {
  32. Content string `json:"content" binding:"required"`
  33. }
  34. if !api.BindAndValid(c, &json) {
  35. return
  36. }
  37. c.Set("maybe_error", "nginx_config_syntax_error")
  38. c.JSON(http.StatusOK, gin.H{
  39. "content": nginx.FmtCode(json.Content),
  40. })
  41. }
  42. func Status(c *gin.Context) {
  43. pidPath := nginx.GetPIDPath()
  44. running := true
  45. if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id
  46. running = false
  47. }
  48. c.JSON(http.StatusOK, gin.H{
  49. "running": running,
  50. })
  51. }