2024-06-14 16:36:33 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/cn-coach-spider/pkg/options"
|
2024-06-16 11:08:28 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/cn-coach-spider/server"
|
2024-06-14 16:36:33 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/cn-coach-spider/spider"
|
|
|
|
|
coach_client "gitea.timerzz.com/kedaya_haitao/common/pkg/coach-client"
|
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
|
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
|
|
|
"github.com/gofiber/fiber/v3/middleware/cors"
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
|
"gorm.io/gorm/logger"
|
|
|
|
|
"log"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
slog.Info(">>> BEGIN INIT<<<")
|
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
|
|
|
|
|
defer cancel()
|
|
|
|
|
cfg, err := options.LoadConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("获取配置失败:%v", err)
|
|
|
|
|
}
|
|
|
|
|
db, err := database.InitDatabase(&cfg.DB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("初始化数据库失败:%v", err)
|
|
|
|
|
}
|
|
|
|
|
db.Logger = logger.New(
|
|
|
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
|
|
|
|
logger.Config{
|
|
|
|
|
SlowThreshold: time.Second,
|
2024-06-16 11:36:32 +08:00
|
|
|
|
LogLevel: logger.Warn,
|
2024-06-14 16:36:33 +08:00
|
|
|
|
IgnoreRecordNotFoundError: false,
|
|
|
|
|
Colorful: true,
|
|
|
|
|
},
|
|
|
|
|
)
|
2024-06-16 11:08:28 +08:00
|
|
|
|
shopType := os.Getenv("SHOP_TYPE")
|
|
|
|
|
if shopType != options.ShopType_coach && shopType != options.ShopType_outlet {
|
|
|
|
|
glog.Fatalf("SHOP_TYPE环境变量错误,请设置SHOP_TYPE=coach或SHOP_TYPE=outlet")
|
|
|
|
|
}
|
2024-06-14 16:36:33 +08:00
|
|
|
|
// coach client
|
2024-06-16 11:08:28 +08:00
|
|
|
|
var xMac = coach_client.XMac_Coach
|
|
|
|
|
if shopType == options.ShopType_outlet {
|
|
|
|
|
xMac = coach_client.XMac_Outlet
|
|
|
|
|
}
|
|
|
|
|
client, err := coach_client.CNClient(xMac)
|
2024-06-14 16:36:33 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("初始化coach client失败:%v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 11:08:28 +08:00
|
|
|
|
spiderCtl := spider.NewController(client, db, cfg.Interval, shopType)
|
2024-06-14 16:36:33 +08:00
|
|
|
|
|
|
|
|
|
go spiderCtl.Run(ctx)
|
|
|
|
|
|
|
|
|
|
r := fiber.New()
|
|
|
|
|
r.Use(cors.New())
|
|
|
|
|
|
|
|
|
|
r.Get("/health", func(ctx fiber.Ctx) error {
|
|
|
|
|
return ctx.SendString("ok")
|
|
|
|
|
})
|
2024-06-16 11:08:28 +08:00
|
|
|
|
api := r.Group("/api/v1")
|
2024-06-14 16:36:33 +08:00
|
|
|
|
|
2024-06-16 11:08:28 +08:00
|
|
|
|
server.NewSpiderSvc(spiderCtl, shopType).RegistryRouter(api)
|
2024-06-14 16:36:33 +08:00
|
|
|
|
if err = r.Listen(":8080"); err != nil {
|
|
|
|
|
glog.Warningf("server over: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|