ngx.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package api
  2. import (
  3. "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "os"
  7. )
  8. func BuildNginxConfig(c *gin.Context) {
  9. var ngxConf nginx.NgxConfig
  10. if !BindAndValid(c, &ngxConf) {
  11. return
  12. }
  13. c.Set("maybe_error", "nginx_config_syntax_error")
  14. c.JSON(http.StatusOK, gin.H{
  15. "content": ngxConf.BuildConfig(),
  16. })
  17. }
  18. func TokenizeNginxConfig(c *gin.Context) {
  19. var json struct {
  20. Content string `json:"content" binding:"required"`
  21. }
  22. if !BindAndValid(c, &json) {
  23. return
  24. }
  25. c.Set("maybe_error", "nginx_config_syntax_error")
  26. ngxConfig := nginx.ParseNgxConfigByContent(json.Content)
  27. c.JSON(http.StatusOK, ngxConfig)
  28. }
  29. func FormatNginxConfig(c *gin.Context) {
  30. var json struct {
  31. Content string `json:"content" binding:"required"`
  32. }
  33. if !BindAndValid(c, &json) {
  34. return
  35. }
  36. c.Set("maybe_error", "nginx_config_syntax_error")
  37. c.JSON(http.StatusOK, gin.H{
  38. "content": nginx.FmtCode(json.Content),
  39. })
  40. }
  41. func NginxStatus(c *gin.Context) {
  42. pidPath := nginx.GetNginxPIDPath()
  43. running := true
  44. if _, err := os.Stat(pidPath); err != nil {
  45. running = false
  46. }
  47. c.JSON(http.StatusOK, gin.H{
  48. "running": running,
  49. })
  50. }
  51. func ReloadNginx(c *gin.Context) {
  52. output := nginx.Reload()
  53. c.JSON(http.StatusOK, gin.H{
  54. "message": output,
  55. "level": nginx.GetLogLevel(output),
  56. })
  57. }
  58. func TestNginx(c *gin.Context) {
  59. output := nginx.TestConf()
  60. c.JSON(http.StatusOK, gin.H{
  61. "message": output,
  62. "level": nginx.GetLogLevel(output),
  63. })
  64. }
  65. func RestartNginx(c *gin.Context) {
  66. output := nginx.Restart()
  67. c.JSON(http.StatusOK, gin.H{
  68. "message": output,
  69. "level": nginx.GetLogLevel(output),
  70. })
  71. }