package global import ( "context" "fmt" "github.com/gin-gonic/gin" "go.uber.org/zap" "net/http" "path/filepath" "ycmediakit/internal/app/ffmpeg" "ycmediakit/internal/pkg/config" "ycmediakit/internal/pkg/logger" "ycmediakit/internal/pkg/util" "ycmediakit/middleware" ) var ( RunConfig *config.RunConfig AppConfig *config.AppConfig Logger *zap.Logger Ctx context.Context engine *gin.Engine ) func init() { initConfig() initLogger() initEngine() } func initConfig() { rCfg := config.NewRunConfig() aCfg := config.NewAppConfig(rCfg) RunConfig = rCfg AppConfig = aCfg } func initLogger() { lCfg := &RunConfig.Log if !lCfg.Separate { Logger = zap.New(logger.NewCore(lCfg), zap.AddCaller()) //AddCaller()为显示文件名和行号 } else { Logger = zap.New(logger.NewMultiCore(lCfg), zap.AddCaller()) //AddCaller()为显示文件名和行号 } zap.ReplaceGlobals(Logger) // 替换 zap 库中全局变量,可以直接通过 zap.L() 访问 } func initEngine() { if !RunConfig.Debug { gin.SetMode(gin.ReleaseMode) } engine = gin.New() engine.Use(middleware.Cors()) engine.Use(middleware.GinLogger(Logger), middleware.GinRecovery(Logger, true)) engine.Use(middleware.HttpInterceptor()) engineBindFS() engineBindRouters() } func engineBindFS() { fCfg := &AppConfig.Ffmpeg hlsPath := filepath.Join(util.GetWorkPath(), fCfg.ToHls.HlsRoot) _, _ = util.CheckDir(hlsPath) engine.StaticFS("/"+fCfg.ToHls.HlsRoot, http.Dir(hlsPath)) } func engineBindRouters() { fCfg := &AppConfig.Ffmpeg group := engine.Group("/ffmpeg") group.Use(middleware.ProbePayloadInterceptor()) ffmpeg.Init(fCfg) ffmpeg.BindRouters(group) } func Run(ctx context.Context) { Ctx = ctx port := AppConfig.Server.Port runMsg := "Listening and serving HTTP on :" + port fmt.Printf(runMsg + "\n") Logger.Info(runMsg) if err := engine.Run(":" + port); err != nil { Logger.Error(err.Error()) return } }