diff --git a/crawler/igdb.go b/crawler/igdb.go index ec70d01..9a399c7 100644 --- a/crawler/igdb.go +++ b/crawler/igdb.go @@ -697,3 +697,30 @@ func GetIGDBPopularGameIDs(popularityType int, offset int, limit int) ([]int, er } return ret, nil } + +func GetIGDBPopularGameIDsCache(popularityType int, offset int, limit int) ([]int, error) { + if config.Config.RedisAvaliable { + key := fmt.Sprintf("igdb_popular_game_ids:%v:%v:%v", popularityType, offset, limit) + val, exist := cache.Get(key) + if exist { + var data []int + if err := json.Unmarshal([]byte(val), &data); err != nil { + return nil, err + } + return data, nil + } else { + data, err := GetIGDBPopularGameIDs(popularityType, offset, limit) + if err != nil { + return nil, err + } + jsonBytes, err := json.Marshal(data) + if err != nil { + return nil, err + } + _ = cache.AddWithExpire(key, jsonBytes, 12*time.Hour) + return data, nil + } + } else { + return GetIGDBPopularGameIDs(popularityType, offset, limit) + } +} diff --git a/crawler/steam250.go b/crawler/steam250.go index f0d820d..bd45a6b 100644 --- a/crawler/steam250.go +++ b/crawler/steam250.go @@ -2,7 +2,10 @@ package crawler import ( "bytes" + "encoding/json" "fmt" + "pcgamedb/cache" + "pcgamedb/config" "pcgamedb/db" "regexp" "strconv" @@ -54,18 +57,158 @@ func GetSteam250Top250() ([]*model.GameInfo, error) { return GetSteam250(constant.Steam250Top250URL) } +func GetSteam250Top250Cache() ([]*model.GameInfo, error) { + if config.Config.RedisAvaliable { + key := "steam250_top250" + val, exist := cache.Get(key) + if exist { + var infos []*model.GameInfo + err := json.Unmarshal([]byte(val), &infos) + if err != nil { + return nil, err + } + return infos, nil + } else { + infos, err := GetSteam250Top250() + if err != nil { + return nil, err + } + jsonBytes, err := json.Marshal(infos) + if err != nil { + return nil, err + } + _ = cache.AddWithExpire(key, string(jsonBytes), 12*time.Hour) + return infos, nil + } + } else { + return GetSteam250Top250() + } +} + func GetSteam250BestOfTheYear() ([]*model.GameInfo, error) { return GetSteam250(fmt.Sprintf(constant.Steam250BestOfTheYearURL, time.Now().UTC().Year())) } +func GetSteam250BestOfTheYearCache() ([]*model.GameInfo, error) { + if config.Config.RedisAvaliable { + key := "steam250_best_of_the_year" + val, exist := cache.Get(key) + if exist { + var infos []*model.GameInfo + err := json.Unmarshal([]byte(val), &infos) + if err != nil { + return nil, err + } + return infos, nil + } else { + infos, err := GetSteam250BestOfTheYear() + if err != nil { + return nil, err + } + jsonBytes, err := json.Marshal(infos) + if err != nil { + return nil, err + } + _ = cache.AddWithExpire(key, string(jsonBytes), 12*time.Hour) + return infos, nil + } + } else { + return GetSteam250BestOfTheYear() + } +} + func GetSteam250WeekTop50() ([]*model.GameInfo, error) { return GetSteam250(constant.Steam250WeekTop50URL) } +func GetSteam250WeekTop50Cache() ([]*model.GameInfo, error) { + if config.Config.RedisAvaliable { + key := "steam250_week_top50" + val, exist := cache.Get(key) + if exist { + var infos []*model.GameInfo + err := json.Unmarshal([]byte(val), &infos) + if err != nil { + return nil, err + } + return infos, nil + } else { + infos, err := GetSteam250WeekTop50() + if err != nil { + return nil, err + } + jsonBytes, err := json.Marshal(infos) + if err != nil { + return nil, err + } + _ = cache.AddWithExpire(key, string(jsonBytes), 12*time.Hour) + return infos, nil + } + } else { + return GetSteam250WeekTop50() + } +} + func GetSteam250MonthTop50() ([]*model.GameInfo, error) { return GetSteam250(constant.Steam250MonthTop50URL) } +func GetSteam250MonthTop50Cache() ([]*model.GameInfo, error) { + if config.Config.RedisAvaliable { + key := "steam250_month_top50" + val, exist := cache.Get(key) + if exist { + var infos []*model.GameInfo + err := json.Unmarshal([]byte(val), &infos) + if err != nil { + return nil, err + } + return infos, nil + } else { + infos, err := GetSteam250MonthTop50() + if err != nil { + return nil, err + } + jsonBytes, err := json.Marshal(infos) + if err != nil { + return nil, err + } + _ = cache.AddWithExpire(key, string(jsonBytes), 12*time.Hour) + return infos, nil + } + } else { + return GetSteam250MonthTop50() + } +} + func GetSteam250MostPlayed() ([]*model.GameInfo, error) { return GetSteam250(constant.Steam250MostPlayedURL) } + +func GetSteam250MostPlayedCache() ([]*model.GameInfo, error) { + if config.Config.RedisAvaliable { + key := "steam250_most_played" + val, exist := cache.Get(key) + if exist { + var infos []*model.GameInfo + err := json.Unmarshal([]byte(val), &infos) + if err != nil { + return nil, err + } + return infos, nil + } else { + infos, err := GetSteam250MostPlayed() + if err != nil { + return nil, err + } + jsonBytes, err := json.Marshal(infos) + if err != nil { + return nil, err + } + _ = cache.AddWithExpire(key, string(jsonBytes), 12*time.Hour) + return infos, nil + } + } else { + return GetSteam250MostPlayed() + } +} diff --git a/db/game.go b/db/game.go index 8af5f8f..77e96ef 100644 --- a/db/game.go +++ b/db/game.go @@ -291,6 +291,7 @@ func GetGameItemsByIDs(ids []primitive.ObjectID) ([]*model.GameItem, error) { return items, err } +// SearchGameItems page start from 1, return (items, totalPage, error) func SearchGameInfos(name string, page int, pageSize int) ([]*model.GameInfo, int, error) { var items []*model.GameInfo name = removeNoneAlphaNumeric.ReplaceAllString(name, " ") diff --git a/go.mod b/go.mod index 3d1b58b..e4b5602 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( github.com/cloudwego/iasm v0.2.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/gabriel-vasile/mimetype v1.4.6 // indirect + github.com/gin-contrib/multitemplate v1.0.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect diff --git a/go.sum b/go.sum index 7877cb7..8285cd9 100644 --- a/go.sum +++ b/go.sum @@ -112,6 +112,8 @@ github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQ github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E= github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk= +github.com/gin-contrib/multitemplate v1.0.1 h1:Asi8boB7NctSoQzbWDosLObon0cYMP5OM+ihQMjlW5M= +github.com/gin-contrib/multitemplate v1.0.1/go.mod h1:uU+PnuKoiEHWqB9Zvco+Kqv9KNrsHi6IZOUUgTctMPA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= diff --git a/server/route.go b/server/route.go index 271434c..172a0ce 100644 --- a/server/route.go +++ b/server/route.go @@ -7,8 +7,10 @@ import ( "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" @@ -29,6 +31,9 @@ func initRoute(app *gin.Engine) { } func initFrontend(app *gin.Engine) { + + r := multitemplate.NewRenderer() + app.Static("/static", "server/static") layoutFiles, err := filepath.Glob("server/templates/layouts/*.html") @@ -43,16 +48,33 @@ func initFrontend(app *gin.Engine) { return } - app.LoadHTMLFiles(append(layoutFiles, rootFiles...)...) + for _, rootFile := range rootFiles { + name := filepath.Base(rootFile) + r.AddFromFiles(name, append([]string{rootFile}, layoutFiles...)...) + } + + app.HTMLRender = r app.GET("/", func(ctx *gin.Context) { - infos, err := crawler.GetSteam250MonthTop50() + 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": infos, + "MonthTop": monthTop, + "MostPlayed": mostPlayed, + "BestOfTheYear": bestOfTheYear, }) }) @@ -74,9 +96,48 @@ func initFrontend(app *gin.Engine) { return } info.Games = games - //TODO: fix this 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) { diff --git a/server/templates/404.html b/server/templates/404.html index 750ea02..e35e539 100644 --- a/server/templates/404.html +++ b/server/templates/404.html @@ -1,2 +1,4 @@ -{{template "base" .}} {{define "title"}}Page Not Found{{end}} {{define -"styles"}} {{end}} {{define "content"}} Page Not Found {{end}} +{{template "base" .}} +{{define "title"}}Page Not Found{{end}} +{{define "styles"}} {{end}} +{{define "content"}} Page Not Found {{end}} \ No newline at end of file diff --git a/server/templates/500.html b/server/templates/500.html index 47acdb0..2e360fb 100644 --- a/server/templates/500.html +++ b/server/templates/500.html @@ -1,2 +1,4 @@ -{{template "base" .}} {{define "title"}}Server Error{{end}} {{define "styles"}} -{{end}} {{define "content"}} {{ . }} {{end}} +{{template "base" .}} +{{define "title"}}Server Error{{end}} +{{define "styles"}}{{end}} +{{define "content"}} {{ . }} {{end}} \ No newline at end of file diff --git a/server/templates/game.html b/server/templates/game.html index 039480e..09778ca 100644 --- a/server/templates/game.html +++ b/server/templates/game.html @@ -1,5 +1,8 @@ -{{template "base" .}} {{define "title"}}{{.Name}} - 游戏详情{{end}} {{define -"styles"}} +{{template "base" .}} + +{{define "title"}}{{.Name}} - 游戏详情{{end}} + +{{define "styles"}} -{{end}} {{define "content"}} +{{end}} + +{{define "content"}}
{{if .Cover}} - {{.Name}} + {{.Name}} {{else}} -
+
暂无封面
{{end}} @@ -98,11 +97,7 @@
{{end}} {{if .SteamID}}
- + 在 Steam 上查看
@@ -113,7 +108,6 @@ {{if .Screenshots}}
-

游戏截图