get.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package config
  2. import (
  3. "net/http"
  4. "os"
  5. "path/filepath"
  6. "github.com/0xJacky/Nginx-UI/internal/config"
  7. "github.com/0xJacky/Nginx-UI/internal/helper"
  8. "github.com/0xJacky/Nginx-UI/internal/nginx"
  9. "github.com/0xJacky/Nginx-UI/query"
  10. "github.com/gin-gonic/gin"
  11. "github.com/uozi-tech/cosy"
  12. )
  13. func GetConfig(c *gin.Context) {
  14. relativePath := helper.UnescapeURL(c.Param("path"))
  15. absPath := nginx.GetConfPath(relativePath)
  16. if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) {
  17. c.JSON(http.StatusForbidden, gin.H{
  18. "message": "path is not under the nginx conf path",
  19. })
  20. return
  21. }
  22. stat, err := os.Stat(absPath)
  23. if err != nil {
  24. cosy.ErrHandler(c, err)
  25. return
  26. }
  27. content, err := os.ReadFile(absPath)
  28. if err != nil {
  29. cosy.ErrHandler(c, err)
  30. return
  31. }
  32. q := query.Config
  33. cfg, err := q.Where(q.Filepath.Eq(absPath)).FirstOrInit()
  34. if err != nil {
  35. cosy.ErrHandler(c, err)
  36. return
  37. }
  38. c.JSON(http.StatusOK, config.Config{
  39. Name: stat.Name(),
  40. Content: string(content),
  41. FilePath: absPath,
  42. ModifiedAt: stat.ModTime(),
  43. Dir: filepath.Dir(relativePath),
  44. SyncNodeIds: cfg.SyncNodeIds,
  45. SyncOverwrite: cfg.SyncOverwrite,
  46. })
  47. }