modify.go 943 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package config
  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. type EditConfigJson struct {
  10. Content string `json:"content" binding:"required"`
  11. }
  12. func EditConfig(c *gin.Context) {
  13. name := c.Param("name")
  14. var request EditConfigJson
  15. err := c.BindJSON(&request)
  16. if err != nil {
  17. api.ErrHandler(c, err)
  18. return
  19. }
  20. path := nginx.GetConfPath("/", name)
  21. content := request.Content
  22. origContent, err := os.ReadFile(path)
  23. if err != nil {
  24. api.ErrHandler(c, err)
  25. return
  26. }
  27. if content != "" && content != string(origContent) {
  28. // model.CreateBackup(path)
  29. err = os.WriteFile(path, []byte(content), 0644)
  30. if err != nil {
  31. api.ErrHandler(c, err)
  32. return
  33. }
  34. }
  35. output := nginx.Reload()
  36. if nginx.GetLogLevel(output) >= nginx.Warn {
  37. c.JSON(http.StatusInternalServerError, gin.H{
  38. "message": output,
  39. })
  40. return
  41. }
  42. GetConfig(c)
  43. }