-
Notifications
You must be signed in to change notification settings - Fork 318
/
storageConfig.go
103 lines (94 loc) · 2.25 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
94
95
96
97
98
99
100
101
102
103
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
}