From b6e9ba10ffdfcc2b03a2f7c9017a554475230604 Mon Sep 17 00:00:00 2001
From: Ruben Cid <rubencidlara@gmail.com>
Date: Tue, 14 Apr 2020 13:57:32 +0200
Subject: [PATCH] Config using viper

---
 AG                           |   0
 configure/channel.go         |   6 +-
 configure/liveconfig.go      | 138 ++++++++++++++++++++++-------------
 container/flv/muxer.go       |   9 ++-
 go.mod                       |   4 +-
 go.sum                       | 119 ++++++++++++++++++++++++++++++
 livego.json                  |   9 ---
 livego.yaml                  |  22 ++++++
 main.go                      |  56 ++++++--------
 protocol/api/api.go          |  40 +++++-----
 protocol/rtmp/cache/cache.go |   9 +--
 protocol/rtmp/rtmp.go        |   9 +--
 12 files changed, 291 insertions(+), 130 deletions(-)
 delete mode 100644 AG
 delete mode 100644 livego.json
 create mode 100644 livego.yaml

diff --git a/AG b/AG
deleted file mode 100644
index e69de29..0000000
diff --git a/configure/channel.go b/configure/channel.go
index 41acbc2..8a092dd 100755
--- a/configure/channel.go
+++ b/configure/channel.go
@@ -22,14 +22,14 @@ var RoomKeys = &RoomKeysType{
 var saveInLocal = true
 
 func Init() {
-	saveInLocal = GetRedisAddr() == nil
+	saveInLocal = len(Config.GetString("redis_addr")) == 0
 	if saveInLocal {
 		return
 	}
 
 	RoomKeys.redisCli = redis.NewClient(&redis.Options{
-		Addr:     *GetRedisAddr(),
-		Password: *GetRedisPwd(),
+		Addr:     Config.GetString("redis_addr"),
+		Password: Config.GetString("redis_pwd"),
 		DB:       0,
 	})
 
diff --git a/configure/liveconfig.go b/configure/liveconfig.go
index 5eecef6..d856f29 100644
--- a/configure/liveconfig.go
+++ b/configure/liveconfig.go
@@ -1,11 +1,15 @@
 package configure
 
 import (
+	"bytes"
 	"encoding/json"
-	"flag"
-	"io/ioutil"
+	"fmt"
+	"strings"
 
+	"github.com/kr/pretty"
 	log "github.com/sirupsen/logrus"
+	"github.com/spf13/pflag"
+	"github.com/spf13/viper"
 )
 
 /*
@@ -20,30 +24,47 @@ import (
   ]
 }
 */
-var (
-	redisAddr = flag.String("redis_addr", "", "redis addr to save room keys ex. localhost:6379")
-	redisPwd  = flag.String("redis_pwd", "", "redis password")
-)
 
 type Application struct {
-	Appname    string   `json:"appname"`
-	Live       bool     `json:"liveon"`
-	Hls        bool     `json:"hls"`
-	StaticPush []string `json:"static_push"`
+	Appname    string   `mapstructure:"appname"`
+	Live       bool     `mapstructure:"live"`
+	Hls        bool     `mapstructure:"hls"`
+	StaticPush []string `mapstructure:"static_push"`
 }
-type JWTCfg struct {
-	Secret    string `json:"secret"`
-	Algorithm string `json:"algorithm"`
+
+type Applications []Application
+
+type JWT struct {
+	Secret    string `mapstructure:"secret"`
+	Algorithm string `mapstructure:"algorithm"`
 }
 type ServerCfg struct {
-	RedisAddr string `json:"redis_addr"`
-	RedisPwd  string `json:"redis_pwd"`
-	JWTCfg    `json:"jwt"`
-	Server    []Application `json:"server"`
+	Level        string        `mapstructure:"level"`
+	ConfigFile   string        `mapstructure:"config_file"`
+	FLVDir       string        `mapstructure:"flv_dir"`
+	RTMPAddr     string        `mapstructure:"rtmp_addr"`
+	HTTPFLVAddr  string        `mapstructure:"httpflv_addr"`
+	HLSAddr      string        `mapstructure:"hls_addr"`
+	APIAddr      string        `mapstructure:"api_addr"`
+	RedisAddr    string        `mapstructure:"redis_addr"`
+	RedisPwd     string        `mapstructure:"redis_pwd"`
+	ReadTimeout  int           `mapstructure:"read_timeout"`
+	WriteTimeout int           `mapstructure:"write_timeout"`
+	GopNum       int           `mapstructure:"gop_num"`
+	JWT          JWT           `mapstructure:"jwt"`
+	Server       []Application `mapstructure:"server"`
 }
 
 // default config
-var RtmpServercfg = ServerCfg{
+var defaultConf = ServerCfg{
+	ConfigFile:   "livego.yaml",
+	RTMPAddr:     ":1935",
+	HTTPFLVAddr:  ":7001",
+	HLSAddr:      ":7002",
+	APIAddr:      ":8090",
+	WriteTimeout: 10,
+	ReadTimeout:  10,
+	GopNum:       1,
 	Server: []Application{{
 		Appname:    "livego",
 		Live:       true,
@@ -52,47 +73,65 @@ var RtmpServercfg = ServerCfg{
 	}},
 }
 
-func LoadConfig(configfilename string) {
-	defer Init()
+var Config = viper.New()
 
-	log.Infof("starting load configure file %s", configfilename)
-	data, err := ioutil.ReadFile(configfilename)
-	if err != nil {
-		log.Warningf("ReadFile %s error:%v", configfilename, err)
-		log.Info("Using default config")
-		return
+func initLog() {
+	if l, err := log.ParseLevel(Config.GetString("level")); err == nil {
+		log.SetLevel(l)
+		log.SetReportCaller(l == log.DebugLevel)
 	}
-
-	err = json.Unmarshal(data, &RtmpServercfg)
-	if err != nil {
-		log.Errorf("json.Unmarshal error:%v", err)
-		log.Info("Using default config")
-	}
-	log.Debugf("get config json data:%v", RtmpServercfg)
 }
 
-func GetRedisAddr() *string {
-	if len(RtmpServercfg.RedisAddr) > 0 {
-		*redisAddr = RtmpServercfg.RedisAddr
-	}
+func LoadConfig() {
+	defer Init()
 
-	if len(*redisAddr) == 0 {
-		return nil
+	// Default config
+	b, _ := json.Marshal(defaultConf)
+	defaultConfig := bytes.NewReader(b)
+	Config.MergeConfig(defaultConfig)
+
+	// Flags
+	pflag.String("rtmp_addr", ":1935", "RTMP server listen address")
+	pflag.String("httpflv_addr", ":7001", "HTTP-FLV server listen address")
+	pflag.String("hls_addr", ":7002", "HLS server listen address")
+	pflag.String("api_addr", ":8090", "HTTP manage interface server listen address")
+	pflag.String("config_file", "livego.yaml", "configure filename")
+	pflag.String("level", "info", "Log level")
+	pflag.String("flv_dir", "tmp", "output flv file at flvDir/APP/KEY_TIME.flv")
+	pflag.Int("read_timeout", 10, "read time out")
+	pflag.Int("write_timeout", 10, "write time out")
+	pflag.Int("gop_num", 1, "gop num")
+	pflag.Parse()
+	Config.BindPFlags(pflag.CommandLine)
+
+	// File
+	Config.SetConfigFile(Config.GetString("config_file"))
+	Config.AddConfigPath(".")
+	err := Config.ReadInConfig()
+	if err != nil {
+		log.Error(err)
+		log.Info("Using default config")
 	}
 
-	return redisAddr
-}
+	// Environment
+	replacer := strings.NewReplacer(".", "_")
+	Config.SetEnvKeyReplacer(replacer)
+	Config.AllowEmptyEnv(true)
+	Config.AutomaticEnv()
 
-func GetRedisPwd() *string {
-	if len(RtmpServercfg.RedisPwd) > 0 {
-		*redisPwd = RtmpServercfg.RedisPwd
-	}
+	// Log
+	initLog()
 
-	return redisPwd
+	c := ServerCfg{}
+	Config.Unmarshal(&c)
+	log.Debugf("Current configurations: \n%# v", pretty.Formatter(c))
 }
 
 func CheckAppName(appname string) bool {
-	for _, app := range RtmpServercfg.Server {
+	apps := Applications{}
+	Config.UnmarshalKey("server", &apps)
+	fmt.Println(apps)
+	for _, app := range apps {
 		if app.Appname == appname {
 			return app.Live
 		}
@@ -101,7 +140,9 @@ func CheckAppName(appname string) bool {
 }
 
 func GetStaticPushUrlList(appname string) ([]string, bool) {
-	for _, app := range RtmpServercfg.Server {
+	apps := Applications{}
+	Config.UnmarshalKey("server", &apps)
+	for _, app := range apps {
 		if (app.Appname == appname) && app.Live {
 			if len(app.StaticPush) > 0 {
 				return app.StaticPush, true
@@ -109,7 +150,6 @@ func GetStaticPushUrlList(appname string) ([]string, bool) {
 				return nil, false
 			}
 		}
-
 	}
 	return nil, false
 }
diff --git a/container/flv/muxer.go b/container/flv/muxer.go
index 2c94331..f7ef137 100755
--- a/container/flv/muxer.go
+++ b/container/flv/muxer.go
@@ -1,7 +1,6 @@
 package flv
 
 import (
-	"flag"
 	"fmt"
 	"os"
 	"path"
@@ -9,6 +8,7 @@ import (
 	"time"
 
 	"livego/av"
+	"livego/configure"
 	"livego/protocol/amf"
 	"livego/utils/pio"
 	"livego/utils/uid"
@@ -18,7 +18,6 @@ import (
 
 var (
 	flvHeader = []byte{0x46, 0x4c, 0x56, 0x01, 0x05, 0x00, 0x00, 0x00, 0x09}
-	flvDir    = flag.String("flvDir", "tmp", "output flv file at flvDir/APP/KEY_TIME.flv")
 )
 
 /*
@@ -152,13 +151,15 @@ func (f *FlvDvr) GetWriter(info av.Info) av.WriteCloser {
 		return nil
 	}
 
-	err := os.MkdirAll(path.Join(*flvDir, paths[0]), 0755)
+	flvDir := configure.Config.GetString("flv_dir")
+
+	err := os.MkdirAll(path.Join(flvDir, paths[0]), 0755)
 	if err != nil {
 		log.Error("mkdir error: ", err)
 		return nil
 	}
 
-	fileName := fmt.Sprintf("%s_%d.%s", path.Join(*flvDir, info.Key), time.Now().Unix(), "flv")
+	fileName := fmt.Sprintf("%s_%d.%s", path.Join(flvDir, info.Key), time.Now().Unix(), "flv")
 	log.Debug("flv dvr save stream to: ", fileName)
 	w, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0755)
 	if err != nil {
diff --git a/go.mod b/go.mod
index 86ea16a..e1cf69e 100644
--- a/go.mod
+++ b/go.mod
@@ -7,11 +7,13 @@ require (
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/go-redis/redis/v7 v7.2.0
 	github.com/gorilla/mux v1.7.4 // indirect
+	github.com/kr/pretty v0.1.0
 	github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6
 	github.com/patrickmn/go-cache v2.1.0+incompatible
 	github.com/satori/go.uuid v1.2.0
 	github.com/sirupsen/logrus v1.5.0
-	github.com/smartystreets/goconvey v1.6.4 // indirect
+	github.com/spf13/pflag v1.0.3
+	github.com/spf13/viper v1.6.3
 	github.com/stretchr/testify v1.4.0
 	github.com/urfave/negroni v1.0.0 // indirect
 )
diff --git a/go.sum b/go.sum
index b745730..a4a636c 100644
--- a/go.sum
+++ b/go.sum
@@ -1,32 +1,85 @@
+cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
+github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
+github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
+github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
+github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
+github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
 github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b h1:CvoEHGmxWl5kONC5icxwqV899dkf4VjOScbxLpllEnw=
 github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b/go.mod h1:LWMyo4iOLWXHGdBki7NIht1kHru/0wM179h+d3g8ATM=
+github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
+github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
+github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
+github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
+github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
+github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
+github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
+github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
+github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
 github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
+github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
 github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
+github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
+github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
+github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
+github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
 github.com/go-redis/redis/v7 v7.2.0 h1:CrCexy/jYWZjW0AyVoHlcJUeZN19VWlbepTh1Vq6dJs=
 github.com/go-redis/redis/v7 v7.2.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
+github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
+github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
+github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
+github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
+github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
+github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
 github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
+github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
+github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
 github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
 github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
+github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
+github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
+github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
+github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
+github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
+github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
+github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
+github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
 github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
 github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
+github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
+github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
+github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
 github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
+github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
 github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
+github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
+github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
+github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
+github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
+github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
+github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
+github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
+github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
+github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
 github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
 github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
 github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -36,29 +89,80 @@ github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6 h1:lNCW6THr
 github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
 github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
 github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
+github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
+github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
+github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
+github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
+github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
+github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
+github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
+github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
+github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
+github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
+github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
+github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
 github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
 github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
+github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
 github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q=
 github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
 github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
 github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
+github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
+github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
+github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
+github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
+github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
+github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
+github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
+github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
+github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
+github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
+github.com/spf13/viper v1.6.3 h1:pDDu1OyEDTKzpJwdq4TiuLyMsUgRa/BT5cn5O62NoHs=
+github.com/spf13/viper v1.6.3/go.mod h1:jUMtyi0/lB5yZH/FjyGAoH7IMNrIhlBf6pXZmbMDvzw=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
+github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
+github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
+github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
 github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
 github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
+github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
+github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
+go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
+go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
+go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
+go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
+golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
+golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
+golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
 golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
 golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
@@ -66,16 +170,31 @@ golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7w
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
+golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
 golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
+google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
+google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
+google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
+google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
+gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
 gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
+gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
+gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
+gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
+gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
 gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
diff --git a/livego.json b/livego.json
deleted file mode 100644
index 6413cfc..0000000
--- a/livego.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
-    "server": [
-        {
-            "appname": "livego",
-            "live": true,
-            "hls": true
-        }
-    ]
-}
diff --git a/livego.yaml b/livego.yaml
new file mode 100644
index 0000000..8a034de
--- /dev/null
+++ b/livego.yaml
@@ -0,0 +1,22 @@
+# # Logger level
+# level: info
+
+# # FLV Options
+# flv_dir: "./tmp"
+# httpflv_addr: ":7001"
+
+# # RTMP Options
+# rtmp_addr: ":1935"
+# read_timeout: 10
+# write_timeout: 10
+
+# # HLS Options
+# hls_addr: ":7002"
+
+# # API Options
+# api_addr: ":8090"
+
+server:
+- appname: livego
+  live: true
+  hls: true
diff --git a/main.go b/main.go
index 36003d4..e3a7cb2 100755
--- a/main.go
+++ b/main.go
@@ -1,7 +1,6 @@
 package main
 
 import (
-	"flag"
 	"fmt"
 	"livego/configure"
 	"livego/protocol/api"
@@ -18,17 +17,9 @@ import (
 
 var VERSION = "master"
 
-var (
-	rtmpAddr       = flag.String("rtmp-addr", ":1935", "RTMP server listen address")
-	httpFlvAddr    = flag.String("httpflv-addr", ":7001", "HTTP-FLV server listen address")
-	hlsAddr        = flag.String("hls-addr", ":7002", "HLS server listen address")
-	operaAddr      = flag.String("manage-addr", ":8090", "HTTP manage interface server listen address")
-	configfilename = flag.String("config-file", "livego.json", "configure filename")
-	levelLog       = flag.String("level", "info", "Log level")
-)
-
 func startHls() *hls.Server {
-	hlsListen, err := net.Listen("tcp", *hlsAddr)
+	hlsAddr := configure.Config.GetString("hls_addr")
+	hlsListen, err := net.Listen("tcp", hlsAddr)
 	if err != nil {
 		log.Fatal(err)
 	}
@@ -40,14 +31,18 @@ func startHls() *hls.Server {
 				log.Error("HLS server panic: ", r)
 			}
 		}()
-		log.Info("HLS listen On ", *hlsAddr)
+		log.Info("HLS listen On ", hlsAddr)
 		hlsServer.Serve(hlsListen)
 	}()
 	return hlsServer
 }
 
+var rtmpAddr string
+
 func startRtmp(stream *rtmp.RtmpStream, hlsServer *hls.Server) {
-	rtmpListen, err := net.Listen("tcp", *rtmpAddr)
+	rtmpAddr = configure.Config.GetString("rtmp_addr")
+
+	rtmpListen, err := net.Listen("tcp", rtmpAddr)
 	if err != nil {
 		log.Fatal(err)
 	}
@@ -56,10 +51,10 @@ func startRtmp(stream *rtmp.RtmpStream, hlsServer *hls.Server) {
 
 	if hlsServer == nil {
 		rtmpServer = rtmp.NewRtmpServer(stream, nil)
-		log.Info("hls server disable....")
+		log.Info("HLS server disable....")
 	} else {
 		rtmpServer = rtmp.NewRtmpServer(stream, hlsServer)
-		log.Info("hls server enable....")
+		log.Info("HLS server enable....")
 	}
 
 	defer func() {
@@ -67,12 +62,14 @@ func startRtmp(stream *rtmp.RtmpStream, hlsServer *hls.Server) {
 			log.Error("RTMP server panic: ", r)
 		}
 	}()
-	log.Info("RTMP Listen On ", *rtmpAddr)
+	log.Info("RTMP Listen On ", rtmpAddr)
 	rtmpServer.Serve(rtmpListen)
 }
 
 func startHTTPFlv(stream *rtmp.RtmpStream) {
-	flvListen, err := net.Listen("tcp", *httpFlvAddr)
+	httpflvAddr := configure.Config.GetString("httpflv_addr")
+
+	flvListen, err := net.Listen("tcp", httpflvAddr)
 	if err != nil {
 		log.Fatal(err)
 	}
@@ -84,33 +81,33 @@ func startHTTPFlv(stream *rtmp.RtmpStream) {
 				log.Error("HTTP-FLV server panic: ", r)
 			}
 		}()
-		log.Info("HTTP-FLV listen On ", *httpFlvAddr)
+		log.Info("HTTP-FLV listen On ", httpflvAddr)
 		hdlServer.Serve(flvListen)
 	}()
 }
 
 func startAPI(stream *rtmp.RtmpStream) {
-	if *operaAddr != "" {
-		opListen, err := net.Listen("tcp", *operaAddr)
+	apiAddr := configure.Config.GetString("api_addr")
+
+	if apiAddr != "" {
+		opListen, err := net.Listen("tcp", apiAddr)
 		if err != nil {
 			log.Fatal(err)
 		}
-		opServer := api.NewServer(stream, *rtmpAddr)
+		opServer := api.NewServer(stream, rtmpAddr)
 		go func() {
 			defer func() {
 				if r := recover(); r != nil {
 					log.Error("HTTP-API server panic: ", r)
 				}
 			}()
-			log.Info("HTTP-API listen On ", *operaAddr)
+			log.Info("HTTP-API listen On ", apiAddr)
 			opServer.Serve(opListen)
 		}()
 	}
 }
 
-func InitLog() {
-	flag.Parse()
-
+func init() {
 	log.SetFormatter(&log.TextFormatter{
 		FullTimestamp: true,
 		CallerPrettyfier: func(f *runtime.Frame) (string, string) {
@@ -118,11 +115,6 @@ func InitLog() {
 			return fmt.Sprintf("%s()", f.Function), fmt.Sprintf(" %s:%d", filename, f.Line)
 		},
 	})
-
-	if l, err := log.ParseLevel(*levelLog); err == nil {
-		log.SetLevel(l)
-		log.SetReportCaller(l == log.DebugLevel)
-	}
 }
 
 func main() {
@@ -133,8 +125,7 @@ func main() {
 		}
 	}()
 
-	// Log options
-	InitLog()
+	configure.LoadConfig()
 
 	log.Infof(`
      _     _            ____       
@@ -144,7 +135,6 @@ func main() {
     |_____|_| \_/ \___|\____|\___/ 
         version: %s
 	`, VERSION)
-	configure.LoadConfig(*configfilename)
 
 	stream := rtmp.NewRtmpStream()
 	hlsServer := startHls()
diff --git a/protocol/api/api.go b/protocol/api/api.go
index 56a5ff1..2b242a8 100755
--- a/protocol/api/api.go
+++ b/protocol/api/api.go
@@ -63,30 +63,32 @@ func NewServer(h av.Handler, rtmpAddr string) *Server {
 }
 
 func JWTMiddleware(next http.Handler) http.Handler {
-	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-		if len(configure.RtmpServercfg.JWTCfg.Secret) > 0 {
-			var algorithm jwt.SigningMethod
-			if len(configure.RtmpServercfg.JWTCfg.Algorithm) > 0 {
-				algorithm = jwt.GetSigningMethod(configure.RtmpServercfg.JWTCfg.Algorithm)
-			}
+	isJWT := len(configure.Config.GetString("jwt.secret")) > 0
+	if !isJWT {
+		return next
+	}
 
-			if algorithm == nil {
-				algorithm = jwt.SigningMethodHS256
-			}
+	log.Info("Using JWT middleware")
 
-			jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
-				Extractor: jwtmiddleware.FromFirst(jwtmiddleware.FromAuthHeader, jwtmiddleware.FromParameter("jwt")),
-				ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
-					return []byte(configure.RtmpServercfg.Secret), nil
-				},
-				SigningMethod: algorithm,
-			})
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		var algorithm jwt.SigningMethod
+		if len(configure.Config.GetString("jwt.algorithm")) > 0 {
+			algorithm = jwt.GetSigningMethod(configure.Config.GetString("jwt.algorithm"))
+		}
 
-			jwtMiddleware.HandlerWithNext(w, r, next.ServeHTTP)
-			return
+		if algorithm == nil {
+			algorithm = jwt.SigningMethodHS256
 		}
-		next.ServeHTTP(w, r)
 
+		jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
+			Extractor: jwtmiddleware.FromFirst(jwtmiddleware.FromAuthHeader, jwtmiddleware.FromParameter("jwt")),
+			ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
+				return []byte(configure.Config.GetString("jwt.secret")), nil
+			},
+			SigningMethod: algorithm,
+		})
+
+		jwtMiddleware.HandlerWithNext(w, r, next.ServeHTTP)
 	})
 }
 
diff --git a/protocol/rtmp/cache/cache.go b/protocol/rtmp/cache/cache.go
index b644e94..71af1de 100755
--- a/protocol/rtmp/cache/cache.go
+++ b/protocol/rtmp/cache/cache.go
@@ -1,13 +1,8 @@
 package cache
 
 import (
-	"flag"
-
 	"livego/av"
-)
-
-var (
-	gopNum = flag.Int("gopNum", 1, "gop num")
+	"livego/configure"
 )
 
 type Cache struct {
@@ -19,7 +14,7 @@ type Cache struct {
 
 func NewCache() *Cache {
 	return &Cache{
-		gop:      NewGopCache(*gopNum),
+		gop:      NewGopCache(configure.Config.GetInt("gop_num")),
 		videoSeq: NewSpecialCache(),
 		audioSeq: NewSpecialCache(),
 		metadata: NewSpecialCache(),
diff --git a/protocol/rtmp/rtmp.go b/protocol/rtmp/rtmp.go
index f446ad6..a724995 100755
--- a/protocol/rtmp/rtmp.go
+++ b/protocol/rtmp/rtmp.go
@@ -1,7 +1,6 @@
 package rtmp
 
 import (
-	"flag"
 	"fmt"
 	"net"
 	"net/url"
@@ -25,8 +24,8 @@ const (
 )
 
 var (
-	readTimeout  = flag.Int("readTimeout", 10, "read time out")
-	writeTimeout = flag.Int("writeTimeout", 10, "write time out")
+	readTimeout  = configure.Config.GetInt("read_timeout")
+	writeTimeout = configure.Config.GetInt("write_timeout")
 )
 
 type Client struct {
@@ -192,7 +191,7 @@ func NewVirWriter(conn StreamReadWriteCloser) *VirWriter {
 	ret := &VirWriter{
 		Uid:         uid.NewId(),
 		conn:        conn,
-		RWBaser:     av.NewRWBaser(time.Second * time.Duration(*writeTimeout)),
+		RWBaser:     av.NewRWBaser(time.Second * time.Duration(writeTimeout)),
 		packetQueue: make(chan *av.Packet, maxQueueNum),
 		WriteBWInfo: StaticsBW{0, 0, 0, 0, 0, 0, 0, 0},
 	}
@@ -367,7 +366,7 @@ func NewVirReader(conn StreamReadWriteCloser) *VirReader {
 	return &VirReader{
 		Uid:        uid.NewId(),
 		conn:       conn,
-		RWBaser:    av.NewRWBaser(time.Second * time.Duration(*writeTimeout)),
+		RWBaser:    av.NewRWBaser(time.Second * time.Duration(writeTimeout)),
 		demuxer:    flv.NewDemuxer(),
 		ReadBWInfo: StaticsBW{0, 0, 0, 0, 0, 0, 0, 0},
 	}