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)
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)
}
}
}