duidui_fiber/main.go
2026-03-27 10:34:03 +08:00

85 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"dd_fiber_api/config"
"dd_fiber_api/internal/wire"
)
func main() {
// 解析命令行参数
configPath := flag.String("config", "config.yaml", "配置文件路径")
flag.Parse()
// 加载配置
cfg, err := config.LoadConfig(*configPath)
if err != nil {
log.Fatalf("加载配置失败: %v", err)
}
log.Printf("🚀 启动 %s v%s", cfg.Service.Name, cfg.Service.Version)
log.Println()
// 使用 Wire 初始化应用
log.Println("📦 正在初始化应用...")
app, err := wire.InitializeApp(cfg)
if err != nil {
log.Fatalf("❌ 应用初始化失败: %v", err)
}
// 确保资源关闭
defer app.Close()
log.Println()
log.Println("=========================================")
log.Println(" ✅ 应用初始化完成")
if app.MySQLClient != nil {
log.Println(" ✅ MySQL: 已连接")
} else {
log.Println(" ⚠️ MySQL: 未配置")
}
if app.MongoDBClient != nil {
log.Printf(" ✅ MongoDB: 已连接 (数据库: %s)", cfg.MongoDB.Database)
} else {
log.Println(" ⚠️ MongoDB: 未连接")
}
if app.RedisClient != nil {
log.Println(" ✅ Redis: 已连接")
} else {
log.Println(" ⚠️ Redis: 未配置或连接失败")
}
log.Println("=========================================")
log.Println()
// 启动Admin服务器
go func() {
adminAddr := fmt.Sprintf("%s:%d", cfg.Service.Host, cfg.Service.AdminPort)
log.Printf("🌐 Admin服务启动监听地址: %s", adminAddr)
if err := app.AdminApp.Listen(adminAddr); err != nil {
log.Fatalf("Admin服务启动失败: %v", err)
}
}()
// 启动API服务器
go func() {
apiAddr := fmt.Sprintf("%s:%d", cfg.Service.Host, cfg.Service.APIPort)
log.Printf("🌐 API服务启动监听地址: %s", apiAddr)
if err := app.APIApp.Listen(apiAddr); err != nil {
log.Fatalf("API服务启动失败: %v", err)
}
}()
// 等待中断信号
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("👋 正在关闭服务器...")
}