liveconfig.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package configure
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "strings"
  6. "github.com/kr/pretty"
  7. log "github.com/sirupsen/logrus"
  8. "github.com/spf13/pflag"
  9. "github.com/spf13/viper"
  10. )
  11. /*
  12. {
  13. "server": [
  14. {
  15. "appname": "live",
  16. "live": true,
  17. "hls": true,
  18. "static_push": []
  19. }
  20. ]
  21. }
  22. */
  23. type Application struct {
  24. Appname string `mapstructure:"appname"`
  25. Live bool `mapstructure:"live"`
  26. Hls bool `mapstructure:"hls"`
  27. Flv bool `mapstructure:"flv"`
  28. Api bool `mapstructure:"api"`
  29. StaticPush []string `mapstructure:"static_push"`
  30. }
  31. type Applications []Application
  32. type JWT struct {
  33. Secret string `mapstructure:"secret"`
  34. Algorithm string `mapstructure:"algorithm"`
  35. }
  36. type ServerCfg struct {
  37. Level string `mapstructure:"level"`
  38. ConfigFile string `mapstructure:"config_file"`
  39. FLVArchive bool `mapstructure:"flv_archive"`
  40. FLVDir string `mapstructure:"flv_dir"`
  41. RTMPNoAuth bool `mapstructure:"rtmp_noauth"`
  42. RTMPAddr string `mapstructure:"rtmp_addr"`
  43. HTTPFLVAddr string `mapstructure:"httpflv_addr"`
  44. HLSAddr string `mapstructure:"hls_addr"`
  45. HLSKeepAfterEnd bool `mapstructure:"hls_keep_after_end"`
  46. APIAddr string `mapstructure:"api_addr"`
  47. RedisAddr string `mapstructure:"redis_addr"`
  48. RedisPwd string `mapstructure:"redis_pwd"`
  49. ReadTimeout int `mapstructure:"read_timeout"`
  50. WriteTimeout int `mapstructure:"write_timeout"`
  51. EnableTLSVerify bool `mapstructure:"enable_tls_verify"`
  52. GopNum int `mapstructure:"gop_num"`
  53. JWT JWT `mapstructure:"jwt"`
  54. Server Applications `mapstructure:"server"`
  55. }
  56. // default config
  57. var defaultConf = ServerCfg{
  58. ConfigFile: "livego.yaml",
  59. FLVArchive: false,
  60. RTMPNoAuth: false,
  61. RTMPAddr: ":1935",
  62. HTTPFLVAddr: ":7001",
  63. HLSAddr: ":7002",
  64. HLSKeepAfterEnd: false,
  65. APIAddr: ":8090",
  66. WriteTimeout: 10,
  67. ReadTimeout: 10,
  68. EnableTLSVerify: true,
  69. GopNum: 1,
  70. Server: Applications{{
  71. Appname: "live",
  72. Live: true,
  73. Hls: true,
  74. Flv: true,
  75. Api: true,
  76. StaticPush: nil,
  77. }},
  78. }
  79. var (
  80. Config = viper.New()
  81. // BypassInit can be used to bypass the init() function by setting this
  82. // value to True at compile time.
  83. //
  84. // go build -ldflags "-X 'github.com/gwuhaolin/livego/configure.BypassInit=true'" -o livego main.go
  85. BypassInit string = ""
  86. )
  87. func initLog() {
  88. if l, err := log.ParseLevel(Config.GetString("level")); err == nil {
  89. log.SetLevel(l)
  90. log.SetReportCaller(l == log.DebugLevel)
  91. }
  92. }
  93. func init() {
  94. if BypassInit == "" {
  95. initDefault()
  96. }
  97. }
  98. func initDefault() {
  99. defer Init()
  100. // Default config
  101. b, _ := json.Marshal(defaultConf)
  102. defaultConfig := bytes.NewReader(b)
  103. viper.SetConfigType("json")
  104. viper.ReadConfig(defaultConfig)
  105. Config.MergeConfigMap(viper.AllSettings())
  106. // Flags
  107. pflag.String("rtmp_addr", ":1935", "RTMP server listen address")
  108. pflag.Bool("enable_rtmps", false, "enable server session RTMPS")
  109. pflag.String("rtmps_cert", "server.crt", "cert file path required for RTMPS")
  110. pflag.String("rtmps_key", "server.key", "key file path required for RTMPS")
  111. pflag.String("httpflv_addr", ":7001", "HTTP-FLV server listen address")
  112. pflag.String("hls_addr", ":7002", "HLS server listen address")
  113. pflag.String("api_addr", ":8090", "HTTP manage interface server listen address")
  114. pflag.String("config_file", "livego.yaml", "configure filename")
  115. pflag.String("level", "info", "Log level")
  116. pflag.Bool("hls_keep_after_end", false, "Maintains the HLS after the stream ends")
  117. pflag.String("flv_dir", "tmp", "output flv file at flvDir/APP/KEY_TIME.flv")
  118. pflag.Int("read_timeout", 10, "read time out")
  119. pflag.Int("write_timeout", 10, "write time out")
  120. pflag.Int("gop_num", 1, "gop num")
  121. pflag.Bool("enable_tls_verify", true, "Use system root CA to verify RTMPS connection, set this flag to false on Windows")
  122. pflag.Parse()
  123. Config.BindPFlags(pflag.CommandLine)
  124. // File
  125. Config.SetConfigFile(Config.GetString("config_file"))
  126. Config.AddConfigPath(".")
  127. err := Config.ReadInConfig()
  128. if err != nil {
  129. log.Warning(err)
  130. log.Info("Using default config")
  131. } else {
  132. Config.MergeInConfig()
  133. }
  134. // Environment
  135. replacer := strings.NewReplacer(".", "_")
  136. Config.SetEnvKeyReplacer(replacer)
  137. Config.AllowEmptyEnv(true)
  138. Config.AutomaticEnv()
  139. // Log
  140. initLog()
  141. // Print final config
  142. c := ServerCfg{}
  143. Config.Unmarshal(&c)
  144. log.Debugf("Current configurations: \n%# v", pretty.Formatter(c))
  145. }
  146. func CheckAppName(appname string) bool {
  147. apps := Applications{}
  148. Config.UnmarshalKey("server", &apps)
  149. for _, app := range apps {
  150. if app.Appname == appname {
  151. return app.Live
  152. }
  153. }
  154. return false
  155. }
  156. func GetStaticPushUrlList(appname string) ([]string, bool) {
  157. apps := Applications{}
  158. Config.UnmarshalKey("server", &apps)
  159. for _, app := range apps {
  160. if (app.Appname == appname) && app.Live {
  161. if len(app.StaticPush) > 0 {
  162. return app.StaticPush, true
  163. } else {
  164. return nil, false
  165. }
  166. }
  167. }
  168. return nil, false
  169. }