list.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package stream
  2. import (
  3. "context"
  4. "os"
  5. "github.com/0xJacky/Nginx-UI/internal/config"
  6. "github.com/0xJacky/Nginx-UI/internal/upstream"
  7. "github.com/0xJacky/Nginx-UI/model"
  8. )
  9. // ListOptions represents the options for listing streams
  10. type ListOptions struct {
  11. Search string
  12. Status string
  13. OrderBy string
  14. Sort string
  15. EnvGroupID uint64
  16. }
  17. // GetStreamConfigs retrieves and processes stream configurations with database integration
  18. func GetStreamConfigs(ctx context.Context, options *ListOptions, streams []*model.Stream) ([]config.Config, error) {
  19. // Convert to generic options
  20. genericOptions := &config.GenericListOptions{
  21. Search: options.Search,
  22. Status: options.Status,
  23. OrderBy: options.OrderBy,
  24. Sort: options.Sort,
  25. EnvGroupID: options.EnvGroupID,
  26. IncludeDirs: false, // Filter out directories for stream configurations
  27. }
  28. // Create processor with stream-specific logic
  29. processor := &config.GenericConfigProcessor{
  30. Paths: config.ConfigPaths{
  31. AvailableDir: "streams-available",
  32. EnabledDir: "streams-enabled",
  33. },
  34. StatusMapBuilder: config.DefaultStatusMapBuilder,
  35. ConfigBuilder: buildConfig,
  36. FilterMatcher: config.DefaultFilterMatcher,
  37. }
  38. return config.GetGenericConfigs(ctx, genericOptions, streams, processor)
  39. }
  40. // buildConfig creates a config.Config from file information with stream-specific data
  41. func buildConfig(fileName string, fileInfo os.FileInfo, status config.ConfigStatus, envGroupID uint64, envGroup *model.EnvGroup) config.Config {
  42. indexedStream := GetIndexedStream(fileName)
  43. // Convert proxy targets, expanding upstream references
  44. var proxyTargets []config.ProxyTarget
  45. upstreamService := upstream.GetUpstreamService()
  46. for _, target := range indexedStream.ProxyTargets {
  47. // Check if target.Host is an upstream name
  48. if upstreamDef, exists := upstreamService.GetUpstreamDefinition(target.Host); exists {
  49. // Replace with upstream servers
  50. for _, server := range upstreamDef.Servers {
  51. proxyTargets = append(proxyTargets, config.ProxyTarget{
  52. Host: server.Host,
  53. Port: server.Port,
  54. Type: server.Type,
  55. })
  56. }
  57. } else {
  58. // Regular proxy target
  59. proxyTargets = append(proxyTargets, config.ProxyTarget{
  60. Host: target.Host,
  61. Port: target.Port,
  62. Type: target.Type,
  63. })
  64. }
  65. }
  66. return config.Config{
  67. Name: fileName,
  68. ModifiedAt: fileInfo.ModTime(),
  69. Size: fileInfo.Size(),
  70. IsDir: fileInfo.IsDir(),
  71. Status: status,
  72. EnvGroupID: envGroupID,
  73. EnvGroup: envGroup,
  74. ProxyTargets: proxyTargets,
  75. }
  76. }