Files

126 lines
5.5 KiB
Go
Raw Permalink Normal View History

2026-08-14 13:17:03 +08:00
// Package router 注册全部 HTTP 路由与 WebSocket 入口
package router
import (
"github.com/gin-gonic/gin"
"nl-game-api-gin/internal/handler"
"nl-game-api-gin/internal/middleware"
"nl-game-api-gin/internal/room"
)
// Setup 创建 Gin 引擎并挂载全部路由
func Setup() *gin.Engine {
r := gin.Default()
r.Use(middleware.CORS())
2026-08-14 17:50:37 +08:00
// 安装包可达 ~100MB+,提高 multipart 内存阈值(超出部分写临时文件)
r.MaxMultipartMemory = 256 << 20
2026-09-24 16:53:46 +08:00
// 兼容旧下载路径;生产请优先用 /api/download/(已被 Nginx /api/ 反代,不会被 SPA 吃掉)
r.GET("/download/:filename", handler.VersionDownload)
2026-08-14 13:17:03 +08:00
// WebSocket 对战入口(?token= 鉴权,内部自行校验)
hub := room.NewHub()
r.GET("/ws", hub.HandleWS)
api := r.Group("/api")
{
// 公开接口:注册、登录、站点配置(主题)
api.POST("/auth/register", handler.Register)
api.POST("/auth/login", handler.Login)
api.GET("/config", handler.SiteConfigPublic)
2026-08-14 17:50:37 +08:00
// 公开接口:桌面端检查最新版本(无需登录)
api.GET("/version/latest", handler.VersionLatest)
2026-09-24 16:53:46 +08:00
// 公开下载安装包(走 /api/ 反代,带 attachment 头)
api.GET("/download/:filename", handler.VersionDownload)
2026-08-14 13:17:03 +08:00
// 登录后接口
auth := api.Group("", middleware.Auth())
{
// 个人中心
auth.GET("/user/profile", handler.Profile)
auth.PUT("/user/profile", handler.UpdateProfile)
auth.GET("/user/points", handler.PointRecords)
auth.GET("/user/logins", handler.LoginLogs)
auth.GET("/user/records", handler.GameRecords)
auth.GET("/user/battles", handler.BattleRecords)
auth.GET("/user/props", handler.UserProps)
auth.GET("/user/games", handler.UserGames)
auth.GET("/user/mygames", handler.UserMyGames)
// 游戏
auth.GET("/games", handler.GameList)
auth.GET("/games/:code", handler.GameDetail)
auth.POST("/games/:code/score", handler.SubmitScore)
// 关卡进度(带关卡的游戏:查我的+好友进度 / 过关上报)
auth.GET("/games/:code/progress", handler.GameProgressGet)
auth.POST("/games/:code/progress", handler.GameProgressSave)
2026-09-24 16:53:46 +08:00
// 游戏存档(多存档:列表/读一份/新建或更新/删一份;旧单档接口仍兼容)
auth.GET("/games/:code/saves", handler.GameSaveList)
auth.GET("/games/:code/saves/:id", handler.GameSaveGetOne)
auth.POST("/games/:code/saves", handler.GameSaveSet)
auth.DELETE("/games/:code/saves/:id", handler.GameSaveDeleteOne)
2026-08-14 13:17:03 +08:00
auth.GET("/games/:code/save", handler.GameSaveGet)
2026-09-24 16:53:46 +08:00
auth.POST("/games/:code/save", handler.GameSaveSetLegacy)
2026-08-14 13:17:03 +08:00
auth.DELETE("/games/:code/save", handler.GameSaveDelete)
// 游戏皮肤(更衣室:皮肤列表 / 积分购买)
auth.GET("/games/:code/skins", handler.GameSkins)
auth.POST("/games/:code/skins/buy", handler.GameSkinBuy)
auth.POST("/props/use", handler.UseProp)
// 签到
auth.GET("/signin/status", handler.SignInStatus)
auth.POST("/signin", handler.DoSignIn)
// VIP会员(等级权益、周免批次、开通续费)
auth.GET("/vip/info", handler.VipInfo)
auth.POST("/vip/buy", handler.VipBuy)
// 商城(游戏+道具+购物车+订单)
auth.GET("/shop/props", handler.PropList)
auth.GET("/shop/cart", handler.CartList)
auth.POST("/shop/cart", handler.AddCart)
auth.DELETE("/shop/cart/:id", handler.RemoveCart)
auth.POST("/shop/checkout", handler.Checkout)
auth.POST("/shop/buy", handler.BuyNow)
auth.GET("/shop/orders", handler.OrderList)
// 排行榜
auth.GET("/rank/points", handler.PointsRank)
auth.GET("/rank/game/:code", handler.GameRank)
// 好友(申请/同意/列表/搜索/删除)
auth.GET("/friends", handler.FriendList)
auth.GET("/friends/requests", handler.FriendRequests)
auth.GET("/friends/search", handler.FriendSearch)
auth.POST("/friends/request", handler.FriendRequest)
auth.POST("/friends/respond", handler.FriendRespond)
auth.DELETE("/friends/:uid", handler.FriendDelete)
// 私聊(会话列表/聊天记录/发送/已读/未读数)
auth.GET("/chat/conversations", handler.ChatConversations)
auth.GET("/chat/messages", handler.ChatMessages)
auth.POST("/chat/send", handler.ChatSend)
auth.POST("/chat/read", handler.ChatRead)
auth.GET("/chat/unread", handler.ChatUnread)
// 后台管理(仅超管)
admin := auth.Group("/admin", middleware.AdminOnly())
{
admin.GET("/stats", handler.AdminStats)
admin.GET("/analytics", handler.AdminAnalytics)
admin.GET("/themes", handler.AdminThemes)
admin.POST("/theme", handler.AdminSwitchTheme)
admin.PUT("/site", handler.AdminUpdateSite)
admin.GET("/users", handler.AdminUsers)
admin.PUT("/users/:id", handler.AdminUpdateUser)
admin.GET("/users/:id/points", handler.AdminUserPoints)
admin.GET("/games", handler.AdminGames)
admin.PUT("/games/:id", handler.AdminUpdateGame)
admin.GET("/orders", handler.AdminOrders)
// VIP等级配置
admin.GET("/vip/levels", handler.AdminVipLevels)
admin.PUT("/vip/levels/:id", handler.AdminUpdateVipLevel)
// AI大模型接入配置(查看/保存/连通性测试)
admin.GET("/ai", handler.AdminAIConfig)
admin.PUT("/ai", handler.AdminSaveAIConfig)
admin.POST("/ai/test", handler.AdminTestAI)
2026-08-14 17:50:37 +08:00
// 桌面端版本:列表 / 上传安装包 / 改说明 / 删除
admin.GET("/version", handler.AdminVersionGet)
admin.PUT("/version", handler.AdminVersionSave)
admin.POST("/version/upload", handler.AdminVersionUpload)
admin.DELETE("/version/:filename", handler.AdminVersionDelete)
2026-08-14 13:17:03 +08:00
}
}
}
return r
}