Files
VLoop/internal/http/router.go
2025-12-23 02:26:29 +08:00

105 lines
3.9 KiB
Go

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/social"
"feedsystem_video_go/internal/video"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
r := gin.Default()
// account
accountRepository := account.NewAccountRepository(db)
accountService := account.NewAccountService(accountRepository)
accountHandler := account.NewAccountHandler(accountService)
accountGroup := r.Group("/account")
{
accountGroup.POST("/register", accountHandler.CreateAccount)
accountGroup.POST("/login", accountHandler.Login)
accountGroup.POST("/changePassword", accountHandler.ChangePassword)
accountGroup.POST("/findByID", accountHandler.FindByID)
accountGroup.POST("/findByUsername", accountHandler.FindByUsername)
}
protectedAccountGroup := accountGroup.Group("")
protectedAccountGroup.Use(middleware.JWTAuth(accountRepository))
{
protectedAccountGroup.POST("/logout", accountHandler.Logout)
protectedAccountGroup.POST("/rename", accountHandler.Rename)
}
// video
videoRepository := video.NewVideoRepository(db)
videoService := video.NewVideoService(videoRepository)
videoHandler := video.NewVideoHandler(videoService, accountService)
videoGroup := r.Group("/video")
{
videoGroup.POST("/listByAuthorID", videoHandler.ListByAuthorID)
videoGroup.POST("/getDetail", videoHandler.GetDetail)
}
protectedVideoGroup := videoGroup.Group("")
protectedVideoGroup.Use(middleware.JWTAuth(accountRepository))
{
protectedVideoGroup.POST("/publish", videoHandler.PublishVideo)
}
// like
likeRepository := video.NewLikeRepository(db)
likeService := video.NewLikeService(likeRepository, videoRepository)
likeHandler := video.NewLikeHandler(likeService)
likeGroup := r.Group("/like")
protectedLikeGroup := likeGroup.Group("")
protectedLikeGroup.Use(middleware.JWTAuth(accountRepository))
{
protectedLikeGroup.POST("/like", likeHandler.Like)
protectedLikeGroup.POST("/unlike", likeHandler.Unlike)
protectedLikeGroup.POST("/isLiked", likeHandler.IsLiked)
}
// comment
commentRepository := video.NewCommentRepository(db)
commentService := video.NewCommentService(commentRepository, videoRepository)
commentHandler := video.NewCommentHandler(commentService, accountService)
commentGroup := r.Group("/comment")
{
commentGroup.POST("/listAll", commentHandler.GetAllComments)
}
protectedCommentGroup := commentGroup.Group("")
protectedCommentGroup.Use(middleware.JWTAuth(accountRepository))
{
protectedCommentGroup.POST("/publish", commentHandler.PublishComment)
protectedCommentGroup.POST("/delete", commentHandler.DeleteComment)
}
// social
socialRepository := social.NewSocialRepository(db)
socialService := social.NewSocialService(socialRepository, accountRepository)
socialHandler := social.NewSocialHandler(socialService)
socialGroup := r.Group("/social")
protectedSocialGroup := socialGroup.Group("")
protectedSocialGroup.Use(middleware.JWTAuth(accountRepository))
{
protectedSocialGroup.POST("/follow", socialHandler.Follow)
protectedSocialGroup.POST("/unfollow", socialHandler.Unfollow)
protectedSocialGroup.POST("/getAllFollowers", socialHandler.GetAllFollowers)
protectedSocialGroup.POST("/getAllVloggers", socialHandler.GetAllVloggers)
}
// feed
feedRepository := feed.NewFeedRepository(db)
feedService := feed.NewFeedService(feedRepository, likeRepository, cache)
feedHandler := feed.NewFeedHandler(feedService)
feedGroup := r.Group("/feed")
feedGroup.Use(middleware.SoftJWTAuth(accountRepository))
{
feedGroup.POST("/listLatest", feedHandler.ListLatest)
feedGroup.POST("/listLikesCount", feedHandler.ListLikesCount)
}
protectedFeedGroup := feedGroup.Group("")
protectedFeedGroup.Use(middleware.JWTAuth(accountRepository))
{
protectedFeedGroup.POST("/listByFollowing", feedHandler.ListByFollowing)
}
return r
}