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" 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) { 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 } app.LoadHTMLFiles(append(layoutFiles, rootFiles...)...) app.GET("/", func(ctx *gin.Context) { infos, err := crawler.GetSteam250MonthTop50() if err != nil { ctx.HTML(500, "500.html", err) return } ctx.HTML(200, "index.html", 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.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 //TODO: fix this ctx.HTML(200, "game.html", info) }) } 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)) }