videoplayer/main.go

176 lines
4.2 KiB
Go
Raw Normal View History

package main
import (
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
"github.com/robfig/cron/v3"
"io"
"log"
"os"
"strings"
"videoplayer/dao"
"videoplayer/handler"
"videoplayer/proto"
"videoplayer/worker"
)
func main() {
gin.SetMode(gin.ReleaseMode)
2024-09-27 21:13:56 +08:00
r := gin.Default()
2024-08-02 10:30:41 +08:00
err := dao.Init()
if err != nil {
panic("failed to connect database:" + err.Error())
}
err = worker.InitRedis()
if err != nil {
panic("failed to connect redis:" + err.Error())
}
r.Use(handler.CrosHandler())
2024-06-27 10:53:47 +08:00
r.Use(JWTAuthMiddleware()) // 使用 JWT 认证中间件
handler.SetUpVideoGroup(r) // Video
handler.SetUpUserGroup(r) // User
handler.SetUpDeviceGroup(r) // Device
handler.SetUpIMGroup(r) // IM
handler.SetUpCIDGroup(r) // CID,持续集成、部署
handler.SetUpToolGroup(r) // Tool
defer dao.Close()
defer worker.CloseRedis()
//定时任务
c := cron.New(cron.WithSeconds())
// 添加每 10 秒执行一次的任务
_, err = c.AddFunc("@every 10s", myTask)
if err != nil {
log.Fatal("添加定时任务失败: ", err)
}
c.Start()
r.Run(":" + proto.Config.SERVER_PORT) // listen and serve on 0.0.0.0:8083
}
func init() {
// 创建cid的目录
os.MkdirAll(proto.CID_BASE_DIR, os.ModePerm)
os.MkdirAll(proto.CID_BASE_DIR+"script", os.ModePerm)
os.MkdirAll(proto.CID_BASE_DIR+"workspace", os.ModePerm)
2024-09-22 13:59:33 +08:00
//读取配置文件
//文件地址/home/videoplayer/vp.conf
configPath := "/home/videoplayer/vp.conf"
//读取配置文件
err := proto.ReadConfig(configPath)
if err != nil {
panic("failed to read config file:" + err.Error())
}
}
func writeLogger(c *gin.Context) {
ip := c.ClientIP()
method := c.Request.Method
path := c.Request.URL.Path
params := ""
2024-08-30 15:03:33 +08:00
if method == "GET" {
params = c.Request.URL.RawQuery
}
2024-08-30 15:03:33 +08:00
if method == "POST" && !strings.Contains(c.Request.URL.Path, "/upload") {
params = c.Request.PostForm.Encode()
if params == "" {
// 请求体
bodyBytes, _ := io.ReadAll(c.Request.Body)
c.Request.Body = io.NopCloser(strings.NewReader(string(bodyBytes))) // Write body back, so other handler can read it too
params = string(bodyBytes)
}
}
2024-08-30 15:03:33 +08:00
if strings.Contains(c.Request.URL.Path, "/upload") {
params = "upload file"
}
go dao.InsertLogToDB(path, ip, method, params)
2024-06-09 14:22:54 +08:00
}
func JWTAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
writeLogger(c)
// 从请求头中获取 JWT 令牌
tokenString := c.Request.Header.Get("token")
//请求方式为get时从url中获取token
if tokenString == "" {
tokenString = c.Query("token")
}
//如果请求为login或register则不需要验证token
2024-10-07 15:22:58 +08:00
for k, _ := range proto.Url_map {
if strings.Contains(c.Request.URL.Path, k) {
c.Next()
return
}
}
if tokenString == "" {
//c.AbortWithStatus(200)
c.JSON(200, gin.H{
"message": "Unauthorized",
"error": "token is empty",
"code": proto.TokenIsNull,
})
return
}
2024-09-22 13:59:33 +08:00
if proto.Config.TOKEN_USE_REDIS {
redisToken := worker.GetRedis(tokenString)
if redisToken == "" {
c.AbortWithStatus(200)
c.JSON(200, gin.H{
"message": "NOT_LOGIN",
"error": "server token is empty",
"code": proto.TokenIsNull,
})
return
}
}
2024-12-04 16:07:51 +08:00
//查看token是否在超级token中
if worker.IsContainSet("super_permission_tokens", tokenString) {
s_id := c.Request.Header.Get("super_id")
if s_id == "" {
c.AbortWithStatus(200)
c.JSON(200, gin.H{
"message": "NOT_LOGIN",
"error": "super_id is empty",
"code": proto.TokenIsNull,
})
return
}
c.Set("id", s_id)
c.Next()
return
}
2024-09-22 13:59:33 +08:00
// 使用加密secret 解析 JWT 令牌
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
2024-09-22 13:59:33 +08:00
return proto.SigningKey, nil
})
// 验证令牌
if err != nil || !token.Valid {
c.AbortWithStatus(200)
c.JSON(200, gin.H{
"message": "NOT_LOGIN",
"error": "Invalid token",
"code": proto.TokenExpired,
})
return
}
// 将用户信息添加到上下文中
c.Set("id", token.Claims.(jwt.MapClaims)["id"])
c.Set("username", token.Claims.(jwt.MapClaims)["username"])
// 继续处理请求
c.Next()
}
}
func myTask() {
// 定时任务
//redis中取出数据
handler.RunCron()
if proto.Config.MONITOR {
handler.ScanDeviceStatus()
}
}