get.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package stream
  2. import (
  3. "os"
  4. "github.com/0xJacky/Nginx-UI/internal/config"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx"
  6. "github.com/0xJacky/Nginx-UI/model"
  7. "github.com/0xJacky/Nginx-UI/query"
  8. )
  9. // StreamInfo represents stream information
  10. type StreamInfo struct {
  11. Path string
  12. Status config.ConfigStatus
  13. Model *model.Stream
  14. FileInfo os.FileInfo
  15. RawContent string
  16. NgxConfig *nginx.NgxConfig
  17. }
  18. // GetStreamInfo retrieves comprehensive information about a stream
  19. func GetStreamInfo(name string) (*StreamInfo, error) {
  20. // Get the absolute path to the stream configuration file
  21. path := nginx.GetConfPath("streams-available", name)
  22. fileInfo, err := os.Stat(path)
  23. if os.IsNotExist(err) {
  24. return nil, ErrStreamNotFound
  25. }
  26. if err != nil {
  27. return nil, err
  28. }
  29. // Check if the stream is enabled
  30. status := config.StatusEnabled
  31. if _, err := os.Stat(nginx.GetConfPath("streams-enabled", name)); os.IsNotExist(err) {
  32. status = config.StatusDisabled
  33. }
  34. // Retrieve or create stream model from database
  35. s := query.Stream
  36. streamModel, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
  37. if err != nil {
  38. return nil, err
  39. }
  40. // Read raw content
  41. rawContent, err := os.ReadFile(path)
  42. if err != nil {
  43. return nil, err
  44. }
  45. info := &StreamInfo{
  46. Path: path,
  47. Status: status,
  48. Model: streamModel,
  49. FileInfo: fileInfo,
  50. RawContent: string(rawContent),
  51. }
  52. // Parse configuration if not in advanced mode
  53. if !streamModel.Advanced {
  54. nginxConfig, err := nginx.ParseNgxConfig(path)
  55. if err != nil {
  56. return nil, err
  57. }
  58. info.NgxConfig = nginxConfig
  59. }
  60. return info, nil
  61. }
  62. // SaveStreamConfig saves stream configuration with database update
  63. func SaveStreamConfig(name, content string, envGroupID uint64, syncNodeIDs []uint64, overwrite bool, postAction string) error {
  64. // Get stream from database or create if not exists
  65. path := nginx.GetConfPath("streams-available", name)
  66. s := query.Stream
  67. streamModel, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
  68. if err != nil {
  69. return err
  70. }
  71. // Update Node Group ID if provided
  72. if envGroupID > 0 {
  73. streamModel.EnvGroupID = envGroupID
  74. }
  75. // Update synchronization node IDs if provided
  76. if syncNodeIDs != nil {
  77. streamModel.SyncNodeIDs = syncNodeIDs
  78. }
  79. // Save the updated stream model to database
  80. _, err = s.Where(s.ID.Eq(streamModel.ID)).Updates(streamModel)
  81. if err != nil {
  82. return err
  83. }
  84. // Save the stream configuration file
  85. return Save(name, content, overwrite, syncNodeIDs, postAction)
  86. }