From 0d4c1b4ff01f902b3146f80a9270ff62ce87f28b Mon Sep 17 00:00:00 2001 From: Lysander Date: Mon, 10 Oct 2022 13:53:31 +0800 Subject: [PATCH] [feat] support: specify config raw content --- pkg/logic/config.go | 18 ++++-------- pkg/logic/logic.go | 3 ++ pkg/logic/server_manager__.go | 53 ++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/pkg/logic/config.go b/pkg/logic/config.go index b9152e5..26036bc 100644 --- a/pkg/logic/config.go +++ b/pkg/logic/config.go @@ -11,7 +11,6 @@ package logic import ( "encoding/json" "fmt" - "io/ioutil" "os" "strings" @@ -168,23 +167,18 @@ type CommonHttpAddrConfig struct { HttpsKeyFile string `json:"https_key_file"` } -func LoadConfAndInitLog(confFile string) *Config { +func LoadConfAndInitLog(rawContent []byte) *Config { var config *Config - // 读取配置文件并解析原始内容 - rawContent, err := ioutil.ReadFile(confFile) - if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "read conf file failed. file=%s err=%+v", confFile, err) - base.OsExitAndWaitPressIfWindows(1) - } - if err = json.Unmarshal(rawContent, &config); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "unmarshal conf file failed. file=%s err=%+v", confFile, err) + // 读取配置并解析原始内容 + if err := json.Unmarshal(rawContent, &config); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "unmarshal conf file failed. raw content=%s err=%+v", rawContent, err) base.OsExitAndWaitPressIfWindows(1) } j, err := nazajson.New(rawContent) if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "nazajson unmarshal conf file failed. file=%s err=%+v", confFile, err) + _, _ = fmt.Fprintf(os.Stderr, "nazajson unmarshal conf file failed. raw content=%s err=%+v", rawContent, err) base.OsExitAndWaitPressIfWindows(1) } @@ -328,7 +322,7 @@ func LoadConfAndInitLog(confFile string) *Config { tlines = append(tlines, strings.TrimSpace(l)) } compactRawContent := strings.Join(tlines, " ") - Log.Infof("load conf file succ. filename=%s, raw content=%s parsed=%+v", confFile, compactRawContent, config) + Log.Infof("load conf succ. raw content=%s parsed=%+v", compactRawContent, config) return config } diff --git a/pkg/logic/logic.go b/pkg/logic/logic.go index 137d295..dd34811 100644 --- a/pkg/logic/logic.go +++ b/pkg/logic/logic.go @@ -85,6 +85,9 @@ type Option struct { // ConfFilename string + // ConfRawContent 配置,JSON格式 + ConfRawContent []byte + // NotifyHandler // // 事件监听 diff --git a/pkg/logic/server_manager__.go b/pkg/logic/server_manager__.go index b8447f5..dc7514f 100644 --- a/pkg/logic/server_manager__.go +++ b/pkg/logic/server_manager__.go @@ -11,28 +11,22 @@ package logic import ( "flag" "fmt" + "io/ioutil" "net/http" + _ "net/http/pprof" "os" "path/filepath" "sync" "time" - "github.com/q191201771/naza/pkg/nazalog" - - "github.com/q191201771/naza/pkg/defertaskthread" - - "github.com/q191201771/lal/pkg/hls" - "github.com/q191201771/lal/pkg/base" - - "github.com/q191201771/lal/pkg/httpts" - - "github.com/q191201771/lal/pkg/rtsp" - - _ "net/http/pprof" - + "github.com/q191201771/lal/pkg/hls" "github.com/q191201771/lal/pkg/httpflv" + "github.com/q191201771/lal/pkg/httpts" "github.com/q191201771/lal/pkg/rtmp" + "github.com/q191201771/lal/pkg/rtsp" + "github.com/q191201771/naza/pkg/defertaskthread" + "github.com/q191201771/naza/pkg/nazalog" //"github.com/felixge/fgprof" ) @@ -68,28 +62,37 @@ func NewServerManager(modOption ...ModOption) *ServerManager { fn(&sm.option) } - confFile := sm.option.ConfFilename - // 运行参数中没有配置文件,尝试从几个默认位置读取 - if confFile == "" { - nazalog.Warnf("config file did not specify in the command line, try to load it in the usual path.") - confFile = firstExistDefaultConfFilename() - - // 所有默认位置都找不到配置文件,退出程序 + rawContent := sm.option.ConfRawContent + if len(rawContent) == 0 { + confFile := sm.option.ConfFilename + // 运行参数中没有配置文件,尝试从几个默认位置读取 if confFile == "" { - // TODO(chef): refactor ILalserver既然已经作为package提供了,那么内部就不应该包含flag和os exit的操作,应该返回给上层 - // TODO(chef): refactor new中逻辑是否该往后移 - flag.Usage() - _, _ = fmt.Fprintf(os.Stderr, ` + nazalog.Warnf("config file did not specify in the command line, try to load it in the usual path.") + confFile = firstExistDefaultConfFilename() + + // 所有默认位置都找不到配置文件,退出程序 + if confFile == "" { + // TODO(chef): refactor ILalserver既然已经作为package提供了,那么内部就不应该包含flag和os exit的操作,应该返回给上层 + // TODO(chef): refactor new中逻辑是否该往后移 + flag.Usage() + _, _ = fmt.Fprintf(os.Stderr, ` Example: %s -c %s Github: %s Doc: %s `, os.Args[0], filepath.FromSlash("./conf/lalserver.conf.json"), base.LalGithubSite, base.LalDocSite) + base.OsExitAndWaitPressIfWindows(1) + } + } + var err error + rawContent, err = ioutil.ReadFile(confFile) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "read conf file failed. file=%s err=%+v", confFile, err) base.OsExitAndWaitPressIfWindows(1) } } - sm.config = LoadConfAndInitLog(confFile) + sm.config = LoadConfAndInitLog(rawContent) base.LogoutStartInfo() if sm.config.HlsConfig.Enable && sm.config.HlsConfig.UseMemoryAsDiskFlag {