watcher/main/main.go

61 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-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/model"
"haitao_watcher/pkg/options"
"haitao_watcher/pkg/pusher"
"haitao_watcher/pkg/watcher"
"haitao_watcher/server"
"log/slog"
"os"
"os/signal"
)
func main() {
slog.Info(">>> BEGIN INIT<<<")
ctx := context.Background()
_ctx, cancel := signal.NotifyContext(ctx, os.Kill, os.Interrupt)
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
go pool.CronUpdate(_ctx, cfg.Proxy.Interval) //定时更新代理
2024-04-10 17:36:56 +08:00
pusherCtl := pusher.NewController(_ctx, db)
ch := make(chan model.PushMsg, 30)
pusherCtl.Consume(ch)
watcherCtl := watcher.NewController(_ctx, db, pool, ch)
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)
server.NewPusherSvcController(pusherCtl).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)
}
}