diff --git a/server/route.go b/server/route.go index 3c50b16..6e963d5 100644 --- a/server/route.go +++ b/server/route.go @@ -1,11 +1,17 @@ package server import ( + "path/filepath" + "pcgamedb/crawler" + "pcgamedb/db" + "pcgamedb/log" "pcgamedb/server/handler" "pcgamedb/server/middleware" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.uber.org/zap" "pcgamedb/docs" @@ -18,7 +24,65 @@ func initRoute(app *gin.Engine) { AllowAllOrigins: true, })) - GameInfoGroup := app.Group("/game") + initFrontend(app) + initApi(app) +} + +func initFrontend(app *gin.Engine) { + app.Static("/static", "server/static") + + layoutFiles, err := filepath.Glob("server/templates/layouts/*.tmpl") + if err != nil { + log.Logger.Fatal("Error loading layout templates", zap.Error(err)) + return + } + + rootFiles, err := filepath.Glob("server/templates/*.tmpl") + if err != nil { + log.Logger.Fatal("Error loading root templates", zap.Error(err)) + return + } + + app.LoadHTMLFiles(append(layoutFiles, rootFiles...)...) + + app.GET("/", func(ctx *gin.Context) { + infos, err := crawler.GetSteam250MonthTop50() + if err != nil { + ctx.HTML(500, "500.tmpl", err) + return + } + ctx.HTML(200, "index.tmpl", gin.H{ + "MonthTop": infos, + }) + }) + + app.GET("/game/:id", func(ctx *gin.Context) { + idStr := ctx.Param("id") + id, err := primitive.ObjectIDFromHex(idStr) + if err != nil { + ctx.HTML(400, "400.tmpl", nil) + return + } + info, err := db.GetGameInfoByID(id) + if err != nil { + ctx.HTML(500, "500.tmpl", err) + return + } + games, err := db.GetGameItemsByIDs(info.GameIDs) + if err != nil { + ctx.HTML(500, "500.tmpl", err) + return + } + info.Games = games + //TODO: fix this + ctx.HTML(200, "game.tmpl", info) + }) +} + +func initApi(app *gin.Engine) { + apiGroup := app.Group("/api") + + GameInfoGroup := apiGroup.Group("/game") GameItemGroup := GameInfoGroup.Group("/raw") GameItemGroup.GET("/unorganized", handler.GetUnorganizedGameItemsHandler) @@ -33,11 +97,11 @@ func initRoute(app *gin.Engine) { GameInfoGroup.GET("/id/:id", handler.GetGameInfoByIDHandler) GameInfoGroup.DELETE("/id/:id", middleware.Auth(), handler.DeleteGameInfoHandler) - app.GET("/popular/:type", handler.GetPopularGameInfosHandler) - app.GET("/healthcheck", handler.HealthCheckHandler) - app.GET("/author", handler.GetAllAuthorsHandler) - app.POST("/clean", middleware.Auth(), handler.CleanGameHandler) + apiGroup.GET("/popular/:type", handler.GetPopularGameInfosHandler) + apiGroup.GET("/healthcheck", handler.HealthCheckHandler) + apiGroup.GET("/author", handler.GetAllAuthorsHandler) + apiGroup.POST("/clean", middleware.Auth(), handler.CleanGameHandler) docs.SwaggerInfo.BasePath = "/api" - app.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler)) + apiGroup.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler)) } diff --git a/server/templates/404.html b/server/templates/404.html new file mode 100644 index 0000000..750ea02 --- /dev/null +++ b/server/templates/404.html @@ -0,0 +1,2 @@ +{{template "base" .}} {{define "title"}}Page Not Found{{end}} {{define +"styles"}} {{end}} {{define "content"}} Page Not Found {{end}} diff --git a/server/templates/500.html b/server/templates/500.html new file mode 100644 index 0000000..47acdb0 --- /dev/null +++ b/server/templates/500.html @@ -0,0 +1,2 @@ +{{template "base" .}} {{define "title"}}Server Error{{end}} {{define "styles"}} +{{end}} {{define "content"}} {{ . }} {{end}} diff --git a/server/templates/game.html b/server/templates/game.html new file mode 100644 index 0000000..039480e --- /dev/null +++ b/server/templates/game.html @@ -0,0 +1,189 @@ +{{template "base" .}} {{define "title"}}{{.Name}} - 游戏详情{{end}} {{define +"styles"}} + +{{end}} {{define "content"}} + +
+ +
+
+ {{if .Cover}} + {{.Name}} + {{else}} +
+ 暂无封面 +
+ {{end}} +
+
+

{{.Name}}

+ + {{if .Aliases}} +
+ 别名: + {{range .Aliases}} + {{.}} + {{end}} +
+ {{end}} + +
+ 开发商: + {{range .Developers}} + {{.}} + {{end}} +
+ +
+ 发行商: + {{range .Publishers}} + {{.}} + {{end}} +
+ + {{if .Languages}} +
+ 支持语言: + {{range .Languages}} + {{.}} + {{end}} +
+ {{end}} {{if .Description}} +
+

{{.Description}}

+
+ {{end}} {{if .SteamID}} +
+ + 在 Steam 上查看 + +
+ {{end}} +
+
+ + + {{if .Screenshots}} +
+

游戏截图

+ +
+ {{end}} + + + {{if .Games}} +
+

下载链接

+
+ {{range .Games}} +
+
+
+
{{.Name}}
+ {{if .Size}} +

+ 文件大小:{{.Size}} +

+ {{end}} {{if .Author}} +

+ 来源:{{.Author}} +

+ {{end}} {{if .Password}} +

解压密码:{{.Password}}

+ {{end}} + +
+ +
+
+ {{end}} +
+
+ {{end}} +
+{{end}} {{define "scripts"}} + + +{{end}} diff --git a/server/templates/index.html b/server/templates/index.html new file mode 100644 index 0000000..847e778 --- /dev/null +++ b/server/templates/index.html @@ -0,0 +1,90 @@ +{{template "base" .}} {{define "title"}}GameDB{{end}} {{define "styles"}} + +{{end}} {{define "content"}} + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+

Month Top

+
+ {{range .MonthTop}} + + {{end}} +
+
+{{end}} {{define "scripts"}} {{end}} diff --git a/server/templates/layouts/base.html b/server/templates/layouts/base.html new file mode 100644 index 0000000..179bc29 --- /dev/null +++ b/server/templates/layouts/base.html @@ -0,0 +1,26 @@ +{{define "base"}} + + + + + + {{template "title" .}} + + + {{block "styles" .}}{{end}} + + + {{template "header" .}} {{template "content" .}} {{template "footer" .}} + + + + {{block "scripts" .}}{{end}} + + +{{end}} diff --git a/server/templates/layouts/footer.html b/server/templates/layouts/footer.html new file mode 100644 index 0000000..4c19a7e --- /dev/null +++ b/server/templates/layouts/footer.html @@ -0,0 +1,17 @@ +{{define "footer"}} + + +{{end}} diff --git a/server/templates/layouts/header.html b/server/templates/layouts/header.html new file mode 100644 index 0000000..757a137 --- /dev/null +++ b/server/templates/layouts/header.html @@ -0,0 +1,16 @@ +{{define "header"}} + + +{{end}}