Files
tyapi-server/cmd/api/main.go
2025-07-11 21:05:58 +08:00

137 lines
3.5 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"
"time"
_ "tyapi-server/docs" // docs is generated by Swag CLI, you have to import it.
"tyapi-server/internal/app"
)
// @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
// @BasePath /api/v1
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and JWT token.
// 构建时注入的变量
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
// 设置时区为北京时间
time.Local = time.FixedZone("CST", 8*3600)
// 命令行参数
var (
showVersion = flag.Bool("version", false, "显示版本信息")
migrate = flag.Bool("migrate", false, "运行数据库迁移")
health = flag.Bool("health", false, "执行健康检查")
env = flag.String("env", "", "指定运行环境 (development|production|testing)")
)
flag.Parse()
// 处理版本信息显示 (不需要初始化完整应用)
if *showVersion {
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)
}
// 处理命令行参数
if *migrate {
fmt.Println("Running database migrations...")
if err := application.RunCommand("migrate"); err != nil {
log.Fatalf("Migration failed: %v", err)
}
fmt.Println("Database migrations completed successfully")
os.Exit(0)
}
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")
os.Exit(0)
}
// 启动应用程序 (使用完整的架构)
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)
}
}
// 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)
}
// 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
}
return "development"
}