ngx.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package api
  2. import (
  3. "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "strings"
  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 ReloadNginx(c *gin.Context) {
  42. output := nginx.Reload()
  43. if output != "" && strings.Contains(output, "error") {
  44. c.JSON(http.StatusInternalServerError, gin.H{
  45. "message": output,
  46. })
  47. return
  48. }
  49. c.JSON(http.StatusOK, gin.H{
  50. "message": "ok",
  51. })
  52. }