pcgamedb/server/route.go
2024-11-29 06:04:28 +08:00

169 lines
4.1 KiB
Go

package server
import (
"path/filepath"
"pcgamedb/crawler"
"pcgamedb/db"
"pcgamedb/log"
"pcgamedb/server/handler"
"pcgamedb/server/middleware"
"strconv"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
"pcgamedb/docs"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
func initRoute(app *gin.Engine) {
app.Use(cors.New(cors.Config{
AllowAllOrigins: true,
}))
initFrontend(app)
initApi(app)
}
func initFrontend(app *gin.Engine) {
r := multitemplate.NewRenderer()
app.Static("/static", "server/static")
layoutFiles, err := filepath.Glob("server/templates/layouts/*.html")
if err != nil {
log.Logger.Fatal("Error loading layout templates", zap.Error(err))
return
}
rootFiles, err := filepath.Glob("server/templates/*.html")
if err != nil {
log.Logger.Fatal("Error loading root templates", zap.Error(err))
return
}
for _, rootFile := range rootFiles {
name := filepath.Base(rootFile)
r.AddFromFiles(name, append([]string{rootFile}, layoutFiles...)...)
}
app.HTMLRender = r
app.GET("/", func(ctx *gin.Context) {
monthTop, err := crawler.GetSteam250MonthTop50Cache()
if err != nil {
ctx.HTML(500, "500.html", err)
return
}
mostPlayed, err := crawler.GetSteam250MostPlayedCache()
if err != nil {
ctx.HTML(500, "500.html", err)
return
}
bestOfTheYear, err := crawler.GetSteam250BestOfTheYearCache()
if err != nil {
ctx.HTML(500, "500.html", err)
return
}
ctx.HTML(200, "index.html", gin.H{
"MonthTop": monthTop,
"MostPlayed": mostPlayed,
"BestOfTheYear": bestOfTheYear,
})
})
app.GET("/game/:id", func(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := primitive.ObjectIDFromHex(idStr)
if err != nil {
ctx.HTML(400, "400.html", nil)
return
}
info, err := db.GetGameInfoByID(id)
if err != nil {
ctx.HTML(500, "500.html", err)
return
}
games, err := db.GetGameItemsByIDs(info.GameIDs)
if err != nil {
ctx.HTML(500, "500.html", err)
return
}
info.Games = games
ctx.HTML(200, "game.html", info)
})
app.GET("/search", func(ctx *gin.Context) {
key := ctx.Query("key")
page := ctx.Query("page")
if len(key) < 2 {
ctx.HTML(400, "400.html", nil)
return
}
if page == "" {
page = "1"
}
pageInt, err := strconv.Atoi(page)
if err != nil {
ctx.HTML(400, "400.html", nil)
return
}
if key == "" {
ctx.HTML(400, "400.html", nil)
return
}
games, totalPage, err := db.SearchGameInfos(key, pageInt, 10)
if err != nil {
ctx.HTML(500, "500.html", err)
return
}
res := gin.H{
"Games": games,
"TotalPage": totalPage,
"CurrentPage": pageInt,
"Key": key,
}
if pageInt > 1 {
res["PrevPage"] = pageInt - 1
}
if pageInt < totalPage {
res["NextPage"] = pageInt + 1
}
ctx.HTML(200, "search.html", res)
})
}
func initApi(app *gin.Engine) {
apiGroup := app.Group("/api")
GameInfoGroup := apiGroup.Group("/game")
GameItemGroup := GameInfoGroup.Group("/raw")
GameItemGroup.GET("/unorganized", handler.GetUnorganizedGameItemsHandler)
GameItemGroup.POST("/organize", middleware.Auth(), handler.OrganizeGameItemHandler)
GameItemGroup.GET("/id/:id", handler.GetGameItemByIDHanlder)
GameItemGroup.GET("/name/:name", handler.GetGameItemByRawNameHandler)
GameItemGroup.GET("/author/:author", handler.GetGameItemsByAuthorHandler)
GameInfoGroup.GET("/search", handler.SearchGamesHandler)
GameInfoGroup.GET("/name/:name", handler.GetGameInfosByNameHandler)
GameInfoGroup.GET("/platform/:platform_type/:platform_id", handler.GetGameInfoByPlatformIDHandler)
GameInfoGroup.GET("/id/:id", handler.GetGameInfoByIDHandler)
GameInfoGroup.DELETE("/id/:id", middleware.Auth(), handler.DeleteGameInfoHandler)
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"
apiGroup.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
}