watcher/main/main.go

66 lines
1.6 KiB
Go

package main
import (
"context"
"haitao_watcher/pkg/options"
"haitao_watcher/pkg/watcher"
"haitao_watcher/server"
"log"
"log/slog"
"os"
"os/signal"
"time"
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
"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/golang/glog"
"gorm.io/gorm/logger"
)
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)
}
db.Logger = logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Warn,
IgnoreRecordNotFoundError: false,
Colorful: true,
})
pool := proxy.NewProxyPool(cfg.Proxy.Subscribes)
go pool.CronUpdate(ctx, cfg.Proxy.Interval) //定时更新代理
watcherCtl := watcher.NewController(ctx, db, pool)
pusher.InitClient("pusher", client.WithHostPorts("pusher:8080"))
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.NewWatcherController(watcherCtl).RegistryRouter(api)
server.NewProxySvc(pool).RegistryRouter(api)
if err = r.Listen(":8080"); err != nil {
glog.Warningf("service over: %v", err)
}
}