123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package stream
- import (
- "os"
- "github.com/0xJacky/Nginx-UI/internal/config"
- "github.com/0xJacky/Nginx-UI/internal/nginx"
- "github.com/0xJacky/Nginx-UI/model"
- "github.com/0xJacky/Nginx-UI/query"
- )
- // StreamInfo represents stream information
- type StreamInfo struct {
- Path string
- Status config.ConfigStatus
- Model *model.Stream
- FileInfo os.FileInfo
- RawContent string
- NgxConfig *nginx.NgxConfig
- }
- // GetStreamInfo retrieves comprehensive information about a stream
- func GetStreamInfo(name string) (*StreamInfo, error) {
- // Get the absolute path to the stream configuration file
- path := nginx.GetConfPath("streams-available", name)
- fileInfo, err := os.Stat(path)
- if os.IsNotExist(err) {
- return nil, ErrStreamNotFound
- }
- if err != nil {
- return nil, err
- }
- // Check if the stream is enabled
- status := config.StatusEnabled
- if _, err := os.Stat(nginx.GetConfPath("streams-enabled", name)); os.IsNotExist(err) {
- status = config.StatusDisabled
- }
- // Retrieve or create stream model from database
- s := query.Stream
- streamModel, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
- if err != nil {
- return nil, err
- }
- // Read raw content
- rawContent, err := os.ReadFile(path)
- if err != nil {
- return nil, err
- }
- info := &StreamInfo{
- Path: path,
- Status: status,
- Model: streamModel,
- FileInfo: fileInfo,
- RawContent: string(rawContent),
- }
- // Parse configuration if not in advanced mode
- if !streamModel.Advanced {
- nginxConfig, err := nginx.ParseNgxConfig(path)
- if err != nil {
- return nil, err
- }
- info.NgxConfig = nginxConfig
- }
- return info, nil
- }
- // SaveStreamConfig saves stream configuration with database update
- func SaveStreamConfig(name, content string, envGroupID uint64, syncNodeIDs []uint64, overwrite bool, postAction string) error {
- // Get stream from database or create if not exists
- path := nginx.GetConfPath("streams-available", name)
- s := query.Stream
- streamModel, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
- if err != nil {
- return err
- }
- // Update Node Group ID if provided
- if envGroupID > 0 {
- streamModel.EnvGroupID = envGroupID
- }
- // Update synchronization node IDs if provided
- if syncNodeIDs != nil {
- streamModel.SyncNodeIDs = syncNodeIDs
- }
- // Save the updated stream model to database
- _, err = s.Where(s.ID.Eq(streamModel.ID)).Updates(streamModel)
- if err != nil {
- return err
- }
- // Save the stream configuration file
- return Save(name, content, overwrite, syncNodeIDs, postAction)
- }
|