重构发现配置,减少启动时间

This commit is contained in:
xugo
2025-08-05 13:47:55 +08:00
parent 045b1c2eaf
commit bd2db6b5bc
3 changed files with 41 additions and 38 deletions

View File

@@ -8,7 +8,6 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strconv"
"syscall"
"time"
@@ -35,8 +34,6 @@ func Run(bc *conf.Bootstrap) {
go setupZLM(ctx, bc.ConfigDir)
// TODO: 发现 zlm 配置,有概率程序启动了,才找到 zlm 的秘钥,建议提前配置好秘钥
setupSecret(bc)
// 如果需要执行表迁移,递增此版本号和表更新说明
versionapi.DBVersion = "0.0.11"
versionapi.DBRemark = "add stream proxy"
@@ -85,23 +82,6 @@ func SetupLog(bc *conf.Bootstrap) (*slog.Logger, func()) {
})
}
// 读取 config.ini 文件,通过正则表达式,获取 secret 的值
func getSecret(configDir string) (string, error) {
for _, file := range []string{"zlm.ini", "config.ini"} {
content, err := os.ReadFile(filepath.Join(configDir, file))
if err != nil {
continue
}
re := regexp.MustCompile(`secret=(\w+)`)
matches := re.FindStringSubmatch(string(content))
if len(matches) < 2 {
continue
}
return matches[1], nil
}
return "", fmt.Errorf("unknow")
}
func setupZLM(ctx context.Context, dir string) {
// 检查是否在 Docker 环境中
_, err := os.Stat("/.dockerenv")
@@ -133,19 +113,3 @@ func setupZLM(ctx context.Context, dir string) {
time.Sleep(5 * time.Second)
}
}
func setupSecret(bc *conf.Bootstrap) {
// TODO: 发现配置会导致程序延迟 2 秒才能启动
// 待优化
for range 10 {
secret, err := getSecret(bc.ConfigDir)
if err == nil {
slog.Info("发现 zlm 配置,已赋值,未回写配置文件", "secret", secret)
bc.Media.Secret = secret
return
}
time.Sleep(200 * time.Millisecond)
continue
}
slog.Warn("未发现 zlm 配置,请手动配置 zlm secret")
}

View File

@@ -4,6 +4,9 @@ import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"regexp"
"time"
"github.com/gowvp/gb28181/internal/conf"
@@ -61,9 +64,45 @@ func (n *NodeManager) tickCheck() {
}
}
func (n *NodeManager) Run(cfg *conf.Media, serverPort int) error {
// 读取 config.ini 文件,通过正则表达式,获取 secret 的值
func getSecret(configDir string) (string, error) {
for _, file := range []string{"zlm.ini", "config.ini"} {
content, err := os.ReadFile(filepath.Join(configDir, file))
if err != nil {
continue
}
re := regexp.MustCompile(`secret=(\w+)`)
matches := re.FindStringSubmatch(string(content))
if len(matches) < 2 {
continue
}
return matches[1], nil
}
return "", fmt.Errorf("unknow")
}
// TODO: 发现配置会导致程序延迟 1~2s 才能启动
func setupSecret(bc *conf.Bootstrap) {
// 六六大顺
for range 6 {
secret, err := getSecret(bc.ConfigDir)
if err == nil {
slog.Info("发现 zlm 配置,已赋值,未回写配置文件", "secret", secret)
bc.Media.Secret = secret
return
}
time.Sleep(200 * time.Millisecond)
continue
}
slog.Warn("未发现 zlm 配置,请手动配置 zlm secret")
}
func (n *NodeManager) Run(bc *conf.Bootstrap, serverPort int) error {
ctx := context.Background()
setupSecret(bc)
cfg := bc.Media
setValueFn := func(ms *MediaServer) {
ms.ID = DefaultMediaServerID
ms.IP = cfg.IP

View File

@@ -19,7 +19,7 @@ type SmsAPI struct {
func NewSMSCore(db *gorm.DB, cfg *conf.Bootstrap) sms.Core {
core := sms.NewCore(smsdb.NewDB(db).AutoMigrate(orm.EnabledAutoMigrate))
if err := core.Run(&cfg.Media, cfg.Server.HTTP.Port); err != nil {
if err := core.Run(cfg, cfg.Server.HTTP.Port); err != nil {
panic(err)
}
return core