watcher/main/main.go
timerzz ba47d0a32a
Some checks failed
Build image / build (push) Failing after 20s
feat 抽出公共方法到common
2024-05-13 20:18:13 +08:00

61 lines
1.4 KiB
Go

package main
import (
"context"
"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"
"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)
}
db, err := database.InitDatabase(&cfg.DB)
if err != nil {
glog.Fatalf("初始化数据库失败:%v", err)
}
pool := proxy.NewProxyPool(cfg.Proxy.Subscribes)
go pool.CronUpdate(_ctx, cfg.Proxy.Interval) //定时更新代理
pusherCtl := pusher.NewController(_ctx, db)
ch := make(chan model.PushMsg, 30)
pusherCtl.Consume(ch)
watcherCtl := watcher.NewController(_ctx, db, pool, ch)
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.NewPusherSvcController(pusherCtl).RegistryRouter(api)
server.NewProxySvc(pool).RegistryRouter(api)
if err = r.Listen(":8080"); err != nil {
glog.Warningf("server over: %v", err)
}
}