-
Notifications
You must be signed in to change notification settings - Fork 2
/
cfg.go
137 lines (118 loc) · 3.23 KB
/
cfg.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"chnvideo.com/cloud/common/core"
"chnvideo.com/cloud/common/mysql"
"fmt"
"log"
"os"
"sync"
"chnvideo.com/cloud/playout/util"
"path"
"encoding/json"
)
type GlobalConfig struct {
core.Config
mysql.SqlCommonConfig
Playout struct {
Ip string `json:"access_ip"`
Ums string `json:"ums_host_port"`
} `json:"playout"`
Resources struct{
VideoDir string `json:"video_dir"`
ScanInterval int `json:"cron_scan_interval_seconds"`
} `json:"resources"`
}
func (v *GlobalConfig) Validate() (err error) {
if err = v.Config.Validate(); err != nil {
log.Println("config validate failed, ", err)
return
}
if err = v.SqlCommonConfig.Validate(); err != nil {
log.Println("mysql config validate failed, ", err)
return
}
if len(v.Playout.Ip) == 0 {
err = fmt.Errorf("access ip is empty")
log.Println(err.Error())
return
}
if len(v.Playout.Ums) == 0 {
err = fmt.Errorf("ums addr is empty")
log.Println(err.Error())
return
}
if len(v.Resources.VideoDir) == 0 {
err = fmt.Errorf("resources video dir is empty")
log.Println(err.Error())
return
}
if v.Resources.ScanInterval <= 0 {
err = fmt.Errorf("resources cron scan iterval should > 0")
log.Println(err.Error())
return
}
return
}
var (
ConfigFile string
config *GlobalConfig
lock = new(sync.RWMutex)
)
func Config() *GlobalConfig {
lock.RLock()
defer lock.RUnlock()
return config
}
func ParseConfig(cfg string) (err error) {
if cfg == "" {
log.Fatalln("use -c to specify configuration file")
}
var f *os.File
if f, err = os.Open(cfg); err != nil {
return
}
defer f.Close()
ConfigFile = cfg
var c GlobalConfig
d := json.NewDecoder(f)
if err = d.Decode(&c); err != nil {
return
}
if err = c.Validate(); err != nil {
return
}
lock.Lock()
defer lock.Unlock()
config = &c
log.Println("read config file:", cfg, "successfully")
return Init()
}
func Init() (err error) {
// generate dynamic js file
jsFile := path.Join(SystemDynamicCodeJsDir, SystemDynamicCodeJsName)
if util.Exist(jsFile) {
os.Remove(jsFile)
}
if !util.Exist(jsFile) {
if err = os.MkdirAll(SystemDynamicCodeJsDir, 0777); err != nil {
log.Fatalln("mkdir for", SystemDynamicCodeJsDir, "failed, err is", err)
return
}
}
var fileOut *os.File
if fileOut, err = os.Create(jsFile); err != nil {
log.Println("unable to create js file:%v, err is %v", jsFile, err)
return
}
defer fileOut.Close()
fileOut.WriteString(fmt.Sprintf("var UMS_ROOT = '%s'; \r\n", config.Playout.Ums))
fileOut.WriteString(fmt.Sprintf("var BPO_ROOT = '%s:%d';\n", config.Playout.Ip, config.Listen))
// check resource video dir exist
if !util.Exist(config.Resources.VideoDir) {
if err = os.MkdirAll(config.Resources.VideoDir, 0777); err != nil {
log.Fatalln("mkdir for", config.Resources.VideoDir, "failed, err is", err)
return
}
}
return
}