package main import ( "context" "flag" "os" "os/signal" ats_tracer "gitea.timerzz.com/kedaya_haitao/coach-spider/ats-tracer" "gitea.timerzz.com/kedaya_haitao/coach-spider/pkg/options" "gitea.timerzz.com/kedaya_haitao/coach-spider/server" "gitea.timerzz.com/kedaya_haitao/coach-spider/spider" "gitea.timerzz.com/kedaya_haitao/coach-spider/watcher" "gitea.timerzz.com/kedaya_haitao/common/pkg/coach-client" "gitea.timerzz.com/kedaya_haitao/common/pkg/database" "gitea.timerzz.com/kedaya_haitao/common/pkg/proxy" "gitea.timerzz.com/kedaya_haitao/common/pkg/redis" "gitea.timerzz.com/kedaya_haitao/common/pkg/web" "gitea.timerzz.com/kedaya_haitao/pusher/rpc/pusher" "github.com/cloudwego/kitex/client" "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() cfg, err := options.LoadConfigs() if err != nil { glog.Fatalf("获取配置失败:%v", err) } // 初始化数据库 db, err := database.InitDefaultDatabase() if err != nil { glog.Fatalf("初始化数据库失败:%v", err) } // 初始化redis rdb, err := redis.InitDefaultRedis() if err != nil { glog.Fatalf("初始化redis失败:%v", err) } pusher.InitClient("pusher", client.WithHostPorts("pusher:8080")) // 代理池 pool, err := proxy.InitDefaultProxyPool() if err != nil { glog.Fatalf("初始化代理池失败:%v", err) } go pool.CronUpdate(ctx) //定时更新代理 // coach client cli := coach_client.US(pool).CreateByType(cfg.ClientType) var wg, _ctx = errgroup.WithContext(ctx) spider := spider.NewController( _ctx, cfg.ProviderId, cli, db, rdb, ) watcher := watcher.NewController( _ctx, cfg, cli, db, ) tracer := ats_tracer.NewController( _ctx, cfg, cli, db, ) wg.Go(spider.Run) wg.Go(watcher.Run) wg.Go(tracer.Run) wg.Go(tracer.CleanAts) // http r := fiber.New(fiber.Config{ErrorHandler: web.ErrHandle}) r.Use(cors.New(), recover.New()) // 判断服务是否就绪 var readyProbe = func(ctx fiber.Ctx) error { if spider.Ready() && watcher.Ready() { return ctx.SendStatus(fiber.StatusOK) } else { return ctx.SendStatus(fiber.StatusServiceUnavailable) } } for _, register := range []web.Register{ server.NewSpiderSvc(spider, cfg.ProviderId), server.NewWatcherSvc(watcher, cfg.ProviderId), server.NewAtsTracerSvc(tracer, cfg.ProviderId), web.NewProbe().SetReadyProbe(readyProbe), } { 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) } }