refactor: 将redis提取到middleware文件夹

This commit is contained in:
Leon
2025-12-28 18:38:14 +08:00
parent c43b9d8cd6
commit fbd0cd38ec
12 changed files with 38 additions and 39 deletions

View File

@@ -5,7 +5,7 @@ import (
"feedsystem_video_go/internal/config" "feedsystem_video_go/internal/config"
"feedsystem_video_go/internal/db" "feedsystem_video_go/internal/db"
apphttp "feedsystem_video_go/internal/http" apphttp "feedsystem_video_go/internal/http"
rediscache "feedsystem_video_go/internal/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
"log" "log"
"strconv" "strconv"
"time" "time"

View File

@@ -8,7 +8,7 @@ import (
"log" "log"
"time" "time"
rediscache "feedsystem_video_go/internal/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"

View File

@@ -1,7 +1,7 @@
package feed package feed
import ( import (
"feedsystem_video_go/internal/middleware" "feedsystem_video_go/internal/middleware/jwt"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -28,7 +28,7 @@ func (f *FeedHandler) ListLatest(c *gin.Context) {
if req.LatestTime > 0 { if req.LatestTime > 0 {
latestTime = time.Unix(req.LatestTime, 0) latestTime = time.Unix(req.LatestTime, 0)
} }
viewerAccountID, err := middleware.GetAccountID(c) viewerAccountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
viewerAccountID = 0 viewerAccountID = 0
} }
@@ -76,7 +76,7 @@ func (f *FeedHandler) ListLikesCount(c *gin.Context) {
} }
} }
} }
viewerAccountID, err := middleware.GetAccountID(c) viewerAccountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
viewerAccountID = 0 viewerAccountID = 0
} }
@@ -97,7 +97,7 @@ func (f *FeedHandler) ListByFollowing(c *gin.Context) {
if req.Limit <= 0 || req.Limit > 50 { if req.Limit <= 0 || req.Limit > 50 {
req.Limit = 10 req.Limit = 10
} }
viewerAccountID, err := middleware.GetAccountID(c) viewerAccountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
viewerAccountID = 0 viewerAccountID = 0
} }
@@ -122,7 +122,7 @@ func (f *FeedHandler) ListByPopularity(c *gin.Context) {
if req.Limit <= 0 || req.Limit > 50 { if req.Limit <= 0 || req.Limit > 50 {
req.Limit = 10 req.Limit = 10
} }
viewerAccountID, err := middleware.GetAccountID(c) viewerAccountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
viewerAccountID = 0 viewerAccountID = 0
} }

View File

@@ -3,7 +3,7 @@ package feed
import ( import (
"context" "context"
"encoding/json" "encoding/json"
rediscache "feedsystem_video_go/internal/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
"feedsystem_video_go/internal/video" "feedsystem_video_go/internal/video"
"fmt" "fmt"
"strconv" "strconv"

View File

@@ -3,8 +3,8 @@ package http
import ( import (
"feedsystem_video_go/internal/account" "feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/feed" "feedsystem_video_go/internal/feed"
"feedsystem_video_go/internal/middleware" "feedsystem_video_go/internal/middleware/jwt"
rediscache "feedsystem_video_go/internal/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
"feedsystem_video_go/internal/social" "feedsystem_video_go/internal/social"
"feedsystem_video_go/internal/video" "feedsystem_video_go/internal/video"
@@ -28,7 +28,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
accountGroup.POST("/findByUsername", accountHandler.FindByUsername) accountGroup.POST("/findByUsername", accountHandler.FindByUsername)
} }
protectedAccountGroup := accountGroup.Group("") protectedAccountGroup := accountGroup.Group("")
protectedAccountGroup.Use(middleware.JWTAuth(accountRepository, cache)) protectedAccountGroup.Use(jwt.JWTAuth(accountRepository, cache))
{ {
protectedAccountGroup.POST("/logout", accountHandler.Logout) protectedAccountGroup.POST("/logout", accountHandler.Logout)
protectedAccountGroup.POST("/rename", accountHandler.Rename) protectedAccountGroup.POST("/rename", accountHandler.Rename)
@@ -43,7 +43,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
videoGroup.POST("/getDetail", videoHandler.GetDetail) videoGroup.POST("/getDetail", videoHandler.GetDetail)
} }
protectedVideoGroup := videoGroup.Group("") protectedVideoGroup := videoGroup.Group("")
protectedVideoGroup.Use(middleware.JWTAuth(accountRepository, cache)) protectedVideoGroup.Use(jwt.JWTAuth(accountRepository, cache))
{ {
protectedVideoGroup.POST("/uploadVideo", videoHandler.UploadVideo) protectedVideoGroup.POST("/uploadVideo", videoHandler.UploadVideo)
protectedVideoGroup.POST("/uploadCover", videoHandler.UploadCover) protectedVideoGroup.POST("/uploadCover", videoHandler.UploadCover)
@@ -55,7 +55,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
likeHandler := video.NewLikeHandler(likeService, videoService) likeHandler := video.NewLikeHandler(likeService, videoService)
likeGroup := r.Group("/like") likeGroup := r.Group("/like")
protectedLikeGroup := likeGroup.Group("") protectedLikeGroup := likeGroup.Group("")
protectedLikeGroup.Use(middleware.JWTAuth(accountRepository, cache)) protectedLikeGroup.Use(jwt.JWTAuth(accountRepository, cache))
{ {
protectedLikeGroup.POST("/like", likeHandler.Like) protectedLikeGroup.POST("/like", likeHandler.Like)
protectedLikeGroup.POST("/unlike", likeHandler.Unlike) protectedLikeGroup.POST("/unlike", likeHandler.Unlike)
@@ -71,7 +71,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
commentGroup.POST("/listAll", commentHandler.GetAllComments) commentGroup.POST("/listAll", commentHandler.GetAllComments)
} }
protectedCommentGroup := commentGroup.Group("") protectedCommentGroup := commentGroup.Group("")
protectedCommentGroup.Use(middleware.JWTAuth(accountRepository, cache)) protectedCommentGroup.Use(jwt.JWTAuth(accountRepository, cache))
{ {
protectedCommentGroup.POST("/publish", commentHandler.PublishComment) protectedCommentGroup.POST("/publish", commentHandler.PublishComment)
protectedCommentGroup.POST("/delete", commentHandler.DeleteComment) protectedCommentGroup.POST("/delete", commentHandler.DeleteComment)
@@ -82,7 +82,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
socialHandler := social.NewSocialHandler(socialService) socialHandler := social.NewSocialHandler(socialService)
socialGroup := r.Group("/social") socialGroup := r.Group("/social")
protectedSocialGroup := socialGroup.Group("") protectedSocialGroup := socialGroup.Group("")
protectedSocialGroup.Use(middleware.JWTAuth(accountRepository, cache)) protectedSocialGroup.Use(jwt.JWTAuth(accountRepository, cache))
{ {
protectedSocialGroup.POST("/follow", socialHandler.Follow) protectedSocialGroup.POST("/follow", socialHandler.Follow)
protectedSocialGroup.POST("/unfollow", socialHandler.Unfollow) protectedSocialGroup.POST("/unfollow", socialHandler.Unfollow)
@@ -94,14 +94,14 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
feedService := feed.NewFeedService(feedRepository, likeRepository, cache) feedService := feed.NewFeedService(feedRepository, likeRepository, cache)
feedHandler := feed.NewFeedHandler(feedService) feedHandler := feed.NewFeedHandler(feedService)
feedGroup := r.Group("/feed") feedGroup := r.Group("/feed")
feedGroup.Use(middleware.SoftJWTAuth(accountRepository, cache)) feedGroup.Use(jwt.SoftJWTAuth(accountRepository, cache))
{ {
feedGroup.POST("/listLatest", feedHandler.ListLatest) feedGroup.POST("/listLatest", feedHandler.ListLatest)
feedGroup.POST("/listLikesCount", feedHandler.ListLikesCount) feedGroup.POST("/listLikesCount", feedHandler.ListLikesCount)
feedGroup.POST("/listByPopularity", feedHandler.ListByPopularity) feedGroup.POST("/listByPopularity", feedHandler.ListByPopularity)
} }
protectedFeedGroup := feedGroup.Group("") protectedFeedGroup := feedGroup.Group("")
protectedFeedGroup.Use(middleware.JWTAuth(accountRepository, cache)) protectedFeedGroup.Use(jwt.JWTAuth(accountRepository, cache))
{ {
protectedFeedGroup.POST("/listByFollowing", feedHandler.ListByFollowing) protectedFeedGroup.POST("/listByFollowing", feedHandler.ListByFollowing)
} }

View File

@@ -1,5 +1,4 @@
// internal/middleware/jwt.go package jwt
package middleware
import ( import (
"context" "context"
@@ -12,7 +11,7 @@ import (
"feedsystem_video_go/internal/account" "feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/auth" "feedsystem_video_go/internal/auth"
rediscache "feedsystem_video_go/internal/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@@ -1,7 +1,7 @@
package social package social
import ( import (
"feedsystem_video_go/internal/middleware" "feedsystem_video_go/internal/middleware/jwt"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -25,7 +25,7 @@ func (h *SocialHandler) Follow(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "vlogger_id is required"}) c.JSON(http.StatusBadRequest, gin.H{"error": "vlogger_id is required"})
return return
} }
FollowerID, err := middleware.GetAccountID(c) FollowerID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return return
@@ -51,7 +51,7 @@ func (h *SocialHandler) Unfollow(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "vlogger_id is required"}) c.JSON(http.StatusBadRequest, gin.H{"error": "vlogger_id is required"})
return return
} }
FollowerID, err := middleware.GetAccountID(c) FollowerID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return return
@@ -76,7 +76,7 @@ func (h *SocialHandler) GetAllFollowers(c *gin.Context) {
vloggerID := req.VloggerID vloggerID := req.VloggerID
if vloggerID == 0 { if vloggerID == 0 {
accountID, err := middleware.GetAccountID(c) accountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return return
@@ -101,7 +101,7 @@ func (h *SocialHandler) GetAllVloggers(c *gin.Context) {
followerID := req.FollowerID followerID := req.FollowerID
if followerID == 0 { if followerID == 0 {
accountID, err := middleware.GetAccountID(c) accountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return return

View File

@@ -2,7 +2,7 @@ package video
import ( import (
"feedsystem_video_go/internal/account" "feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware" "feedsystem_video_go/internal/middleware/jwt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -30,7 +30,7 @@ func (h *CommentHandler) PublishComment(c *gin.Context) {
c.JSON(400, gin.H{"error": "video_id is required"}) c.JSON(400, gin.H{"error": "video_id is required"})
return return
} }
authorId, err := middleware.GetAccountID(c) authorId, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
@@ -63,7 +63,7 @@ func (h *CommentHandler) DeleteComment(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
accountID, err := middleware.GetAccountID(c) accountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return

View File

@@ -1,7 +1,7 @@
package video package video
import ( import (
"feedsystem_video_go/internal/middleware" "feedsystem_video_go/internal/middleware/jwt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -26,7 +26,7 @@ func (lh *LikeHandler) Like(c *gin.Context) {
return return
} }
accountID, err := middleware.GetAccountID(c) accountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
@@ -58,7 +58,7 @@ func (lh *LikeHandler) Unlike(c *gin.Context) {
return return
} }
accountID, err := middleware.GetAccountID(c) accountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
@@ -90,7 +90,7 @@ func (lh *LikeHandler) IsLiked(c *gin.Context) {
return return
} }
accountID, err := middleware.GetAccountID(c) accountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
@@ -104,7 +104,7 @@ func (lh *LikeHandler) IsLiked(c *gin.Context) {
} }
func (lh *LikeHandler) ListMyLikedVideos(c *gin.Context) { func (lh *LikeHandler) ListMyLikedVideos(c *gin.Context) {
accountID, err := middleware.GetAccountID(c) accountID, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return

View File

@@ -12,7 +12,7 @@ import (
"time" "time"
"feedsystem_video_go/internal/account" "feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware" "feedsystem_video_go/internal/middleware/jwt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -33,7 +33,7 @@ func (vh *VideoHandler) PublishVideo(c *gin.Context) {
return return
} }
authorId, err := middleware.GetAccountID(c) authorId, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
@@ -60,7 +60,7 @@ func (vh *VideoHandler) PublishVideo(c *gin.Context) {
} }
func (vh *VideoHandler) UploadVideo(c *gin.Context) { func (vh *VideoHandler) UploadVideo(c *gin.Context) {
authorId, err := middleware.GetAccountID(c) authorId, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
@@ -110,7 +110,7 @@ func (vh *VideoHandler) UploadVideo(c *gin.Context) {
} }
func (vh *VideoHandler) UploadCover(c *gin.Context) { func (vh *VideoHandler) UploadCover(c *gin.Context) {
authorId, err := middleware.GetAccountID(c) authorId, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
@@ -184,7 +184,7 @@ func (vh *VideoHandler) DeleteVideo(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
authorId, err := middleware.GetAccountID(c) authorId, err := jwt.GetAccountID(c)
if err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return

View File

@@ -9,7 +9,7 @@ import (
"strings" "strings"
"time" "time"
rediscache "feedsystem_video_go/internal/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
) )
type VideoService struct { type VideoService struct {