pcgamedb/server/route.go

108 lines
2.9 KiB
Go
Raw Normal View History

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
"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")
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")
2024-09-24 06:17:11 -04: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
}