generated from kedaya_haitao/template
74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/redis"
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
|
|
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
|
|
"gitea.timerzz.com/kedaya_haitao/profitRate/rate"
|
|
"gitea.timerzz.com/kedaya_haitao/profitRate/service"
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/gofiber/fiber/v3/middleware/cors"
|
|
"github.com/gofiber/fiber/v3/middleware/recover"
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"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)
|
|
}
|
|
// 初始化redis
|
|
rdb, err := redis.InitDefaultRedis()
|
|
if err != nil {
|
|
glog.Fatalf("init redis failed: %v", err)
|
|
}
|
|
// 初始化服务
|
|
r := fiber.New(fiber.Config{ErrorHandler: web.ErrHandle})
|
|
r.Use(cors.New(), recover.New())
|
|
|
|
stg := storage.NewStorage(db)
|
|
ctl := rate.NewController(ctx, stg, rdb)
|
|
|
|
var wg errgroup.Group
|
|
// 运行ctl
|
|
wg.Go(ctl.Run)
|
|
|
|
svc := []web.Register{
|
|
web.NewProbe(),
|
|
service.NewProfitRate(ctl),
|
|
}
|
|
|
|
for _, s := range svc {
|
|
s.Registry(r)
|
|
}
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "80"
|
|
}
|
|
|
|
// 运行service
|
|
wg.Go(func() error {
|
|
return r.Listen(":"+port, fiber.ListenConfig{
|
|
EnablePrintRoutes: true,
|
|
GracefulContext: ctx,
|
|
})
|
|
})
|
|
if err = wg.Wait(); err != nil {
|
|
glog.Warningf("over err: %v", err)
|
|
}
|
|
}
|