watcher/main/main.go

66 lines
1.6 KiB
Go
Raw Normal View History

2024-04-10 17:36:56 +08:00
package main
import (
"context"
"haitao_watcher/pkg/options"
"haitao_watcher/pkg/watcher"
"haitao_watcher/server"
2024-05-30 21:41:05 +08:00
"log"
2024-04-10 17:36:56 +08:00
"log/slog"
"os"
"os/signal"
2024-05-30 21:41:05 +08:00
"time"
2024-09-01 17:00:09 +08:00
"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"
2024-04-10 17:36:56 +08:00
)
func main() {
slog.Info(">>> BEGIN INIT<<<")
2024-05-21 16:42:45 +08:00
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
2024-04-10 17:36:56 +08:00
defer cancel()
cfg, err := options.LoadConfig()
if err != nil {
glog.Fatalf("获取配置失败:%v", err)
}
2024-04-10 17:36:56 +08:00
db, err := database.InitDatabase(&cfg.DB)
if err != nil {
glog.Fatalf("初始化数据库失败:%v", err)
}
2024-05-30 21:41:05 +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-13 20:18:13 +08:00
pool := proxy.NewProxyPool(cfg.Proxy.Subscribes)
2024-04-12 15:29:43 +08:00
2024-05-21 16:42:45 +08:00
go pool.CronUpdate(ctx, cfg.Proxy.Interval) //定时更新代理
2024-04-12 15:29:43 +08:00
2024-05-21 16:42:45 +08:00
watcherCtl := watcher.NewController(ctx, db, pool)
2024-04-10 17:36:56 +08:00
2024-05-21 21:04:44 +08:00
pusher.InitClient("pusher", client.WithHostPorts("pusher:8080"))
2024-04-10 17:36:56 +08:00
r := fiber.New()
2024-05-12 16:14:24 +08:00
r.Use(cors.New())
2024-04-10 17:36:56 +08:00
2024-05-12 16:44:33 +08:00
r.Get("/health", func(ctx fiber.Ctx) error {
2024-05-12 16:45:40 +08:00
return ctx.SendString("ok")
2024-05-12 16:44:33 +08:00
})
2024-04-10 17:36:56 +08:00
api := r.Group("/api/v1")
server.NewWatcherController(watcherCtl).RegistryRouter(api)
2024-04-12 15:29:43 +08:00
server.NewProxySvc(pool).RegistryRouter(api)
2024-04-10 17:36:56 +08:00
2024-05-12 16:14:24 +08:00
if err = r.Listen(":8080"); err != nil {
2024-09-01 17:00:09 +08:00
glog.Warningf("service over: %v", err)
2024-04-10 17:36:56 +08:00
}
}