2024-05-14 15:27:40 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-09-13 22:47:19 +08:00
|
|
|
|
"flag"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
|
2024-12-02 19:07:38 +08:00
|
|
|
|
ats_tracer "gitea.timerzz.com/kedaya_haitao/coach-spider/ats-tracer"
|
2024-05-14 15:27:40 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/coach-spider/pkg/options"
|
2024-05-14 21:18:24 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/coach-spider/server"
|
2024-09-13 22:47:19 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/coach-spider/spider"
|
2024-11-30 20:26:37 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/coach-spider/watcher"
|
2024-05-21 15:38:09 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/coach-client"
|
2024-05-14 15:27:40 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
|
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
|
2024-09-13 22:47:19 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/redis"
|
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
|
2024-12-02 19:25:39 +08:00
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/pusher/rpc/pusher"
|
|
|
|
|
"github.com/cloudwego/kitex/client"
|
2024-05-14 21:18:24 +08:00
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
|
|
|
"github.com/gofiber/fiber/v3/middleware/cors"
|
2024-09-13 22:47:19 +08:00
|
|
|
|
"github.com/gofiber/fiber/v3/middleware/recover"
|
2024-05-14 15:27:40 +08:00
|
|
|
|
"github.com/golang/glog"
|
2024-09-13 22:47:19 +08:00
|
|
|
|
"golang.org/x/sync/errgroup"
|
2024-05-14 15:27:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-09-13 22:47:19 +08:00
|
|
|
|
flag.Parse()
|
|
|
|
|
glog.Info(">>> BEGIN INIT<<<")
|
|
|
|
|
|
2024-05-14 15:27:40 +08:00
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
|
|
|
|
|
defer cancel()
|
2024-09-13 22:47:19 +08:00
|
|
|
|
cfg, err := options.LoadConfigs()
|
2024-05-14 15:27:40 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("获取配置失败:%v", err)
|
|
|
|
|
}
|
2024-09-13 22:47:19 +08:00
|
|
|
|
// 初始化数据库
|
|
|
|
|
db, err := database.InitDefaultDatabase()
|
2024-05-14 15:27:40 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("初始化数据库失败:%v", err)
|
|
|
|
|
}
|
2024-09-13 22:47:19 +08:00
|
|
|
|
|
|
|
|
|
// 初始化redis
|
|
|
|
|
rdb, err := redis.InitDefaultRedis()
|
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("初始化redis失败:%v", err)
|
|
|
|
|
}
|
2024-05-14 15:27:40 +08:00
|
|
|
|
|
2024-12-02 19:25:39 +08:00
|
|
|
|
pusher.InitClient("pusher", client.WithHostPorts("pusher:8080"))
|
2024-11-30 20:26:37 +08:00
|
|
|
|
|
2024-05-14 15:27:40 +08:00
|
|
|
|
// 代理池
|
2024-12-04 20:09:58 +08:00
|
|
|
|
pool, err := proxy.InitDefaultProxyPool()
|
|
|
|
|
if err != nil {
|
|
|
|
|
glog.Fatalf("初始化代理池失败:%v", err)
|
|
|
|
|
}
|
2024-05-14 15:27:40 +08:00
|
|
|
|
|
2024-12-04 20:09:58 +08:00
|
|
|
|
go pool.CronUpdate(ctx) //定时更新代理
|
2024-05-14 15:27:40 +08:00
|
|
|
|
|
|
|
|
|
// coach client
|
2024-11-30 20:26:37 +08:00
|
|
|
|
cli := coach_client.USClient(pool)
|
2024-05-14 15:27:40 +08:00
|
|
|
|
|
2024-09-13 22:47:19 +08:00
|
|
|
|
var wg, _ctx = errgroup.WithContext(ctx)
|
2024-05-14 15:27:40 +08:00
|
|
|
|
|
2024-11-30 20:26:37 +08:00
|
|
|
|
spider := spider.NewController(
|
2024-09-13 22:47:19 +08:00
|
|
|
|
_ctx,
|
|
|
|
|
cfg.ProviderId,
|
2024-11-30 20:26:37 +08:00
|
|
|
|
cli,
|
2024-09-13 22:47:19 +08:00
|
|
|
|
db,
|
|
|
|
|
rdb,
|
|
|
|
|
)
|
2024-05-14 21:18:24 +08:00
|
|
|
|
|
2024-11-30 20:26:37 +08:00
|
|
|
|
watcher := watcher.NewController(
|
|
|
|
|
_ctx,
|
|
|
|
|
cfg,
|
|
|
|
|
cli,
|
|
|
|
|
db,
|
|
|
|
|
)
|
2024-12-02 19:07:38 +08:00
|
|
|
|
|
|
|
|
|
tracer := ats_tracer.NewController(
|
|
|
|
|
_ctx,
|
|
|
|
|
cfg,
|
|
|
|
|
cli,
|
|
|
|
|
db,
|
|
|
|
|
)
|
2024-11-30 20:26:37 +08:00
|
|
|
|
wg.Go(spider.Run)
|
|
|
|
|
wg.Go(watcher.Run)
|
2024-12-02 19:07:38 +08:00
|
|
|
|
wg.Go(tracer.Run)
|
2024-12-04 13:12:50 +08:00
|
|
|
|
wg.Go(tracer.CleanAts)
|
2024-11-30 20:26:37 +08:00
|
|
|
|
|
|
|
|
|
// http
|
2024-09-13 22:47:19 +08:00
|
|
|
|
r := fiber.New(fiber.Config{ErrorHandler: web.ErrHandle})
|
2024-05-14 21:18:24 +08:00
|
|
|
|
|
2024-09-13 22:47:19 +08:00
|
|
|
|
r.Use(cors.New(), recover.New())
|
2024-05-14 21:18:24 +08:00
|
|
|
|
|
2024-11-30 20:26:37 +08:00
|
|
|
|
// 判断服务是否就绪
|
|
|
|
|
var readyProbe = func(ctx fiber.Ctx) error {
|
|
|
|
|
if spider.Ready() && watcher.Ready() {
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
} else {
|
|
|
|
|
return ctx.SendStatus(fiber.StatusServiceUnavailable)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 22:47:19 +08:00
|
|
|
|
for _, register := range []web.Register{
|
2024-11-30 20:26:37 +08:00
|
|
|
|
server.NewSpiderSvc(spider, cfg.ProviderId),
|
|
|
|
|
server.NewWatcherSvc(watcher, cfg.ProviderId),
|
2024-12-02 19:07:38 +08:00
|
|
|
|
server.NewAtsTracerSvc(tracer, cfg.ProviderId),
|
2024-11-30 20:26:37 +08:00
|
|
|
|
web.NewProbe().SetReadyProbe(readyProbe),
|
2024-09-13 22:47:19 +08:00
|
|
|
|
} {
|
|
|
|
|
register.Registry(r)
|
|
|
|
|
}
|
|
|
|
|
glog.Info(">>> BEGIN RUN<<<")
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
|
if port == "" {
|
|
|
|
|
port = "80"
|
|
|
|
|
}
|
2024-05-14 21:18:24 +08:00
|
|
|
|
|
2024-09-13 22:47:19 +08:00
|
|
|
|
wg.Go(func() error {
|
|
|
|
|
defer glog.Infof("server 服务退出")
|
|
|
|
|
return r.Listen(":"+port, fiber.ListenConfig{
|
|
|
|
|
EnablePrintRoutes: true,
|
|
|
|
|
GracefulContext: _ctx,
|
|
|
|
|
})
|
|
|
|
|
})
|
2024-05-14 15:27:40 +08:00
|
|
|
|
|
2024-09-13 22:47:19 +08:00
|
|
|
|
if err = wg.Wait(); err != nil {
|
|
|
|
|
glog.Fatalf("服务异常退出:%v", err)
|
2024-05-14 21:18:24 +08:00
|
|
|
|
}
|
2024-05-14 15:27:40 +08:00
|
|
|
|
}
|