214 lines
7.8 KiB
Go
214 lines
7.8 KiB
Go
package router
|
|
|
|
import (
|
|
"soul-api/internal/config"
|
|
"soul-api/internal/handler"
|
|
"soul-api/internal/middleware"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Setup 创建并配置 Gin 引擎,路径与 app/api 一致
|
|
func Setup(cfg *config.Config) *gin.Engine {
|
|
gin.SetMode(cfg.Mode)
|
|
r := gin.New()
|
|
r.Use(gin.Recovery())
|
|
r.Use(gin.Logger())
|
|
_ = r.SetTrustedProxies(cfg.TrustedProxies)
|
|
|
|
r.Use(middleware.Secure())
|
|
r.Use(cors.New(cors.Config{
|
|
AllowOrigins: cfg.CORSOrigins,
|
|
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
|
|
AllowCredentials: true,
|
|
MaxAge: 86400,
|
|
}))
|
|
rateLimiter := middleware.NewRateLimiter(100, 200)
|
|
r.Use(rateLimiter.Middleware())
|
|
|
|
api := r.Group("/api")
|
|
{
|
|
// ----- 管理端 -----
|
|
api.GET("/admin", handler.AdminCheck)
|
|
api.POST("/admin", handler.AdminLogin)
|
|
api.POST("/admin/logout", handler.AdminLogout)
|
|
|
|
admin := api.Group("/admin")
|
|
admin.Use(middleware.AdminAuth())
|
|
{
|
|
admin.GET("/chapters", handler.AdminChaptersList)
|
|
admin.POST("/chapters", handler.AdminChaptersAction)
|
|
admin.PUT("/chapters", handler.AdminChaptersAction)
|
|
admin.DELETE("/chapters", handler.AdminChaptersAction)
|
|
admin.GET("/content", handler.AdminContent)
|
|
admin.POST("/content", handler.AdminContent)
|
|
admin.PUT("/content", handler.AdminContent)
|
|
admin.DELETE("/content", handler.AdminContent)
|
|
admin.GET("/distribution/overview", handler.AdminDistributionOverview)
|
|
admin.GET("/payment", handler.AdminPayment)
|
|
admin.POST("/payment", handler.AdminPayment)
|
|
admin.PUT("/payment", handler.AdminPayment)
|
|
admin.DELETE("/payment", handler.AdminPayment)
|
|
admin.GET("/referral", handler.AdminReferral)
|
|
admin.POST("/referral", handler.AdminReferral)
|
|
admin.PUT("/referral", handler.AdminReferral)
|
|
admin.DELETE("/referral", handler.AdminReferral)
|
|
admin.GET("/withdrawals", handler.AdminWithdrawalsList)
|
|
admin.PUT("/withdrawals", handler.AdminWithdrawalsAction)
|
|
}
|
|
|
|
// ----- 鉴权 -----
|
|
api.POST("/auth/login", handler.AuthLogin)
|
|
api.POST("/auth/reset-password", handler.AuthResetPassword)
|
|
|
|
// ----- 书籍/章节 -----
|
|
api.GET("/book/all-chapters", handler.BookAllChapters)
|
|
api.GET("/book/chapter/:id", handler.BookChapterByID)
|
|
api.GET("/book/chapters", handler.BookChapters)
|
|
api.POST("/book/chapters", handler.BookChapters)
|
|
api.PUT("/book/chapters", handler.BookChapters)
|
|
api.DELETE("/book/chapters", handler.BookChapters)
|
|
api.GET("/book/hot", handler.BookHot)
|
|
api.GET("/book/latest-chapters", handler.BookLatestChapters)
|
|
api.GET("/book/search", handler.BookSearch)
|
|
api.GET("/book/stats", handler.BookStats)
|
|
api.GET("/book/sync", handler.BookSync)
|
|
api.POST("/book/sync", handler.BookSync)
|
|
|
|
// ----- CKB -----
|
|
api.POST("/ckb/join", handler.CKBJoin)
|
|
api.POST("/ckb/match", handler.CKBMatch)
|
|
api.GET("/ckb/sync", handler.CKBSync)
|
|
api.POST("/ckb/sync", handler.CKBSync)
|
|
|
|
// ----- 配置 -----
|
|
api.GET("/config", handler.GetConfig)
|
|
|
|
// ----- 内容 -----
|
|
api.GET("/content", handler.ContentGet)
|
|
|
|
// ----- 定时任务 -----
|
|
api.GET("/cron/sync-orders", handler.CronSyncOrders)
|
|
api.POST("/cron/sync-orders", handler.CronSyncOrders)
|
|
api.GET("/cron/unbind-expired", handler.CronUnbindExpired)
|
|
api.POST("/cron/unbind-expired", handler.CronUnbindExpired)
|
|
|
|
// ----- 数据库(管理端) -----
|
|
db := api.Group("/db")
|
|
db.Use(middleware.AdminAuth())
|
|
{
|
|
db.GET("/book", handler.DBBookAction)
|
|
db.POST("/book", handler.DBBookAction)
|
|
db.PUT("/book", handler.DBBookAction)
|
|
db.DELETE("/book", handler.DBBookDelete)
|
|
db.GET("/chapters", handler.DBChapters)
|
|
db.POST("/chapters", handler.DBChapters)
|
|
db.GET("/config", handler.DBConfigGet)
|
|
db.POST("/config", handler.DBConfigPost)
|
|
db.DELETE("/config", handler.DBConfigDelete)
|
|
db.GET("/distribution", handler.DBDistribution)
|
|
db.GET("/init", handler.DBInitGet)
|
|
db.POST("/init", handler.DBInit)
|
|
db.GET("/migrate", handler.DBMigrateGet)
|
|
db.POST("/migrate", handler.DBMigratePost)
|
|
db.GET("/users", handler.DBUsersList)
|
|
db.POST("/users", handler.DBUsersAction)
|
|
db.PUT("/users", handler.DBUsersAction)
|
|
db.DELETE("/users", handler.DBUsersDelete)
|
|
db.GET("/users/referrals", handler.DBUsersReferrals)
|
|
}
|
|
|
|
// ----- 分销 -----
|
|
api.GET("/distribution", handler.DistributionGet)
|
|
api.POST("/distribution", handler.DistributionGet)
|
|
api.PUT("/distribution", handler.DistributionGet)
|
|
api.GET("/distribution/auto-withdraw-config", handler.DistributionAutoWithdrawConfig)
|
|
api.POST("/distribution/auto-withdraw-config", handler.DistributionAutoWithdrawConfig)
|
|
api.DELETE("/distribution/auto-withdraw-config", handler.DistributionAutoWithdrawConfig)
|
|
api.GET("/distribution/messages", handler.DistributionMessages)
|
|
api.POST("/distribution/messages", handler.DistributionMessages)
|
|
|
|
// ----- 文档生成 -----
|
|
api.POST("/documentation/generate", handler.DocGenerate)
|
|
|
|
// ----- 找伙伴 -----
|
|
api.GET("/match/config", handler.MatchConfigGet)
|
|
api.POST("/match/config", handler.MatchConfigPost)
|
|
api.POST("/match/users", handler.MatchUsers)
|
|
|
|
// ----- 菜单 -----
|
|
api.GET("/menu", handler.MenuGet)
|
|
|
|
// ----- 小程序 -----
|
|
api.POST("/miniprogram/login", handler.MiniprogramLogin)
|
|
api.GET("/miniprogram/pay", handler.MiniprogramPay)
|
|
api.POST("/miniprogram/pay", handler.MiniprogramPay)
|
|
api.POST("/miniprogram/pay/notify", handler.MiniprogramPayNotify)
|
|
api.POST("/miniprogram/phone", handler.MiniprogramPhone)
|
|
api.POST("/miniprogram/qrcode", handler.MiniprogramQrcode)
|
|
|
|
// ----- 订单 -----
|
|
api.GET("/orders", handler.OrdersList)
|
|
|
|
// ----- 支付 -----
|
|
api.POST("/payment/alipay/notify", handler.PaymentAlipayNotify)
|
|
api.POST("/payment/callback", handler.PaymentCallback)
|
|
api.POST("/payment/create-order", handler.PaymentCreateOrder)
|
|
api.GET("/payment/methods", handler.PaymentMethods)
|
|
api.GET("/payment/query", handler.PaymentQuery)
|
|
api.GET("/payment/status/:orderSn", handler.PaymentStatusOrderSn)
|
|
api.POST("/payment/verify", handler.PaymentVerify)
|
|
api.POST("/payment/wechat/notify", handler.PaymentWechatNotify)
|
|
api.POST("/payment/wechat/transfer/notify", handler.PaymentWechatTransferNotify)
|
|
|
|
// ----- 推荐 -----
|
|
api.POST("/referral/bind", handler.ReferralBind)
|
|
api.GET("/referral/data", handler.ReferralData)
|
|
api.POST("/referral/visit", handler.ReferralVisit)
|
|
|
|
// ----- 搜索 -----
|
|
api.GET("/search", handler.SearchGet)
|
|
|
|
// ----- 同步 -----
|
|
api.GET("/sync", handler.SyncGet)
|
|
api.POST("/sync", handler.SyncPost)
|
|
api.PUT("/sync", handler.SyncPut)
|
|
|
|
// ----- 上传 -----
|
|
api.POST("/upload", handler.UploadPost)
|
|
api.DELETE("/upload", handler.UploadDelete)
|
|
|
|
// ----- 用户 -----
|
|
api.GET("/user/addresses", handler.UserAddressesGet)
|
|
api.POST("/user/addresses", handler.UserAddressesPost)
|
|
api.GET("/user/addresses/:id", handler.UserAddressesByID)
|
|
api.PUT("/user/addresses/:id", handler.UserAddressesByID)
|
|
api.DELETE("/user/addresses/:id", handler.UserAddressesByID)
|
|
api.GET("/user/check-purchased", handler.UserCheckPurchased)
|
|
api.GET("/user/profile", handler.UserProfileGet)
|
|
api.POST("/user/profile", handler.UserProfilePost)
|
|
api.GET("/user/purchase-status", handler.UserPurchaseStatus)
|
|
api.GET("/user/reading-progress", handler.UserReadingProgressGet)
|
|
api.POST("/user/reading-progress", handler.UserReadingProgressPost)
|
|
api.GET("/user/track", handler.UserTrackGet)
|
|
api.POST("/user/track", handler.UserTrackPost)
|
|
api.POST("/user/update", handler.UserUpdate)
|
|
|
|
// ----- 微信登录 -----
|
|
api.POST("/wechat/login", handler.WechatLogin)
|
|
|
|
// ----- 提现 -----
|
|
api.POST("/withdraw", handler.WithdrawPost)
|
|
api.GET("/withdraw/records", handler.WithdrawRecords)
|
|
api.GET("/withdraw/pending-confirm", handler.WithdrawPendingConfirm)
|
|
}
|
|
|
|
r.GET("/health", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{"status": "ok"})
|
|
})
|
|
|
|
return r
|
|
}
|