updated deps; switched sitemap lib; ent regen
This commit is contained in:
81
main.go
81
main.go
@@ -23,7 +23,6 @@ import (
|
||||
"github.com/go-redis/redis/v8"
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
"github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs/common"
|
||||
"github.com/sabloger/sitemap-generator/smg"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wercker/journalhook"
|
||||
"golang.org/x/text/language"
|
||||
@@ -33,6 +32,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"somegit.dev/anonfunc/gositemap"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
@@ -50,6 +50,7 @@ var (
|
||||
configFlag = flag.String("config", "config.yaml", "Set config file to use")
|
||||
journalLogFlag = flag.Bool("journal", false, "Log to systemd journal instead of stdout")
|
||||
sqlDebugFlag = flag.Bool("sqldebug", false, "Debug SQL queries")
|
||||
siteMap *gositemap.SiteMap
|
||||
)
|
||||
|
||||
func housekeeping() {
|
||||
@@ -1051,53 +1052,35 @@ func getMatch(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, mResponse)
|
||||
}
|
||||
|
||||
func getSiteMapIndex(c *gin.Context) {
|
||||
res, err := siteMap.SiteMapIndex()
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Data(http.StatusOK, "application/xml", res)
|
||||
}
|
||||
|
||||
func getSiteMap(c *gin.Context) {
|
||||
now := time.Now().UTC()
|
||||
sm := smg.NewSitemap(false)
|
||||
sm.SetLastMod(&now)
|
||||
sm.SetHostname("https://csgow.tf")
|
||||
if c.Param("num") == "" {
|
||||
_ = c.AbortWithError(http.StatusBadRequest, fmt.Errorf("no index specified"))
|
||||
return
|
||||
}
|
||||
|
||||
players, err := db.Player.Query().IDs(c)
|
||||
id, err := strconv.Atoi(c.Param("num"))
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, tPlayer := range players {
|
||||
err = sm.Add(&smg.SitemapLoc{
|
||||
Loc: fmt.Sprintf("/player/%d", tPlayer),
|
||||
ChangeFreq: smg.Daily,
|
||||
})
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
matches, err := db.Match.Query().IDs(c)
|
||||
res, err := siteMap.SiteMap(id)
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, tMatch := range matches {
|
||||
err = sm.Add(&smg.SitemapLoc{
|
||||
Loc: fmt.Sprintf("/match/%d", tMatch),
|
||||
ChangeFreq: smg.Weekly,
|
||||
})
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
readBuf := new(bytes.Buffer)
|
||||
sm.Finalize()
|
||||
_, err = sm.WriteTo(readBuf)
|
||||
if err != nil {
|
||||
log.Warningf("error writing to pipe: %v", err)
|
||||
}
|
||||
c.DataFromReader(http.StatusOK, int64(readBuf.Len()), "application/xml", readBuf, nil)
|
||||
c.Data(http.StatusOK, "application/xml", res)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1205,6 +1188,29 @@ func main() {
|
||||
// start housekeeper
|
||||
go housekeeping()
|
||||
|
||||
// populate sitemap
|
||||
siteMap = &gositemap.SiteMap{BaseURL: "csgow.tf"}
|
||||
players, err := db.Player.Query().IDs(context.Background())
|
||||
if err != nil {
|
||||
log.Panicf("error setting up SiteMap: %v", err)
|
||||
}
|
||||
|
||||
for _, tPlayer := range players {
|
||||
freq := gositemap.Daily
|
||||
siteMap.AddURL(fmt.Sprintf("/player/%d", tPlayer), nil, &freq, nil)
|
||||
}
|
||||
|
||||
matches, err := db.Match.Query().IDs(context.Background())
|
||||
if err != nil {
|
||||
log.Panicf("error setting up SiteMap: %v", err)
|
||||
}
|
||||
|
||||
for _, tMatch := range matches {
|
||||
freq := gositemap.Weekly
|
||||
siteMap.AddURL(fmt.Sprintf("/match/%d", tMatch), nil, &freq, nil)
|
||||
}
|
||||
|
||||
// routes
|
||||
r := gin.New()
|
||||
r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("%s - - \"%s %s %s\" %d %d %q %q %s %s\n",
|
||||
@@ -1238,7 +1244,8 @@ func main() {
|
||||
r.GET("/match/:id/chat", getMatchChat)
|
||||
r.GET("/matches", getMatches)
|
||||
r.GET("/matches/next/:time", getMatches)
|
||||
r.GET("/sitemap", getSiteMap)
|
||||
r.GET("/sitemap_index.xml", getSiteMapIndex)
|
||||
r.GET("/sitemap:num.xml", getSiteMap)
|
||||
|
||||
log.Info("Start listening...")
|
||||
|
||||
|
Reference in New Issue
Block a user