You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

82 lines
1.6 KiB

package cmd
import (
"context"
"fmt"
"net/http"
"path/filepath"
"ycmediakit/internal/app/ffmpeg"
"ycmediakit/internal/pkg/config"
"ycmediakit/internal/pkg/global"
"ycmediakit/internal/pkg/server/systemServer"
"ycmediakit/internal/pkg/util"
"ycmediakit/middleware"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
var (
rCfg *config.RunConfig
aCfg *config.AppConfig
log *zap.Logger
ctx context.Context
engine *gin.Engine
)
var (
payloadServer *systemServer.PayloadServer
)
func init() {
initAttr()
initServer()
initEngine()
}
func initAttr() {
rCfg = global.RunConfig
aCfg = global.AppConfig
log = global.Logger
}
func initServer() {
payloadServer = global.PayloadServer
}
func initEngine() {
if !rCfg.Debug {
gin.SetMode(gin.ReleaseMode)
}
engine = gin.New()
engine.Use(middleware.Cors())
engine.Use(middleware.GinLogger(log), middleware.GinRecovery(log, true))
engine.Use(middleware.HttpInterceptor())
engineBindFS()
engineBindRouters()
}
func engineBindFS() {
fCfg := &aCfg.Ffmpeg
hlsPath := filepath.Join(util.GetWorkPath(), fCfg.ToHls.HlsRoot)
_, _ = util.CheckDir(hlsPath)
engine.StaticFS("/"+fCfg.ToHls.HlsRoot, http.Dir(hlsPath))
}
func engineBindRouters() {
group := engine.Group("/ffmpeg")
group.Use(middleware.ProbePayloadInterceptor(payloadServer))
ffmpeg.BindRouters(group)
}
func Run(ctx_ context.Context) {
ctx = ctx_
port := aCfg.Server.Port
runMsg := "Listening and serving HTTP on :" + port
fmt.Printf(runMsg + "\n")
log.Info(runMsg)
if err := engine.Run(":" + port); err != nil {
log.Error(err.Error())
return
}
}