8000 Config using viper · invisiblecoder99/livego@b6e9ba1 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit b6e9ba1

Browse files
committed
Config using viper
1 parent b464789 commit b6e9ba1

File tree

12 files changed

+291
-130
lines changed

12 files changed

+291
-130
lines changed

AG

Whitespace-only changes.

configure/channel.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ var RoomKeys = &RoomKeysType{
2222
var saveInLocal = true
2323

2424
func Init() {
25-
saveInLocal = GetRedisAddr() == nil
25+
saveInLocal = len(Config.GetString("redis_addr")) == 0
2626
if saveInLocal {
2727
return
2828
}
2929

3030
RoomKeys.redisCli = redis.NewClient(&redis.Options{
31-
Addr: *GetRedisAddr(),
32-
Password: *GetRedisPwd(),
31+
Addr: Config.GetString("redis_addr"),
32+
Password: Config.GetString("redis_pwd"),
3333
DB: 0,
3434
})
3535

configure/liveconfig.go

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package configure
22

33
import (
4+
"bytes"
45
"encoding/json"
5-
"flag"
6-
"io/ioutil"
6+
"fmt"
7+
"strings"
78

9+
"github.com/kr/pretty"
810
log "github.com/sirupsen/logrus"
11+
"github.com/spf13/pflag"
12+
"github.com/spf13/viper"
913
)
1014

1115
/*
@@ -20,30 +24,47 @@ import (
2024
]
2125
}
2226
*/
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-
)
2727

2828
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"`
3333
}
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"`
3740
}
3841
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"`
4356
}
4457

4558
// 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,
4768
Server: []Application{{
4869
Appname: "livego",
4970
Live: true,
@@ -52,47 +73,65 @@ var RtmpServercfg = ServerCfg{
5273
}},
5374
}
5475

55-
func LoadConfig(configfilename string) {
56-
defer Init()
76+
var Config = viper.New()
5777

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)
6482
}
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)
7283
}
7384

74-
func GetRedisAddr() *string {
75-
if len(RtmpServercfg.RedisAddr) > 0 {
76-
*redisAddr = RtmpServercfg.RedisAddr
77-
}
85+
func LoadConfig() {
86+
defer Init()
7887

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")
81114
}
82115

83-
return redisAddr
84-
}
116+
// Environment
117+
replacer := strings.NewReplacer(".", "_")
118+
Config.SetEnvKeyReplacer(replacer)
119+
Config.AllowEmptyEnv(true)
120+
Config.AutomaticEnv()
85121

86-
func GetRedisPwd() *string {
87-
if len(RtmpServercfg.RedisPwd) > 0 {
88-
*redisPwd = RtmpServercfg.RedisPwd
89-
}
122+
// Log
123+
initLog()
90124

91-
return redisPwd
125+
c := ServerCfg{}
126+
Config.Unmarshal(&c)
127+
log.Debugf("Current configurations: \n%# v", pretty.Formatter(c))
92128
}
93129

94130
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 {
96135
if app.Appname == appname {
97136
return app.Live
98137
}
@@ -101,15 +140,16 @@ func CheckAppName(appname string) bool {
101140
}
102141

103142
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 {
105146
if (app.Appname == appname) && app.Live {
106147
if len(app.StaticPush) > 0 {
107148
return app.StaticPush, true
108149
} else {
109150
return nil, false
110151
}
111152
}
112-
113153
}
114154
return nil, false
115155
}

container/flv/muxer.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package flv
22

33
import (
4-
"flag"
54
"fmt"
65
"os"
76
"path"
87
"strings"
98
"time"
109

1110
"livego/av"
11+
"livego/configure"
1212
"livego/protocol/amf"
1313
"livego/utils/pio"
1414
"livego/utils/uid"
@@ -18,7 +18,6 @@ import (
1818

1919
var (
2020
flvHeader = []byte{0x46, F438 0x4c, 0x56, 0x01, 0x05, 0x00, 0x00, 0x00, 0x09}
21-
flvDir = flag.String("flvDir", "tmp", "output flv file at flvDir/APP/KEY_TIME.flv")
2221
)
2322

2423
/*
@@ -152,13 +151,15 @@ func (f *FlvDvr) GetWriter(info av.Info) av.WriteCloser {
152151
return nil
153152
}
154153

155-
err := os.MkdirAll(path.Join(*flvDir, paths[0]), 0755)
154+
flvDir := configure.Config.GetString("flv_dir")
155+
156+
err := os.MkdirAll(path.Join(flvDir, paths[0]), 0755)
156157
if err != nil {
157158
log.Error("mkdir error: ", err)
158159
return nil
159160
}
160161

161-
fileName := fmt.Sprintf("%s_%d.%s", path.Join(*flvDir, info.Key), time.Now().Unix(), "flv")
162+
fileName := fmt.Sprintf("%s_%d.%s", path.Join(flvDir, info.Key), time.Now().Unix(), "flv")
162163
log.Debug("flv dvr save stream to: ", fileName)
163164
w, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0755)
164165
if err != nil {

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ require (
77
github.com/dgrijalva/jwt-go v3.2.0+incompatible
88
github.com/go-redis/redis/v7 v7.2.0
99
github.com/gorilla/mux v1.7.4 // indirect
10+
github.com/kr/pretty v0.1.0
1011
github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6
1112
github.com/patrickmn/go-cache v2.1.0+incompatible
1213
github.com/satori/go.uuid v1.2.0
1314
github.com/sirupsen/logrus v1.5.0
14-
github.com/smartystreets/goconvey v1.6.4 // indirect
15+
github.com/spf13/pflag v1.0.3
16+
github.com/spf13/viper v1.6.3
1517
github.com/stretchr/testify v1.4.0
1618
github.com/urfave/negroni v1.0.0 // indirect
1719
)

0 commit comments

Comments
 (0)
0