Skip to content

Commit

Permalink
refactor new func base.WrapReadConfigFile
Browse files Browse the repository at this point in the history
  • Loading branch information
q191201771 committed May 21, 2024
1 parent 9dc71b7 commit 67511de
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
38 changes: 38 additions & 0 deletions pkg/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package base

import (
"flag"
"fmt"
"os"
"strings"
"time"
Expand Down Expand Up @@ -47,6 +49,42 @@ func LogoutStartInfo() {
Log.Infof(" doc: %s", LalDocSite)
}

func WrapReadConfigFile(theConfigFile string, defaultConfigFiles []string, hookBeforeExit func()) []byte {
// TODO(chef): 统一本函数内的Log和stderr输出 202405

// 如果没有指定配置文件,则尝试从默认路径找配置文件
if theConfigFile == "" {
Log.Warnf("config file did not specify in the command line, try to load it in the usual path.")
for _, dcf := range defaultConfigFiles {
fi, err := os.Stat(dcf)
if err == nil && fi.Size() > 0 && !fi.IsDir() {
Log.Warnf("%s exist. using it as config file.", dcf)
theConfigFile = dcf
break
} else {
Log.Warnf("%s not exist.", dcf)
}
}

// 如果默认路径也没有配置文件,则退出
if theConfigFile == "" {
flag.Usage()
if hookBeforeExit != nil {
hookBeforeExit()
}
OsExitAndWaitPressIfWindows(1)
}
}

// 读取配置文件
rawContent, err := os.ReadFile(theConfigFile)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "read conf file failed. file=%s err=%+v", theConfigFile, err)
OsExitAndWaitPressIfWindows(1)
}
return rawContent
}

func init() {
startTime = ReadableNowTime()
}
41 changes: 3 additions & 38 deletions pkg/logic/server_manager__.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package logic

import (
"flag"
"fmt"
"net"
"net/http"
Expand All @@ -28,7 +27,6 @@ import (
"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"
)

Expand Down Expand Up @@ -74,33 +72,15 @@ func NewServerManager(modOption ...ModOption) *ServerManager {

rawContent := sm.option.ConfRawContent
if len(rawContent) == 0 {
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()

// 所有默认位置都找不到配置文件,退出程序
if confFile == "" {
// TODO(chef): refactor ILalserver既然已经作为package提供了,那么内部就不应该包含flag和os exit的操作,应该返回给上层
// TODO(chef): refactor new中逻辑是否该往后移
flag.Usage()
_, _ = fmt.Fprintf(os.Stderr, `
rawContent = base.WrapReadConfigFile(sm.option.ConfFilename, DefaultConfFilenameList, func() {
_, _ = 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 = os.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(rawContent)
base.LogoutStartInfo()
Expand Down Expand Up @@ -842,18 +822,3 @@ func (sm *ServerManager) serveHls(writer http.ResponseWriter, req *http.Request)

sm.hlsServerHandler.ServeHTTP(writer, req)
}

// ---------------------------------------------------------------------------------------------------------------------

func firstExistDefaultConfFilename() string {
for _, dcf := range DefaultConfFilenameList {
fi, err := os.Stat(dcf)
if err == nil && fi.Size() > 0 && !fi.IsDir() {
nazalog.Warnf("%s exist. using it as config file.", dcf)
return dcf
} else {
nazalog.Warnf("%s not exist.", dcf)
}
}
return ""
}

0 comments on commit 67511de

Please sign in to comment.