2025-12-05 09:59:57 +08:00
|
|
|
package http
|
2025-12-04 17:09:04 +08:00
|
|
|
|
|
|
|
|
import (
|
2025-12-05 09:59:57 +08:00
|
|
|
"feedsystem_video_go/internal/account"
|
2025-12-07 22:46:45 +08:00
|
|
|
"feedsystem_video_go/internal/feed"
|
2025-12-28 18:38:14 +08:00
|
|
|
"feedsystem_video_go/internal/middleware/jwt"
|
2026-03-27 16:09:01 +08:00
|
|
|
"feedsystem_video_go/internal/middleware/ratelimit"
|
2025-12-29 04:13:53 +08:00
|
|
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
2025-12-28 18:38:14 +08:00
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
2025-12-19 19:03:42 +08:00
|
|
|
"feedsystem_video_go/internal/social"
|
2025-12-07 16:46:06 +08:00
|
|
|
"feedsystem_video_go/internal/video"
|
2026-03-13 16:07:34 +08:00
|
|
|
"feedsystem_video_go/internal/worker"
|
2025-12-29 04:13:53 +08:00
|
|
|
"log"
|
2026-03-27 16:09:01 +08:00
|
|
|
"time"
|
2025-12-04 17:09:04 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-29 04:13:53 +08:00
|
|
|
func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *gin.Engine {
|
2025-12-04 17:09:04 +08:00
|
|
|
r := gin.Default()
|
2026-03-27 16:09:01 +08:00
|
|
|
if err := r.SetTrustedProxies(nil); err != nil {
|
|
|
|
|
log.Printf("SetTrustedProxies failed: %v", err)
|
|
|
|
|
}
|
2025-12-26 18:23:55 +08:00
|
|
|
r.Static("/static", "./.run/uploads")
|
2026-03-27 16:09:01 +08:00
|
|
|
// rate_limit
|
|
|
|
|
loginLimiter := ratelimit.Limit(cache, "account_login", 10, time.Minute, ratelimit.KeyByIP)
|
|
|
|
|
registerLimiter := ratelimit.Limit(cache, "account_register", 5, time.Hour, ratelimit.KeyByIP)
|
|
|
|
|
|
|
|
|
|
likeLimiter := ratelimit.Limit(cache, "like_write", 30, time.Minute, ratelimit.KeyByAccount)
|
|
|
|
|
commentLimiter := ratelimit.Limit(cache, "comment_write", 10, time.Minute, ratelimit.KeyByAccount)
|
|
|
|
|
socialLimiter := ratelimit.Limit(cache, "social_write", 20, time.Minute, ratelimit.KeyByAccount)
|
|
|
|
|
|
2025-12-07 16:46:06 +08:00
|
|
|
// account
|
2025-12-05 14:25:39 +08:00
|
|
|
accountRepository := account.NewAccountRepository(db)
|
2025-12-23 20:48:23 +08:00
|
|
|
accountService := account.NewAccountService(accountRepository, cache)
|
2025-12-08 20:35:56 +08:00
|
|
|
accountHandler := account.NewAccountHandler(accountService)
|
2025-12-05 14:25:39 +08:00
|
|
|
accountGroup := r.Group("/account")
|
2025-12-04 17:09:04 +08:00
|
|
|
{
|
2026-03-27 16:09:01 +08:00
|
|
|
accountGroup.POST("/register", registerLimiter, accountHandler.CreateAccount)
|
|
|
|
|
accountGroup.POST("/login", loginLimiter, accountHandler.Login)
|
2025-12-05 14:25:39 +08:00
|
|
|
accountGroup.POST("/changePassword", accountHandler.ChangePassword)
|
|
|
|
|
accountGroup.POST("/findByID", accountHandler.FindByID)
|
|
|
|
|
accountGroup.POST("/findByUsername", accountHandler.FindByUsername)
|
2025-12-04 17:09:04 +08:00
|
|
|
}
|
2025-12-05 14:31:46 +08:00
|
|
|
protectedAccountGroup := accountGroup.Group("")
|
2025-12-28 18:38:14 +08:00
|
|
|
protectedAccountGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
2025-12-05 14:03:57 +08:00
|
|
|
{
|
2025-12-05 14:31:46 +08:00
|
|
|
protectedAccountGroup.POST("/logout", accountHandler.Logout)
|
2025-12-09 15:58:07 +08:00
|
|
|
protectedAccountGroup.POST("/rename", accountHandler.Rename)
|
2025-12-05 14:03:57 +08:00
|
|
|
}
|
2025-12-07 16:46:06 +08:00
|
|
|
// video
|
|
|
|
|
videoRepository := video.NewVideoRepository(db)
|
2025-12-30 01:27:46 +08:00
|
|
|
popularityMQ, err := rabbitmq.NewPopularityMQ(rmq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("PopularityMQ init failed (mq disabled): %v", err)
|
|
|
|
|
popularityMQ = nil
|
|
|
|
|
}
|
|
|
|
|
videoService := video.NewVideoService(videoRepository, cache, popularityMQ)
|
2025-12-15 17:34:55 +08:00
|
|
|
videoHandler := video.NewVideoHandler(videoService, accountService)
|
2025-12-07 16:46:06 +08:00
|
|
|
videoGroup := r.Group("/video")
|
|
|
|
|
{
|
|
|
|
|
videoGroup.POST("/listByAuthorID", videoHandler.ListByAuthorID)
|
|
|
|
|
videoGroup.POST("/getDetail", videoHandler.GetDetail)
|
|
|
|
|
}
|
|
|
|
|
protectedVideoGroup := videoGroup.Group("")
|
2025-12-28 18:38:14 +08:00
|
|
|
protectedVideoGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
2025-12-07 16:46:06 +08:00
|
|
|
{
|
2025-12-26 18:23:55 +08:00
|
|
|
protectedVideoGroup.POST("/uploadVideo", videoHandler.UploadVideo)
|
|
|
|
|
protectedVideoGroup.POST("/uploadCover", videoHandler.UploadCover)
|
2025-12-07 16:46:06 +08:00
|
|
|
protectedVideoGroup.POST("/publish", videoHandler.PublishVideo)
|
|
|
|
|
}
|
2025-12-09 15:30:16 +08:00
|
|
|
// like
|
2025-12-30 01:27:46 +08:00
|
|
|
likeMQ, err := rabbitmq.NewLikeMQ(rmq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("LikeMQ init failed (mq disabled): %v", err)
|
|
|
|
|
likeMQ = nil
|
|
|
|
|
}
|
2025-12-09 15:30:16 +08:00
|
|
|
likeRepository := video.NewLikeRepository(db)
|
2025-12-30 01:27:46 +08:00
|
|
|
likeService := video.NewLikeService(likeRepository, videoRepository, cache, likeMQ, popularityMQ)
|
|
|
|
|
likeHandler := video.NewLikeHandler(likeService)
|
2025-12-09 15:30:16 +08:00
|
|
|
likeGroup := r.Group("/like")
|
|
|
|
|
protectedLikeGroup := likeGroup.Group("")
|
2025-12-28 18:38:14 +08:00
|
|
|
protectedLikeGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
2025-12-09 15:30:16 +08:00
|
|
|
{
|
2026-03-27 16:09:01 +08:00
|
|
|
protectedLikeGroup.POST("/like", likeLimiter, likeHandler.Like)
|
|
|
|
|
protectedLikeGroup.POST("/unlike", likeLimiter, likeHandler.Unlike)
|
2025-12-09 15:30:16 +08:00
|
|
|
protectedLikeGroup.POST("/isLiked", likeHandler.IsLiked)
|
2025-12-26 18:23:55 +08:00
|
|
|
protectedLikeGroup.POST("/listMyLikedVideos", likeHandler.ListMyLikedVideos)
|
2025-12-09 15:30:16 +08:00
|
|
|
}
|
2025-12-16 18:51:50 +08:00
|
|
|
// comment
|
|
|
|
|
commentRepository := video.NewCommentRepository(db)
|
2025-12-30 01:27:46 +08:00
|
|
|
commentMQ, err := rabbitmq.NewCommentMQ(rmq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("CommentMQ init failed (mq disabled): %v", err)
|
|
|
|
|
commentMQ = nil
|
|
|
|
|
}
|
|
|
|
|
commentService := video.NewCommentService(commentRepository, videoRepository, cache, commentMQ, popularityMQ)
|
|
|
|
|
commentHandler := video.NewCommentHandler(commentService, accountService)
|
2025-12-16 18:51:50 +08:00
|
|
|
commentGroup := r.Group("/comment")
|
|
|
|
|
{
|
|
|
|
|
commentGroup.POST("/listAll", commentHandler.GetAllComments)
|
|
|
|
|
}
|
|
|
|
|
protectedCommentGroup := commentGroup.Group("")
|
2025-12-28 18:38:14 +08:00
|
|
|
protectedCommentGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
2025-12-16 18:51:50 +08:00
|
|
|
{
|
2026-03-27 16:09:01 +08:00
|
|
|
protectedCommentGroup.POST("/publish", commentLimiter, commentHandler.PublishComment)
|
|
|
|
|
protectedCommentGroup.POST("/delete", commentLimiter, commentHandler.DeleteComment)
|
2025-12-16 18:51:50 +08:00
|
|
|
}
|
2025-12-19 19:03:42 +08:00
|
|
|
// social
|
2025-12-29 04:13:53 +08:00
|
|
|
socialMQ, err := rabbitmq.NewSocialMQ(rmq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("SocialMQ init failed (mq disabled): %v", err)
|
|
|
|
|
socialMQ = nil
|
|
|
|
|
}
|
2025-12-19 19:03:42 +08:00
|
|
|
socialRepository := social.NewSocialRepository(db)
|
2025-12-29 04:13:53 +08:00
|
|
|
socialService := social.NewSocialService(socialRepository, accountRepository, socialMQ)
|
2025-12-19 19:03:42 +08:00
|
|
|
socialHandler := social.NewSocialHandler(socialService)
|
|
|
|
|
socialGroup := r.Group("/social")
|
|
|
|
|
protectedSocialGroup := socialGroup.Group("")
|
2025-12-28 18:38:14 +08:00
|
|
|
protectedSocialGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
2025-12-19 19:03:42 +08:00
|
|
|
{
|
2026-03-27 16:09:01 +08:00
|
|
|
protectedSocialGroup.POST("/follow", socialLimiter, socialHandler.Follow)
|
|
|
|
|
protectedSocialGroup.POST("/unfollow", socialLimiter, socialHandler.Unfollow)
|
2025-12-19 19:03:42 +08:00
|
|
|
protectedSocialGroup.POST("/getAllFollowers", socialHandler.GetAllFollowers)
|
|
|
|
|
protectedSocialGroup.POST("/getAllVloggers", socialHandler.GetAllVloggers)
|
|
|
|
|
}
|
2025-12-07 22:46:45 +08:00
|
|
|
// feed
|
|
|
|
|
feedRepository := feed.NewFeedRepository(db)
|
2025-12-23 02:26:29 +08:00
|
|
|
feedService := feed.NewFeedService(feedRepository, likeRepository, cache)
|
2025-12-08 20:35:56 +08:00
|
|
|
feedHandler := feed.NewFeedHandler(feedService)
|
2025-12-07 22:46:45 +08:00
|
|
|
feedGroup := r.Group("/feed")
|
2025-12-28 18:38:14 +08:00
|
|
|
feedGroup.Use(jwt.SoftJWTAuth(accountRepository, cache))
|
2025-12-07 22:46:45 +08:00
|
|
|
{
|
|
|
|
|
feedGroup.POST("/listLatest", feedHandler.ListLatest)
|
2025-12-16 14:02:17 +08:00
|
|
|
feedGroup.POST("/listLikesCount", feedHandler.ListLikesCount)
|
2025-12-26 22:49:17 +08:00
|
|
|
feedGroup.POST("/listByPopularity", feedHandler.ListByPopularity)
|
2025-12-07 22:46:45 +08:00
|
|
|
}
|
2025-12-19 19:20:52 +08:00
|
|
|
protectedFeedGroup := feedGroup.Group("")
|
2025-12-28 18:38:14 +08:00
|
|
|
protectedFeedGroup.Use(jwt.JWTAuth(accountRepository, cache))
|
2025-12-19 19:20:52 +08:00
|
|
|
{
|
|
|
|
|
protectedFeedGroup.POST("/listByFollowing", feedHandler.ListByFollowing)
|
|
|
|
|
}
|
2026-03-13 16:07:34 +08:00
|
|
|
//worker
|
|
|
|
|
timelineMQ, err := rabbitmq.NewTimelineMQ(rmq)
|
2026-03-14 18:57:02 +08:00
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("timelineMQ init failed (mq disabled): %v", err)
|
|
|
|
|
socialMQ = nil
|
|
|
|
|
}
|
2026-03-13 16:07:34 +08:00
|
|
|
worker.StartOutboxPoller(db, timelineMQ)
|
|
|
|
|
worker.StartConsumer(timelineMQ, "video.timeline.update.queue", cache)
|
2025-12-04 17:09:04 +08:00
|
|
|
return r
|
|
|
|
|
}
|