You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RTSPtoWeb/storageConfig.go

104 lines
2.3 KiB
Go

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
for i, i2 := range tmp.Streams {
for i3, i4 := range i2.Channels {
channel := tmp.ChannelDefaults
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
}