package main import ( "context" "flag" "os" "os/signal" "gitea.timerzz.com/kedaya_haitao/common/pkg/database" "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" dw_sdk "gitea.timerzz.com/kedaya_haitao/dw-sdk" "gitea.timerzz.com/kedaya_haitao/dw-spider/server" "gitea.timerzz.com/kedaya_haitao/dw-spider/spider" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/cors" "github.com/gofiber/fiber/v3/middleware/recover" "github.com/golang/glog" "golang.org/x/sync/errgroup" ) func main() { flag.Parse() glog.Info(">>> BEGIN INIT<<<") ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt) defer cancel() // 读取环境变量 sellerId := v2.SellerId(os.Getenv("SELLER_ID")) if sellerId == "" { glog.Fatal("SELLER_ID 未设置") } bidType := dw_sdk.LoadBiddingType(os.Getenv("BID_TYPE")) db, err := database.InitDefaultDatabase() if err != nil { glog.Fatalf("初始化数据库失败:%v", err) } // 初始化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, ) 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" } wg.Go(func() error { defer glog.Infof("server 服务退出") return r.Listen(":"+port, fiber.ListenConfig{ EnablePrintRoutes: true, GracefulContext: _ctx, }) }) if err = wg.Wait(); err != nil { glog.Fatalf("服务异常退出:%v", err) } }