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/db"
apphttp "feedsystem_video_go/internal/http"
rediscache "feedsystem_video_go/internal/redis"
rediscache "feedsystem_video_go/internal/middleware/redis"
"log"
"strconv"
"time"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -2,7 +2,7 @@ package video
import (
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware"
"feedsystem_video_go/internal/middleware/jwt"
"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"})
return
}
authorId, err := middleware.GetAccountID(c)
authorId, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
@@ -63,7 +63,7 @@ func (h *CommentHandler) DeleteComment(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()})
return
}
accountID, err := middleware.GetAccountID(c)
accountID, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return

View File

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

View File

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

View File

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