us-coach-spider/cmd/main.go

99 lines
2.3 KiB
Go
Raw Normal View History

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-05-14 15:27:40 +08:00
"gitea.timerzz.com/kedaya_haitao/coach-spider/pkg/options"
"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-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"
"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
// 代理池
pool := proxy.NewProxyPool(cfg.Proxy.Subscribes)
go pool.CronUpdate(ctx, cfg.Proxy.Interval) //定时更新代理
// coach client
2024-06-15 21:10:37 +08:00
client := 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-09-13 22:47:19 +08:00
ctl := spider.NewController(
_ctx,
cfg.ProviderId,
client,
db,
rdb,
)
wg.Go(ctl.Run)
2024-09-13 22:47:19 +08:00
r := fiber.New(fiber.Config{ErrorHandler: web.ErrHandle})
2024-09-13 22:47:19 +08:00
r.Use(cors.New(), recover.New())
2024-09-13 22:47:19 +08:00
for _, register := range []web.Register{
server.NewSpiderSvc(ctl, cfg.ProviderId),
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-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 15:27:40 +08:00
}