905086
2026-11-09 cbb26873e88a7a13e868448c1cc578ce70b8c6ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
 
import (
    "fmt"
    "net/http"
    "os"
    "path/filepath"
 
    "gamebox-server/config"
    "gamebox-server/handlers"
    "gamebox-server/middleware"
 
    "github.com/gin-gonic/gin"
)
 
func main() {
    config.Load("config.yaml")
 
    // 大文件上传时 multipart 会先写系统临时目录(C盘),
    // 系统盘空间不足会导致大压缩包上传失败,这里指到程序目录下的 uploads/tmp
    if abs, err := filepath.Abs(filepath.Join("uploads", "tmp")); err == nil {
        os.MkdirAll(abs, 0755)
        os.Setenv("TMP", abs)
        os.Setenv("TEMP", abs)
    }
 
    config.InitDB()
 
    r := gin.Default()
    r.MaxMultipartMemory = 64 << 20 // 64MB
 
    // 静态资源(封面/视频/游戏包)
    r.Static("/static", "./uploads")
 
    // ---------- 客户端 API ----------
    api := r.Group("/api")
    {
        api.GET("/captcha", handlers.GetCaptcha)
        api.POST("/register", handlers.Register)
        api.POST("/login", handlers.Login)
        api.POST("/reset_password", handlers.ResetPassword)
        api.GET("/categories", handlers.GetCategories)
        api.GET("/games", handlers.GetGames)
        api.GET("/banners", handlers.GetBanners)
        api.GET("/tools", handlers.GetTools)
        api.GET("/tools/:id/download", handlers.ToolDownloadInfo)
        api.GET("/announcements", handlers.GetAnnouncements)
        api.GET("/client/latest", handlers.GetClientLatest)   // 客户端下载页:最新版本信息(公开)
        api.GET("/client/download", handlers.ClientDownload)  // 客户端下载页:下载最新版(公开,累计下载次数)
        api.GET("/games/:id", middleware.OptionalUserAuth(), handlers.GetGameDetail) // 支持可选登录:登录后返回收藏/资源库状态
 
        auth := api.Group("", middleware.UserAuth())
        {
            auth.GET("/user/info", handlers.GetUserInfo)
            auth.POST("/favorites", handlers.ToggleFavorite)
            auth.GET("/favorites", handlers.GetFavorites)
            auth.POST("/library", handlers.AddToLibrary)
            auth.GET("/library", handlers.GetLibrary)
            auth.DELETE("/library/:gameId", handlers.RemoveFromLibrary)
            auth.GET("/download/:id", handlers.GetDownloadInfo)
            auth.POST("/keys/activate", handlers.ActivateKey)
        }
    }
 
    // ---------- 游戏目录展示页(公开,仅浏览不可下载) ----------
    r.GET("/showcase", func(c *gin.Context) {
        c.File("./showcase/index.html")
    })
    r.GET("/", func(c *gin.Context) {
        c.Redirect(http.StatusFound, "/showcase")
    })
 
    // ---------- 客户端下载页(公开,无需登录) ----------
    r.GET("/download", func(c *gin.Context) {
        c.File("./download/index.html")
    })
 
    // ---------- 管理后台 ----------
    r.GET("/admin", func(c *gin.Context) {
        c.File("./admin/index.html")
    })
    admin := r.Group("/admin/api")
    {
        admin.POST("/login", handlers.AdminLogin)
 
        a := admin.Group("", middleware.AdminAuth())
        {
            a.GET("/stats", handlers.AdminStats)
            a.GET("/keys", handlers.ListKeys)
            a.POST("/keys/generate", handlers.GenerateKeys)
            a.POST("/keys/:id/reset", handlers.ResetKey)
            a.POST("/keys/:id/freeze", handlers.FreezeKey)
            a.DELETE("/keys/:id", handlers.DeleteKey)
 
            a.GET("/tools", handlers.AdminListTools)
            a.POST("/tools", handlers.AdminSaveTool)
            a.DELETE("/tools/:id", handlers.AdminDeleteTool)
 
            a.GET("/versions", handlers.AdminListVersions)
            a.POST("/versions", handlers.AdminSaveVersion)
            a.DELETE("/versions/:id", handlers.AdminDeleteVersion)
 
            a.GET("/games", handlers.AdminListGames)
            a.POST("/games", handlers.AdminSaveGame)
            a.DELETE("/games/:id", handlers.AdminDeleteGame)
 
            a.GET("/categories", handlers.AdminListCategories)
            a.POST("/categories", handlers.AdminSaveCategory)
            a.DELETE("/categories/:id", handlers.AdminDeleteCategory)
 
            a.GET("/announcement", handlers.AdminGetAnnouncement)
            a.POST("/announcement", handlers.AdminSaveAnnouncement)
 
            a.POST("/upload", handlers.UploadFile)
 
            // 密码重置申请管理
            a.GET("/reset_requests", handlers.ListResetRequests)
            a.POST("/reset_requests/:id/approve", handlers.ApproveResetRequest)
            a.DELETE("/reset_requests/:id", handlers.DeleteResetRequest)
        }
    }
 
    r.Run(fmt.Sprintf(":%d", config.App.Server.Port))
}