pcgamedb/server/server.go

60 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-09-24 06:17:11 -04:00
package server
import (
"io"
"time"
2024-11-20 06:09:04 -05:00
"pcgamedb/cache"
"pcgamedb/config"
"pcgamedb/db"
"pcgamedb/log"
"pcgamedb/server/middleware"
"pcgamedb/task"
2024-11-15 02:02:45 -05:00
2024-09-24 06:17:11 -04:00
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
)
func init() {
config.Runtime.ServerStartTime = time.Now()
}
func Run() {
db.CheckConnect()
cache.CheckConnect()
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard
app := gin.New()
app.Use(middleware.Recovery())
initRoute(app)
log.Logger.Info("Server running", zap.String("port", config.Config.Server.Port))
// Start auto-crawl task
2024-09-24 06:17:11 -04:00
if config.Config.Server.AutoCrawl {
go func() {
c := cron.New()
2024-11-17 03:53:47 -05:00
_, err := c.AddFunc(config.Config.Server.AutoCrawlCron, func() { task.Crawl(log.TaskLogger) })
2024-09-24 06:17:11 -04:00
if err != nil {
log.Logger.Error("Error adding cron job", zap.Error(err))
}
c.Start()
}()
}
// Start auto-update task
go func() {
c := cron.New()
_, err := c.AddFunc("0 */3 * * *", func() { task.UpdateOutdatedGameInfos(log.TaskLogger) })
if err != nil {
log.Logger.Error("Error adding cron job", zap.Error(err))
}
c.Start()
}()
2024-09-24 06:17:11 -04:00
err := app.Run(":" + config.Config.Server.Port)
if err != nil {
log.Logger.Panic("Failed to run server", zap.Error(err))
}
}