package main import ( "encoding/json" "flag" "io/ioutil" "os" "time" "github.com/hashicorp/go-version" "github.com/imdario/mergo" "github.com/liip/sheriff" "github.com/sirupsen/logrus" ) // Command line flag global variables var debug bool var configFile string // NewStreamCore do load config file func NewStreamCore() *StorageST { flag.BoolVar(&debug, "debug", true, "set debug mode") flag.StringVar(&configFile, "config", "config.json", "config patch (/etc/server/config.json or config.json)") flag.Parse() var tmp StorageST data, err := ioutil.ReadFile(configFile) if err != nil { log.WithFields(logrus.Fields{ "module": "config", "func": "NewStreamCore", "call": "ReadFile", }).Errorln(err.Error()) os.Exit(1) } err = json.Unmarshal(data, &tmp) if err != nil { log.WithFields(logrus.Fields{ "module": "config", "func": "NewStreamCore", "call": "Unmarshal", }).Errorln(err.Error()) os.Exit(1) } debug = tmp.Server.Debug // 初始化数据库连接 if tmp.Database.Enabled { err = tmp.InitDatabase() if err != nil { log.WithFields(logrus.Fields{ "module": "config", "func": "NewStreamCore", "call": "InitDatabase", }).Errorln(err.Error()) os.Exit(1) } // 从数据库加载流配置 err = tmp.LoadStreamsFromDatabase() if err != nil { log.WithFields(logrus.Fields{ "module": "config", "func": "NewStreamCore", "call": "LoadStreamsFromDatabase", }).Errorln(err.Error()) os.Exit(1) } } // 处理流配置(从配置文件或数据库) for i, i2 := range tmp.Streams { for i3, i4 := range i2.Channels { var channel ChannelST err = mergo.Merge(&channel, i4) if err != nil { log.WithFields(logrus.Fields{ "module": "config", "func": "NewStreamCore", "call": "Merge", }).Errorln(err.Error()) os.Exit(1) } channel.clients = make(map[string]ClientST) channel.ack = time.Now().Add(-255 * time.Hour) channel.hlsSegmentBuffer = make(map[int]SegmentOld) channel.signals = make(chan int, 100) i2.Channels[i3] = channel } tmp.Streams[i] = i2 } return &tmp } // ClientDelete Delete Client func (obj *StorageST) SaveConfig() error { log.WithFields(logrus.Fields{ "module": "config", "func": "NewStreamCore", }).Debugln("Saving configuration to", configFile) v2, err := version.NewVersion("2.0.0") if err != nil { return err } data, err := sheriff.Marshal(&sheriff.Options{ Groups: []string{"config"}, ApiVersion: v2, }, obj) if err != nil { return err } res, err := json.MarshalIndent(data, "", " ") if err != nil { return err } err = ioutil.WriteFile(configFile, res, 0644) if err != nil { log.WithFields(logrus.Fields{ "module": "config", "func": "SaveConfig", "call": "WriteFile", }).Errorln(err.Error()) return err } return nil }