package main
import (
"net/http"
"time"
"yungao/models"
"yungao/router"
"github.com/gin-gonic/gin"
"github.com/robfig/cron"
)
func init() {
//全局设置时区
location, _ := time.LoadLocation("Asia/Shanghai")
time.Local = location
cr := cron.New()
cr.AddFunc("0 5 15 5 * *", func() {
models.Log.Errorf(":", "开始执行")
var c gin.Context
models.GetPriceList(&c) //执行的方法
})
cr.Start()
}
func main() {
gin.SetMode(gin.ReleaseMode) //模式设置为 release
r := gin.Default() //创建一个服务
cors := func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
}
r.Use(ErrorHandler(), cors)
router.RoutersInit(r)
r.Run(":8811" )
}
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
models.Log.Errorf(":", err, c.Request.URL.Path)
c.AbortWithStatusJSON(http.StatusOK, gin.H{"res": "fail", "msg": err})
}
}()
c.Next()
if len(c.Errors) > 0 {
panicOnError(c.Errors[0].Err)
}
}
}
golang 定时任务
于 2023-12-29 14:08:38 首次发布