watcher/main/main.go

57 lines
1.4 KiB
Go
Raw Normal View History

2024-04-10 17:36:56 +08:00
package main
import (
"context"
2024-05-13 20:18:13 +08:00
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
2024-05-21 21:04:44 +08:00
"gitea.timerzz.com/kedaya_haitao/pusher/rpc/pusher"
"github.com/cloudwego/kitex/client"
2024-04-10 17:36:56 +08:00
"github.com/gofiber/fiber/v3"
2024-05-12 16:14:24 +08:00
"github.com/gofiber/fiber/v3/middleware/cors"
2024-04-10 17:36:56 +08:00
"github.com/golang/glog"
"haitao_watcher/pkg/options"
"haitao_watcher/pkg/watcher"
"haitao_watcher/server"
"log/slog"
"os"
"os/signal"
)
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-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-04-10 17:36:56 +08:00
glog.Warningf("server over: %v", err)
}
}