2024-08-27 16:56:53 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-01 19:13:56 +08:00
|
|
|
"context"
|
2024-09-13 22:08:09 +08:00
|
|
|
"flag"
|
2024-08-27 16:56:53 +08:00
|
|
|
"os"
|
2024-09-01 19:13:56 +08:00
|
|
|
"os/signal"
|
2024-08-27 16:56:53 +08:00
|
|
|
|
|
|
|
"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/seller/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() {
|
2024-09-13 22:08:09 +08:00
|
|
|
flag.Parse()
|
2024-08-27 16:56:53 +08:00
|
|
|
glog.Info(">>> BEGIN INIT<<<")
|
2024-09-01 19:13:56 +08:00
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
|
|
|
|
defer cancel()
|
2024-08-27 16:56:53 +08:00
|
|
|
// 初始化数据库
|
|
|
|
db, err := database.InitDefaultDatabase()
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("init database failed: %v", err)
|
|
|
|
}
|
|
|
|
// 初始化服务
|
2024-09-01 13:25:36 +08:00
|
|
|
r := fiber.New(fiber.Config{ErrorHandler: web.ErrHandle})
|
2024-08-27 16:56:53 +08:00
|
|
|
r.Use(cors.New(), recover.New())
|
|
|
|
|
|
|
|
stg := storage.NewStorage(db)
|
|
|
|
if err = stg.Seller().AutoMigrate(); err != nil {
|
|
|
|
glog.Fatalf("autoMigrate database failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
svc := []web.Register{
|
|
|
|
web.NewProbe(),
|
|
|
|
service.NewSeller(stg),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range svc {
|
|
|
|
s.Registry(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "80"
|
|
|
|
}
|
2024-09-01 19:13:56 +08:00
|
|
|
if err = r.Listen(":"+port, fiber.ListenConfig{
|
|
|
|
EnablePrintRoutes: true,
|
|
|
|
GracefulContext: ctx,
|
|
|
|
}); err != nil {
|
2024-08-27 16:56:53 +08:00
|
|
|
glog.Warningf("service over: %v", err)
|
|
|
|
}
|
|
|
|
}
|