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"}} + +
{{.Description}}
+