forked from deepch/RTSPtoWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storageConfig.go
93 lines (85 loc) · 1.98 KB
/
storageConfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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"
)
//debug global
var debug bool
//NewStreamCore do load config file
func NewStreamCore() *StorageST {
argConfigPatch := flag.String("config", "config.json", "config patch (/etc/server/config.json or config.json)")
argDebug := flag.Bool("debug", true, "set debug mode")
debug = *argDebug
flag.Parse()
var tmp StorageST
data, err := ioutil.ReadFile(*argConfigPatch)
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 {
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("config.json", res, 0644)
if err != nil {
return err
}
return nil
}