2024-09-24 06:17:11 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-11-28 14:53:36 -05:00
|
|
|
"path/filepath"
|
|
|
|
"pcgamedb/crawler"
|
|
|
|
"pcgamedb/db"
|
|
|
|
"pcgamedb/log"
|
2024-11-20 06:09:04 -05:00
|
|
|
"pcgamedb/server/handler"
|
|
|
|
"pcgamedb/server/middleware"
|
2024-09-24 06:17:11 -04:00
|
|
|
|
|
|
|
"github.com/gin-contrib/cors"
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-11-28 14:53:36 -05:00
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
"go.uber.org/zap"
|
2024-09-24 06:17:11 -04:00
|
|
|
|
2024-11-21 12:30:26 -05:00
|
|
|
"pcgamedb/docs"
|
2024-09-24 06:17:11 -04:00
|
|
|
|
|
|
|
swaggerfiles "github.com/swaggo/files"
|
|
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func initRoute(app *gin.Engine) {
|
|
|
|
app.Use(cors.New(cors.Config{
|
|
|
|
AllowAllOrigins: true,
|
|
|
|
}))
|
|
|
|
|
2024-11-28 14:53:36 -05:00
|
|
|
initFrontend(app)
|
|
|
|
initApi(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initFrontend(app *gin.Engine) {
|
|
|
|
app.Static("/static", "server/static")
|
|
|
|
|
2024-11-28 14:55:45 -05:00
|
|
|
layoutFiles, err := filepath.Glob("server/templates/layouts/*.html")
|
2024-11-28 14:53:36 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Logger.Fatal("Error loading layout templates", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-28 14:55:45 -05:00
|
|
|
rootFiles, err := filepath.Glob("server/templates/*.html")
|
2024-11-28 14:53:36 -05:00
|
|
|
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 {
|
2024-11-28 14:55:45 -05:00
|
|
|
ctx.HTML(500, "500.html", err)
|
2024-11-28 14:53:36 -05:00
|
|
|
return
|
|
|
|
}
|
2024-11-28 14:55:45 -05:00
|
|
|
ctx.HTML(200, "index.html", gin.H{
|
2024-11-28 14:53:36 -05:00
|
|
|
"MonthTop": infos,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.GET("/game/:id", func(ctx *gin.Context) {
|
|
|
|
idStr := ctx.Param("id")
|
|
|
|
id, err := primitive.ObjectIDFromHex(idStr)
|
|
|
|
if err != nil {
|
2024-11-28 14:55:45 -05:00
|
|
|
ctx.HTML(400, "400.html", nil)
|
2024-11-28 14:53:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
info, err := db.GetGameInfoByID(id)
|
|
|
|
if err != nil {
|
2024-11-28 14:55:45 -05:00
|
|
|
ctx.HTML(500, "500.html", err)
|
2024-11-28 14:53:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
games, err := db.GetGameItemsByIDs(info.GameIDs)
|
|
|
|
if err != nil {
|
2024-11-28 14:55:45 -05:00
|
|
|
ctx.HTML(500, "500.html", err)
|
2024-11-28 14:53:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
info.Games = games
|
|
|
|
//TODO: fix this
|
2024-11-28 14:55:45 -05:00
|
|
|
ctx.HTML(200, "game.html", info)
|
2024-11-28 14:53:36 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func initApi(app *gin.Engine) {
|
|
|
|
apiGroup := app.Group("/api")
|
|
|
|
|
|
|
|
GameInfoGroup := apiGroup.Group("/game")
|
2024-11-16 00:48:48 -05:00
|
|
|
GameItemGroup := GameInfoGroup.Group("/raw")
|
2024-09-24 06:17:11 -04:00
|
|
|
|
2024-11-16 00:48:48 -05:00
|
|
|
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)
|
2024-09-24 06:17:11 -04:00
|
|
|
|
|
|
|
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)
|
2024-09-25 09:33:46 -04:00
|
|
|
GameInfoGroup.DELETE("/id/:id", middleware.Auth(), handler.DeleteGameInfoHandler)
|
2024-09-24 06:17:11 -04:00
|
|
|
|
2024-11-28 14:53:36 -05:00
|
|
|
apiGroup.GET("/popular/:type", handler.GetPopularGameInfosHandler)
|
|
|
|
apiGroup.GET("/healthcheck", handler.HealthCheckHandler)
|
|
|
|
apiGroup.GET("/author", handler.GetAllAuthorsHandler)
|
|
|
|
apiGroup.POST("/clean", middleware.Auth(), handler.CleanGameHandler)
|
2024-09-24 06:17:11 -04:00
|
|
|
|
|
|
|
docs.SwaggerInfo.BasePath = "/api"
|
2024-11-28 14:53:36 -05:00
|
|
|
apiGroup.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|