Files
tyapi-server/cmd/api/main.go

136 lines
3.4 KiB
Go
Raw Permalink Normal View History

package main
import (
"flag"
"fmt"
"log"
"os"
2025-07-11 21:05:58 +08:00
"time"
"tyapi-server/internal/app"
)
2025-07-02 16:17:59 +08:00
// @title TYAPI Server API
// @version 1.0
// @description 基于DDD和Clean Architecture的企业级后端API服务
// @description 采用Gin框架构建支持用户管理、JWT认证、事件驱动等功能
// @contact.name API Support
// @contact.url https://github.com/your-org/tyapi-server-gin
// @contact.email support@example.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
2025-07-13 16:36:20 +08:00
// @BasePath /
2025-07-02 16:17:59 +08:00
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and JWT token.
// 构建时注入的变量
var (
2025-07-02 16:17:59 +08:00
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
2025-07-11 21:05:58 +08:00
// 设置时区为北京时间
time.Local = time.FixedZone("CST", 8*3600)
2025-07-02 16:17:59 +08:00
// 命令行参数
var (
showVersion = flag.Bool("version", false, "显示版本信息")
2025-07-02 16:17:59 +08:00
migrate = flag.Bool("migrate", false, "运行数据库迁移")
health = flag.Bool("health", false, "执行健康检查")
env = flag.String("env", "", "指定运行环境 (development|production|testing)")
)
flag.Parse()
2025-07-02 16:17:59 +08:00
// 处理版本信息显示 (不需要初始化完整应用)
if *showVersion {
2025-07-02 16:17:59 +08:00
fmt.Printf("TYAPI Server\n")
fmt.Printf("Version: %s\n", version)
fmt.Printf("Commit: %s\n", commit)
fmt.Printf("Build Date: %s\n", date)
os.Exit(0)
}
// 设置环境变量(如果通过命令行指定)
if *env != "" {
if err := validateEnvironment(*env); err != nil {
log.Fatalf("无效的环境参数: %v", err)
}
os.Setenv("ENV", *env)
fmt.Printf("🌍 通过命令行设置环境: %s\n", *env)
}
// 显示当前环境
currentEnv := getCurrentEnvironment()
fmt.Printf("🔧 当前运行环境: %s\n", currentEnv)
// 生产环境安全提示
if currentEnv == "production" {
fmt.Printf("⚠️ 生产环境模式 - 请确保配置正确\n")
}
// 创建应用程序实例
application, err := app.NewApplication()
if err != nil {
log.Fatalf("Failed to create application: %v", err)
}
2025-07-02 16:17:59 +08:00
// 处理命令行参数
if *migrate {
fmt.Println("Running database migrations...")
if err := application.RunCommand("migrate"); err != nil {
log.Fatalf("Migration failed: %v", err)
}
2025-07-02 16:17:59 +08:00
fmt.Println("Database migrations completed successfully")
os.Exit(0)
}
2025-07-02 16:17:59 +08:00
if *health {
fmt.Println("Performing health check...")
if err := application.RunCommand("health"); err != nil {
log.Fatalf("Health check failed: %v", err)
}
fmt.Println("Health check passed")
2025-07-02 16:17:59 +08:00
os.Exit(0)
}
2025-07-02 16:17:59 +08:00
// 启动应用程序 (使用完整的架构)
fmt.Printf("🚀 Starting TYAPI Server v%s (%s)\n", version, commit)
if err := application.Run(); err != nil {
log.Fatalf("Application failed to start: %v", err)
}
}
2025-07-02 16:17:59 +08:00
// validateEnvironment 验证环境参数
func validateEnvironment(env string) error {
validEnvs := []string{"development", "production", "testing"}
for _, validEnv := range validEnvs {
if env == validEnv {
return nil
}
}
return fmt.Errorf("环境必须是以下之一: %v", validEnvs)
}
2025-07-02 16:17:59 +08:00
// getCurrentEnvironment 获取当前环境与config包中的逻辑保持一致
func getCurrentEnvironment() string {
if env := os.Getenv("CONFIG_ENV"); env != "" {
return env
}
if env := os.Getenv("ENV"); env != "" {
return env
}
if env := os.Getenv("APP_ENV"); env != "" {
return env
}
2025-07-02 16:17:59 +08:00
return "development"
}