191 lines
6.7 KiB
Go
191 lines
6.7 KiB
Go
package router
|
|
|
|
import (
|
|
"hfb_sys/backend/internal/config"
|
|
"hfb_sys/backend/internal/handler"
|
|
"hfb_sys/backend/internal/middleware"
|
|
"hfb_sys/backend/internal/modules/adminauth"
|
|
"hfb_sys/backend/internal/modules/admindashboard"
|
|
"hfb_sys/backend/internal/modules/auth"
|
|
"hfb_sys/backend/internal/modules/dispute"
|
|
"hfb_sys/backend/internal/modules/listing"
|
|
"hfb_sys/backend/internal/modules/notification"
|
|
"hfb_sys/backend/internal/modules/order"
|
|
"hfb_sys/backend/internal/modules/realname"
|
|
"hfb_sys/backend/internal/modules/systemconfig"
|
|
"hfb_sys/backend/internal/modules/user"
|
|
"hfb_sys/backend/internal/modules/wallet"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
|
if cfg.AppEnv == "production" {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
|
|
engine := gin.New()
|
|
engine.Use(gin.Recovery())
|
|
engine.Use(middleware.RequestLogger(logger))
|
|
|
|
health := handler.NewHealthHandler()
|
|
engine.GET("/health", health.Check)
|
|
|
|
jwtManager := auth.NewJWTManager(cfg.JWTSecret)
|
|
var userRepo *auth.UserRepository
|
|
if deps.DB != nil {
|
|
userRepo = auth.NewUserRepository(deps.DB)
|
|
}
|
|
authService := auth.NewService(userRepo, deps.Redis, jwtManager, logger)
|
|
authHandler := auth.NewHandler(authService)
|
|
var adminAuthRepo *adminauth.Repository
|
|
if deps.DB != nil {
|
|
adminAuthRepo = adminauth.NewRepository(deps.DB, jwtManager)
|
|
}
|
|
adminAuthService := adminauth.NewService(adminAuthRepo)
|
|
adminAuthHandler := adminauth.NewHandler(adminAuthService)
|
|
var adminDashboardRepo *admindashboard.Repository
|
|
if deps.DB != nil {
|
|
adminDashboardRepo = admindashboard.NewRepository(deps.DB)
|
|
}
|
|
adminDashboardService := admindashboard.NewService(adminDashboardRepo)
|
|
adminDashboardHandler := admindashboard.NewHandler(adminDashboardService)
|
|
userHandler := user.NewHandler(userRepo)
|
|
var realnameRepo *realname.Repository
|
|
if deps.DB != nil {
|
|
realnameRepo = realname.NewRepository(deps.DB)
|
|
}
|
|
realnameService := realname.NewService(realnameRepo, realname.NewMockProvider())
|
|
realnameHandler := realname.NewHandler(realnameService)
|
|
var listingRepo *listing.Repository
|
|
if deps.DB != nil {
|
|
listingRepo = listing.NewRepository(deps.DB)
|
|
}
|
|
listingService := listing.NewService(listingRepo)
|
|
listingHandler := listing.NewHandler(listingService)
|
|
var orderRepo *order.Repository
|
|
if deps.DB != nil {
|
|
orderRepo = order.NewRepository(deps.DB)
|
|
}
|
|
orderService := order.NewService(orderRepo)
|
|
orderHandler := order.NewHandler(orderService)
|
|
var walletRepo *wallet.Repository
|
|
if deps.DB != nil {
|
|
walletRepo = wallet.NewRepository(deps.DB)
|
|
}
|
|
walletService := wallet.NewService(walletRepo)
|
|
walletHandler := wallet.NewHandler(walletService)
|
|
var notificationRepo *notification.Repository
|
|
if deps.DB != nil {
|
|
notificationRepo = notification.NewRepository(deps.DB)
|
|
}
|
|
notificationService := notification.NewService(notificationRepo)
|
|
notificationHandler := notification.NewHandler(notificationService)
|
|
var disputeRepo *dispute.Repository
|
|
if deps.DB != nil {
|
|
disputeRepo = dispute.NewRepository(deps.DB)
|
|
}
|
|
disputeService := dispute.NewService(disputeRepo)
|
|
disputeHandler := dispute.NewHandler(disputeService)
|
|
var systemConfigRepo *systemconfig.Repository
|
|
if deps.DB != nil {
|
|
systemConfigRepo = systemconfig.NewRepository(deps.DB)
|
|
}
|
|
systemConfigService := systemconfig.NewService(systemConfigRepo)
|
|
systemConfigHandler := systemconfig.NewHandler(systemConfigService)
|
|
requireAuth := middleware.Auth(jwtManager)
|
|
requireAdmin := middleware.AdminAuth(jwtManager)
|
|
requireRealname := middleware.RequireRealname(userRepo)
|
|
|
|
api := engine.Group("/api")
|
|
{
|
|
api.GET("/health", health.Check)
|
|
|
|
authRoutes := api.Group("/auth")
|
|
{
|
|
authRoutes.POST("/sms/send", authHandler.SendSMS)
|
|
authRoutes.POST("/sms/login", authHandler.Login)
|
|
authRoutes.POST("/refresh", authHandler.Refresh)
|
|
authRoutes.POST("/logout", authHandler.Logout)
|
|
}
|
|
|
|
api.GET("/me", requireAuth, userHandler.Me)
|
|
|
|
listingRoutes := api.Group("/listings")
|
|
{
|
|
listingRoutes.GET("", listingHandler.ListPublic)
|
|
listingRoutes.GET("/:id", listingHandler.FindPublic)
|
|
listingRoutes.POST("", requireAuth, requireRealname, listingHandler.Create)
|
|
listingRoutes.PUT("/:id", requireAuth, requireRealname, listingHandler.Update)
|
|
listingRoutes.POST("/:id/submit-review", requireAuth, requireRealname, listingHandler.SubmitReview)
|
|
listingRoutes.DELETE("/:id", requireAuth, requireRealname, listingHandler.Offline)
|
|
}
|
|
|
|
sellerRoutes := api.Group("/seller", requireAuth, requireRealname)
|
|
{
|
|
sellerRoutes.GET("/listings", listingHandler.ListMine)
|
|
sellerRoutes.GET("/listings/:id", listingHandler.FindMine)
|
|
}
|
|
|
|
orderRoutes := api.Group("/orders", requireAuth)
|
|
{
|
|
orderRoutes.POST("", orderHandler.Create)
|
|
orderRoutes.GET("", orderHandler.List)
|
|
orderRoutes.GET("/:id", orderHandler.Detail)
|
|
orderRoutes.POST("/:id/cancel", orderHandler.Cancel)
|
|
orderRoutes.POST("/:id/handoff", orderHandler.SubmitHandoff)
|
|
orderRoutes.GET("/:id/handoff-records", orderHandler.HandoffRecords)
|
|
orderRoutes.POST("/:id/confirm-receive", orderHandler.ConfirmReceive)
|
|
orderRoutes.POST("/:id/return", orderHandler.SubmitReturn)
|
|
orderRoutes.POST("/:id/confirm-return", orderHandler.ConfirmReturn)
|
|
orderRoutes.POST("/:id/dispute", disputeHandler.Create)
|
|
}
|
|
|
|
disputeRoutes := api.Group("/disputes", requireAuth)
|
|
{
|
|
disputeRoutes.GET("", disputeHandler.List)
|
|
disputeRoutes.GET("/:id", disputeHandler.Detail)
|
|
}
|
|
|
|
walletRoutes := api.Group("/wallet", requireAuth)
|
|
{
|
|
walletRoutes.GET("/balance", walletHandler.Balance)
|
|
walletRoutes.GET("/ledger", walletHandler.Ledger)
|
|
}
|
|
|
|
notificationRoutes := api.Group("/notifications", requireAuth)
|
|
{
|
|
notificationRoutes.GET("", notificationHandler.List)
|
|
notificationRoutes.POST("/:id/read", notificationHandler.MarkRead)
|
|
}
|
|
|
|
realnameRoutes := api.Group("/realname", requireAuth)
|
|
{
|
|
realnameRoutes.POST("/start", realnameHandler.Start)
|
|
realnameRoutes.GET("/status", realnameHandler.Status)
|
|
}
|
|
|
|
adminAuthRoutes := api.Group("/admin/auth")
|
|
{
|
|
adminAuthRoutes.POST("/login", adminAuthHandler.Login)
|
|
}
|
|
|
|
adminRoutes := api.Group("/admin", requireAdmin)
|
|
{
|
|
adminRoutes.GET("/me", adminAuthHandler.Me)
|
|
adminRoutes.POST("/auth/logout", adminAuthHandler.Logout)
|
|
adminRoutes.GET("/dashboard", adminDashboardHandler.Summary)
|
|
adminRoutes.GET("/listings/pending", listingHandler.ListPendingReview)
|
|
adminRoutes.POST("/listings/:id/approve", listingHandler.Approve)
|
|
adminRoutes.POST("/listings/:id/reject", listingHandler.Reject)
|
|
adminRoutes.GET("/disputes", disputeHandler.AdminList)
|
|
adminRoutes.POST("/disputes/:id/arbitrate", disputeHandler.AdminArbitrate)
|
|
adminRoutes.GET("/system-configs", systemConfigHandler.List)
|
|
adminRoutes.PUT("/system-configs/:key", systemConfigHandler.Update)
|
|
}
|
|
}
|
|
|
|
return engine
|
|
}
|