list.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package config
  2. import (
  3. "net/http"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "github.com/0xJacky/Nginx-UI/internal/config"
  9. "github.com/0xJacky/Nginx-UI/internal/helper"
  10. "github.com/0xJacky/Nginx-UI/internal/nginx"
  11. "github.com/0xJacky/Nginx-UI/model"
  12. "github.com/gin-gonic/gin"
  13. "github.com/uozi-tech/cosy"
  14. )
  15. // ConfigFileEntity represents a generic configuration file entity
  16. type ConfigFileEntity struct {
  17. path string
  18. envGroupID uint64
  19. envGroup *model.EnvGroup
  20. }
  21. // GetPath implements ConfigEntity interface
  22. func (c *ConfigFileEntity) GetPath() string {
  23. return c.path
  24. }
  25. // GetEnvGroupID implements ConfigEntity interface
  26. func (c *ConfigFileEntity) GetEnvGroupID() uint64 {
  27. return c.envGroupID
  28. }
  29. // GetEnvGroup implements ConfigEntity interface
  30. func (c *ConfigFileEntity) GetEnvGroup() *model.EnvGroup {
  31. return c.envGroup
  32. }
  33. func GetConfigs(c *gin.Context) {
  34. search := c.Query("search")
  35. sortBy := c.Query("sort_by")
  36. order := c.DefaultQuery("order", "desc")
  37. envGroupIDStr := c.Query("env_group_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. // Parse env_group_id
  48. var envGroupID uint64
  49. if envGroupIDStr != "" {
  50. if id, err := strconv.ParseUint(envGroupIDStr, 10, 64); err == nil {
  51. envGroupID = id
  52. }
  53. }
  54. // Create options
  55. options := &config.GenericListOptions{
  56. Search: search,
  57. OrderBy: sortBy,
  58. Sort: order,
  59. EnvGroupID: envGroupID,
  60. IncludeDirs: true, // Keep directories for the list.go endpoint
  61. }
  62. // Get config files from directory and create entities
  63. configFiles, err := os.ReadDir(nginx.GetConfPath(dir))
  64. if err != nil {
  65. cosy.ErrHandler(c, err)
  66. return
  67. }
  68. // Create entities for each config file
  69. var entities []*ConfigFileEntity
  70. for _, file := range configFiles {
  71. // Skip directories only if IncludeDirs is false
  72. if file.IsDir() && !options.IncludeDirs {
  73. continue
  74. }
  75. // For generic config files, we don't have database records
  76. // so envGroupID and envGroup will be 0 and nil
  77. entity := &ConfigFileEntity{
  78. path: filepath.Join(nginx.GetConfPath(dir), file.Name()),
  79. envGroupID: 0,
  80. envGroup: nil,
  81. }
  82. entities = append(entities, entity)
  83. }
  84. // Create processor for generic config files
  85. processor := &config.GenericConfigProcessor{
  86. Paths: config.ConfigPaths{
  87. AvailableDir: dir,
  88. EnabledDir: dir, // For generic configs, available and enabled are the same
  89. },
  90. StatusMapBuilder: config.DefaultStatusMapBuilder,
  91. ConfigBuilder: createConfigBuilder(dir),
  92. FilterMatcher: config.DefaultFilterMatcher,
  93. }
  94. // Get configurations using the generic processor
  95. configs, err := config.GetGenericConfigs(c, options, entities, processor)
  96. if err != nil {
  97. cosy.ErrHandler(c, err)
  98. return
  99. }
  100. c.JSON(http.StatusOK, gin.H{
  101. "data": configs,
  102. })
  103. }
  104. // createConfigBuilder creates a custom config builder for generic config files
  105. func createConfigBuilder(dir string) config.ConfigBuilder {
  106. return func(fileName string, fileInfo os.FileInfo, status config.ConfigStatus, envGroupID uint64, envGroup *model.EnvGroup) config.Config {
  107. return config.Config{
  108. Name: fileName,
  109. ModifiedAt: fileInfo.ModTime(),
  110. Size: fileInfo.Size(),
  111. IsDir: fileInfo.IsDir(),
  112. Status: status,
  113. EnvGroupID: envGroupID,
  114. EnvGroup: envGroup,
  115. Dir: dir,
  116. }
  117. }
  118. }