1
1
package configure
2
2
3
3
import (
4
+ "bytes"
4
5
"encoding/json"
5
- "flag "
6
- "io/ioutil "
6
+ "fmt "
7
+ "strings "
7
8
9
+ "github.com/kr/pretty"
8
10
log "github.com/sirupsen/logrus"
11
+ "github.com/spf13/pflag"
12
+ "github.com/spf13/viper"
9
13
)
10
14
11
15
/*
@@ -20,30 +24,47 @@ import (
20
24
]
21
25
}
22
26
*/
23
- var (
24
- redisAddr = flag .String ("redis_addr" , "" , "redis addr to save room keys ex. localhost:6379" )
25
- redisPwd = flag .String ("redis_pwd" , "" , "redis password" )
26
- )
27
27
28
28
type Application struct {
29
- Appname string `json :"appname"`
30
- Live bool `json:"liveon "`
31
- Hls bool `json :"hls"`
32
- StaticPush []string `json :"static_push"`
29
+ Appname string `mapstructure :"appname"`
30
+ Live bool `mapstructure:"live "`
31
+ Hls bool `mapstructure :"hls"`
32
+ StaticPush []string `mapstructure :"static_push"`
33
33
}
34
- type JWTCfg struct {
35
- Secret string `json:"secret"`
36
- Algorithm string `json:"algorithm"`
34
+
35
+ type Applications []Application
36
+
37
+ type JWT struct {
38
+ Secret string `mapstructure:"secret"`
39
+ Algorithm string `mapstructure:"algorithm"`
37
40
}
38
41
type ServerCfg struct {
39
- RedisAddr string `json:"redis_addr"`
40
- RedisPwd string `json:"redis_pwd"`
41
- JWTCfg `json:"jwt"`
42
- Server []Application `json:"server"`
42
+ Level string `mapstructure:"level"`
43
+ ConfigFile string `mapstructure:"config_file"`
44
+ FLVDir string `mapstructure:"flv_dir"`
45
+ RTMPAddr string `mapstructure:"rtmp_addr"`
46
+ HTTPFLVAddr string `mapstructure:"httpflv_addr"`
47
+ HLSAddr string `mapstructure:"hls_addr"`
48
+ APIAddr string `mapstructure:"api_addr"`
49
+ RedisAddr string `mapstructure:"redis_addr"`
50
+ RedisPwd string `mapstructure:"redis_pwd"`
51
+ ReadTimeout int `mapstructure:"read_timeout"`
52
+ WriteTimeout int `mapstructure:"write_timeout"`
53
+ GopNum int `mapstructure:"gop_num"`
54
+ JWT JWT `mapstructure:"jwt"`
55
+ Server []Application `mapstructure:"server"`
43
56
}
44
57
45
58
// default config
46
- var RtmpServercfg = ServerCfg {
59
+ var defaultConf = ServerCfg {
60
+ ConfigFile : "livego.yaml" ,
61
+ RTMPAddr : ":1935" ,
62
+ HTTPFLVAddr : ":7001" ,
63
+ HLSAddr : ":7002" ,
64
+ APIAddr : ":8090" ,
65
+ WriteTimeout : 10 ,
66
+ ReadTimeout : 10 ,
67
+ GopNum : 1 ,
47
68
Server : []Application {{
48
69
Appname : "livego" ,
49
70
Live : true ,
@@ -52,47 +73,65 @@ var RtmpServercfg = ServerCfg{
52
73
}},
53
74
}
54
75
55
- func LoadConfig (configfilename string ) {
56
- defer Init ()
76
+ var Config = viper .New ()
57
77
58
- log .Infof ("starting load configure file %s" , configfilename )
59
- data , err := ioutil .ReadFile (configfilename )
60
- if err != nil {
61
- log .Warningf ("ReadFile %s error:%v" , configfilename , err )
62
- log .Info ("Using default config" )
63
- return
78
+ func initLog () {
79
+ if l , err := log .ParseLevel (Config .GetString ("level" )); err == nil {
80
+ log .SetLevel (l )
81
+ log .SetReportCaller (l == log .DebugLevel )
64
82
}
65
-
66
- err = json .Unmarshal (data , & RtmpServercfg )
67
- if err != nil {
68
- log .Errorf ("json.Unmarshal error:%v" , err )
69
- log .Info ("Using default config" )
70
- }
71
- log .Debugf ("get config json data:%v" , RtmpServercfg )
72
83
}
73
84
74
- func GetRedisAddr () * string {
75
- if len (RtmpServercfg .RedisAddr ) > 0 {
76
- * redisAddr = RtmpServercfg .RedisAddr
77
- }
85
+ func LoadConfig () {
86
+ defer Init ()
78
87
79
- if len (* redisAddr ) == 0 {
80
- return nil
88
+ // Default config
89
+ b , _ := json .Marshal (defaultConf )
90
+ defaultConfig := bytes .NewReader (b )
91
+ Config .MergeConfig (defaultConfig )
92
+
93
+ // Flags
94
+ pflag .String ("rtmp_addr" , ":1935" , "RTMP server listen address" )
95
+ pflag .String ("httpflv_addr" , ":7001" , "HTTP-FLV server listen address" )
96
+ pflag .String ("hls_addr" , ":7002" , "HLS server listen address" )
97
+ pflag .String ("api_addr" , ":8090" , "HTTP manage interface server listen address" )
98
+ pflag .String ("config_file" , "livego.yaml" , "configure filename" )
99
+ pflag .String ("level" , "info" , "Log level" )
100
+ pflag .String ("flv_dir" , "tmp" , "output flv file at flvDir/APP/KEY_TIME.flv" )
101
+ pflag .Int ("read_timeout" , 10 , "read time out" )
102
+ pflag .Int ("write_timeout" , 10 , "write time out" )
103
+ pflag .Int ("gop_num" , 1 , "gop num" )
104
+ pflag .Parse ()
105
+ Config .BindPFlags (pflag .CommandLine )
106
+
107
+ // File
108
+ Config .SetConfigFile (Config .GetString ("config_file" ))
109
+ Config .AddConfigPath ("." )
110
+ err := Config .ReadInConfig ()
111
+ if err != nil {
112
+ log .Error (err )
113
+ log .Info ("Using default config" )
81
114
}
82
115
83
- return redisAddr
84
- }
116
+ // Environment
117
+ replacer := strings .NewReplacer ("." , "_" )
118
+ Config .SetEnvKeyReplacer (replacer )
119
+ Config .AllowEmptyEnv (true )
120
+ Config .AutomaticEnv ()
85
121
86
- func GetRedisPwd () * string {
87
- if len (RtmpServercfg .RedisPwd ) > 0 {
88
- * redisPwd = RtmpServercfg .RedisPwd
89
- }
122
+ // Log
123
+ initLog ()
90
124
91
- return redisPwd
125
+ c := ServerCfg {}
126
+ Config .Unmarshal (& c )
127
+ log .Debugf ("Current configurations: \n %# v" , pretty .Formatter (c ))
92
128
}
93
129
94
130
func CheckAppName (appname string ) bool {
95
- for _ , app := range RtmpServercfg .Server {
131
+ apps := Applications {}
132
+ Config .UnmarshalKey ("server" , & apps )
133
+ fmt .Println (apps )
134
+ for _ , app := range apps {
96
135
if app .Appname == appname {
97
136
return app .Live
98
137
}
@@ -101,15 +140,16 @@ func CheckAppName(appname string) bool {
101
140
}
102
141
103
142
func GetStaticPushUrlList (appname string ) ([]string , bool ) {
104
- for _ , app := range RtmpServercfg .Server {
143
+ apps := Applications {}
144
+ Config .UnmarshalKey ("server" , & apps )
145
+ for _ , app := range apps {
105
146
if (app .Appname == appname ) && app .Live {
106
147
if len (app .StaticPush ) > 0 {
107
148
return app .StaticPush , true
108
149
} else {
109
150
return nil , false
110
151
}
111
152
}
112
-
113
153
}
114
154
return nil , false
115
155
}
0 commit comments