2024-11-30 20:26:39 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/watcher/service"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
|
|
"github.com/gofiber/fiber/v3/middleware/cors"
|
|
|
|
"github.com/gofiber/fiber/v3/middleware/recover"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
glog.Info(">>> BEGIN INIT<<<")
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
// 初始化数据库
|
|
|
|
db, err := database.InitDefaultDatabase()
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("init database failed: %v", err)
|
|
|
|
}
|
|
|
|
// 初始化服务
|
|
|
|
r := fiber.New(fiber.Config{ErrorHandler: web.ErrHandle})
|
|
|
|
r.Use(cors.New(), recover.New())
|
|
|
|
|
|
|
|
stg := storage.NewStorage(db)
|
|
|
|
_ = stg.Article().AutoMigrate()
|
|
|
|
|
|
|
|
svc := []web.Register{
|
|
|
|
web.NewProbe(),
|
|
|
|
service.NewWatcher(stg),
|
2024-12-02 19:07:39 +08:00
|
|
|
service.NewTracer(stg),
|
2024-11-30 20:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range svc {
|
|
|
|
s.Registry(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "80"
|
|
|
|
}
|
|
|
|
if err = r.Listen(":"+port, fiber.ListenConfig{
|
|
|
|
EnablePrintRoutes: true,
|
|
|
|
GracefulContext: ctx,
|
|
|
|
}); err != nil {
|
|
|
|
glog.Warningf("service over: %v", err)
|
|
|
|
}
|
|
|
|
}
|