us-coach-spider/cmd/main.go
timerzz f39d94c5a3
All checks were successful
Build image / build (push) Successful in 2m32s
feat v2
2024-09-13 22:47:19 +08:00

99 lines
2.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"context"
"flag"
"os"
"os/signal"
"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/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"
"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)
}
// 代理池
pool := proxy.NewProxyPool(cfg.Proxy.Subscribes)
go pool.CronUpdate(ctx, cfg.Proxy.Interval) //定时更新代理
// coach client
client := coach_client.USClient(pool)
var wg, _ctx = errgroup.WithContext(ctx)
ctl := spider.NewController(
_ctx,
cfg.ProviderId,
client,
db,
rdb,
)
wg.Go(ctl.Run)
r := fiber.New(fiber.Config{ErrorHandler: web.ErrHandle})
r.Use(cors.New(), recover.New())
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"
}
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)
}
}