us-coach-spider/cmd/main.go

75 lines
1.8 KiB
Go
Raw Normal View History

2024-05-14 15:27:40 +08:00
package main
import (
"context"
"gitea.timerzz.com/kedaya_haitao/coach-spider/pkg/options"
"gitea.timerzz.com/kedaya_haitao/coach-spider/product"
"gitea.timerzz.com/kedaya_haitao/coach-spider/server"
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"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/cors"
2024-05-14 15:27:40 +08:00
"github.com/golang/glog"
2024-05-15 20:41:42 +08:00
"gorm.io/gorm/logger"
"log"
2024-05-14 15:27:40 +08:00
"log/slog"
"os"
"os/signal"
2024-05-15 20:41:42 +08:00
"time"
2024-05-14 15:27:40 +08:00
)
func main() {
slog.Info(">>> BEGIN INIT<<<")
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
defer cancel()
cfg, err := options.LoadConfig()
if err != nil {
glog.Fatalf("获取配置失败:%v", err)
}
db, err := database.InitDatabase(&cfg.DB)
if err != nil {
glog.Fatalf("初始化数据库失败:%v", err)
}
2024-05-15 20:41:42 +08:00
db.Logger = logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Warn,
IgnoreRecordNotFoundError: false,
Colorful: true,
},
)
2024-05-14 15:27:40 +08:00
// 代理池
pool := proxy.NewProxyPool(cfg.Proxy.Subscribes)
go pool.CronUpdate(ctx, cfg.Proxy.Interval) //定时更新代理
// coach client
client := coach_client.NewClient(pool)
2024-05-14 16:17:04 +08:00
productCtl := product.NewController(client, db)
2024-05-14 15:27:40 +08:00
go productCtl.Run(ctx)
r := fiber.New()
r.Use(cors.New())
r.Get("/health", func(ctx fiber.Ctx) error {
return ctx.SendString("ok")
})
api := r.Group("/api/v1")
server.NewSpiderSvc(productCtl).RegistryRouter(api)
2024-05-14 15:27:40 +08:00
//
//server.NewWatcherController(watcherCtl).RegistryRouter(api)
//server.NewPusherSvcController(pusherCtl).RegistryRouter(api)
//server.NewProxySvc(pool).RegistryRouter(api)
if err = r.Listen(":8080"); err != nil {
glog.Warningf("server over: %v", err)
}
2024-05-14 15:27:40 +08:00
}