2024-07-29 21:32:27 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-09-13 22:52:21 +08:00
|
|
|
|
"flag"
|
2024-07-29 21:32:27 +08:00
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
|
2024-09-13 22:52:21 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/redis"
|
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
|
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
|
|
|
|
|
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
2024-07-29 21:32:27 +08:00
|
|
|
|
dw_sdk "gitea.timerzz.com/kedaya_haitao/dw-sdk"
|
2024-09-13 22:52:21 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/dw-spider/server"
|
2024-07-29 21:32:27 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/dw-spider/spider"
|
2024-07-29 21:49:39 +08:00
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
|
|
|
"github.com/gofiber/fiber/v3/middleware/cors"
|
2024-09-13 22:52:21 +08:00
|
|
|
|
"github.com/gofiber/fiber/v3/middleware/recover"
|
2024-07-29 21:32:27 +08:00
|
|
|
|
"github.com/golang/glog"
|
2024-09-13 22:52:21 +08:00
|
|
|
|
"golang.org/x/sync/errgroup"
|
2024-07-29 21:32:27 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-09-13 22:52:21 +08:00
|
|
|
|
flag.Parse()
|
|
|
|
|
glog.Info(">>> BEGIN INIT<<<")
|
2024-07-29 21:32:27 +08:00
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
|
|
|
|
|
defer cancel()
|
2024-09-13 22:52:21 +08:00
|
|
|
|
|
|
|
|
|
// 读取环境变量
|
|
|
|
|
sellerId := v2.SellerId(os.Getenv("SELLER_ID"))
|
|
|
|
|
if sellerId == "" {
|
|
|
|
|
glog.Fatal("SELLER_ID 未设置")
|
2024-07-29 21:32:27 +08:00
|
|
|
|
}
|
2024-09-13 22:52:21 +08:00
|
|
|
|
|
|
|
|
|
bidType := dw_sdk.LoadBiddingType(os.Getenv("BID_TYPE"))
|
|
|
|
|
|
|
|
|
|
db, err := database.InitDefaultDatabase()
|
2024-07-29 21:32:27 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("初始化数据库失败:%v", err)
|
|
|
|
|
}
|
2024-09-13 22:52:21 +08:00
|
|
|
|
// 初始化redis
|
|
|
|
|
rdb, err := redis.InitDefaultRedis()
|
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("初始化redis失败:%v", err)
|
|
|
|
|
}
|
|
|
|
|
client, err := dw_sdk.InitDefaultDWClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("初始化redis失败:%v", err)
|
|
|
|
|
}
|
|
|
|
|
// wait group
|
|
|
|
|
var wg, _ctx = errgroup.WithContext(ctx)
|
|
|
|
|
// 控制器
|
|
|
|
|
ctl := spider.NewController(
|
|
|
|
|
ctx,
|
|
|
|
|
sellerId,
|
|
|
|
|
client,
|
|
|
|
|
storage.NewStorage(db),
|
|
|
|
|
rdb,
|
|
|
|
|
bidType,
|
2024-07-29 21:32:27 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-09-13 22:52:21 +08:00
|
|
|
|
wg.Go(ctl.Run)
|
|
|
|
|
|
|
|
|
|
// server
|
|
|
|
|
r := fiber.New(fiber.Config{ErrorHandler: web.ErrHandle})
|
|
|
|
|
|
|
|
|
|
r.Use(cors.New(), recover.New())
|
|
|
|
|
|
|
|
|
|
for _, register := range []web.Register{
|
|
|
|
|
server.NewSpiderSvc(ctl, sellerId),
|
|
|
|
|
web.NewProbe().SetReadyProbe(func(ctx fiber.Ctx) error {
|
|
|
|
|
if ctl.Ready() {
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
} else {
|
|
|
|
|
return ctx.SendStatus(fiber.StatusServiceUnavailable)
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
} {
|
|
|
|
|
register.Registry(r)
|
|
|
|
|
}
|
|
|
|
|
glog.Info(">>> BEGIN RUN<<<")
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
|
if port == "" {
|
|
|
|
|
port = "80"
|
|
|
|
|
}
|
2024-07-29 21:49:39 +08:00
|
|
|
|
|
2024-09-13 22:52:21 +08:00
|
|
|
|
wg.Go(func() error {
|
|
|
|
|
defer glog.Infof("server 服务退出")
|
|
|
|
|
return r.Listen(":"+port, fiber.ListenConfig{
|
|
|
|
|
EnablePrintRoutes: true,
|
|
|
|
|
GracefulContext: _ctx,
|
|
|
|
|
})
|
2024-07-29 21:49:39 +08:00
|
|
|
|
})
|
|
|
|
|
|
2024-09-13 22:52:21 +08:00
|
|
|
|
if err = wg.Wait(); err != nil {
|
|
|
|
|
glog.Fatalf("服务异常退出:%v", err)
|
2024-07-29 21:49:39 +08:00
|
|
|
|
}
|
2024-07-29 21:32:27 +08:00
|
|
|
|
}
|