index.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package stream
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "github.com/0xJacky/Nginx-UI/internal/cache"
  6. "github.com/0xJacky/Nginx-UI/internal/upstream"
  7. )
  8. type StreamIndex struct {
  9. Path string
  10. Content string
  11. ProxyTargets []upstream.ProxyTarget
  12. }
  13. var (
  14. IndexedStreams = make(map[string]*StreamIndex)
  15. )
  16. func GetIndexedStream(path string) *StreamIndex {
  17. if stream, ok := IndexedStreams[path]; ok {
  18. return stream
  19. }
  20. return &StreamIndex{}
  21. }
  22. func init() {
  23. cache.RegisterCallback(scanForStream)
  24. }
  25. func scanForStream(configPath string, content []byte) error {
  26. // Only process stream configuration files
  27. if !isStreamConfig(configPath) {
  28. return nil
  29. }
  30. streamIndex := StreamIndex{
  31. Path: configPath,
  32. Content: string(content),
  33. ProxyTargets: []upstream.ProxyTarget{},
  34. }
  35. // Parse proxy targets from the configuration content
  36. streamIndex.ProxyTargets = upstream.ParseProxyTargetsFromRawContent(string(content))
  37. // Only store if we found proxy targets
  38. if len(streamIndex.ProxyTargets) > 0 {
  39. IndexedStreams[filepath.Base(configPath)] = &streamIndex
  40. }
  41. return nil
  42. }
  43. // isStreamConfig checks if the config path is a stream configuration
  44. func isStreamConfig(configPath string) bool {
  45. return strings.Contains(configPath, "streams-available") ||
  46. strings.Contains(configPath, "streams-enabled")
  47. }