list.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package config
  2. import (
  3. "net/http"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/0xJacky/Nginx-UI/internal/config"
  8. "github.com/0xJacky/Nginx-UI/internal/helper"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/model"
  11. "github.com/gin-gonic/gin"
  12. "github.com/spf13/cast"
  13. "github.com/uozi-tech/cosy"
  14. )
  15. // FileEntity represents a generic configuration file entity
  16. type FileEntity struct {
  17. path string
  18. namespaceID uint64
  19. namespace *model.Namespace
  20. }
  21. // GetPath implements Entity interface
  22. func (c *FileEntity) GetPath() string {
  23. return c.path
  24. }
  25. // GetNamespaceID implements Entity interface
  26. func (c *FileEntity) GetNamespaceID() uint64 {
  27. return c.namespaceID
  28. }
  29. // GetNamespace implements Entity interface
  30. func (c *FileEntity) GetNamespace() *model.Namespace {
  31. return c.namespace
  32. }
  33. func GetConfigs(c *gin.Context) {
  34. search := c.Query("search")
  35. sortBy := c.DefaultQuery("sort_by", "name")
  36. order := c.DefaultQuery("order", "asc")
  37. namespaceId := cast.ToUint64(c.Query("namespace_id"))
  38. // Get directory parameter
  39. encodedDir := c.DefaultQuery("dir", "/")
  40. // Handle cases where the path might be encoded multiple times
  41. dir := helper.UnescapeURL(encodedDir)
  42. // Ensure the directory path format is correct
  43. dir = strings.TrimSpace(dir)
  44. if dir != "/" && strings.HasSuffix(dir, "/") {
  45. dir = strings.TrimSuffix(dir, "/")
  46. }
  47. // Create options
  48. options := &config.GenericListOptions{
  49. Search: search,
  50. OrderBy: sortBy,
  51. Sort: order,
  52. NamespaceID: namespaceId,
  53. IncludeDirs: true, // Keep directories for the list.go endpoint
  54. }
  55. // Get config files from directory and create entities
  56. configFiles, err := os.ReadDir(nginx.GetConfPath(dir))
  57. if err != nil {
  58. cosy.ErrHandler(c, err)
  59. return
  60. }
  61. // Create entities for each config file
  62. var entities []*FileEntity
  63. for _, file := range configFiles {
  64. // Skip directories only if IncludeDirs is false
  65. if file.IsDir() && !options.IncludeDirs {
  66. continue
  67. }
  68. // For generic config files, we don't have database records
  69. // so namespaceID and namespace will be 0 and nil
  70. entity := &FileEntity{
  71. path: filepath.Join(nginx.GetConfPath(dir), file.Name()),
  72. namespaceID: 0,
  73. namespace: nil,
  74. }
  75. entities = append(entities, entity)
  76. }
  77. // Create processor for generic config files
  78. processor := &config.GenericConfigProcessor{
  79. Paths: config.Paths{
  80. AvailableDir: dir,
  81. EnabledDir: dir, // For generic configs, available and enabled are the same
  82. },
  83. StatusMapBuilder: config.DefaultStatusMapBuilder,
  84. ConfigBuilder: createConfigBuilder(dir),
  85. FilterMatcher: config.DefaultFilterMatcher,
  86. }
  87. // Get configurations using the generic processor
  88. configs, err := config.GetGenericConfigs(c, options, entities, processor)
  89. if err != nil {
  90. cosy.ErrHandler(c, err)
  91. return
  92. }
  93. c.JSON(http.StatusOK, gin.H{
  94. "data": configs,
  95. })
  96. }
  97. // createConfigBuilder creates a custom config builder for generic config files
  98. func createConfigBuilder(dir string) config.Builder {
  99. return func(fileName string, fileInfo os.FileInfo, status config.Status, namespaceID uint64, namespace *model.Namespace) config.Config {
  100. return config.Config{
  101. Name: fileName,
  102. ModifiedAt: fileInfo.ModTime(),
  103. Size: fileInfo.Size(),
  104. IsDir: fileInfo.IsDir(),
  105. Status: status,
  106. NamespaceID: namespaceID,
  107. Namespace: namespace,
  108. Dir: dir,
  109. }
  110. }
  111. }