Initial commit: Basic project structure and dependencies
This commit is contained in:
110
cmd/api/main.go
Normal file
110
cmd/api/main.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"tyapi-server/internal/app"
|
||||
)
|
||||
|
||||
var (
|
||||
// 版本信息
|
||||
version = "1.0.0"
|
||||
commit = "unknown"
|
||||
date = "unknown"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 解析命令行参数
|
||||
var (
|
||||
showVersion = flag.Bool("version", false, "显示版本信息")
|
||||
runMigrate = flag.Bool("migrate", false, "运行数据库迁移")
|
||||
healthCheck = flag.Bool("health", false, "执行健康检查")
|
||||
command = flag.String("cmd", "", "执行特定命令 (version|migrate|health)")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
// 显示版本信息
|
||||
if *showVersion {
|
||||
printVersion()
|
||||
return
|
||||
}
|
||||
|
||||
// 创建应用程序实例
|
||||
application, err := app.NewApplication()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create application: %v", err)
|
||||
}
|
||||
|
||||
// 处理命令行命令
|
||||
if *command != "" {
|
||||
if err := application.RunCommand(*command); err != nil {
|
||||
log.Fatalf("Command '%s' failed: %v", *command, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 运行数据库迁移
|
||||
if *runMigrate {
|
||||
if err := application.RunMigrations(); err != nil {
|
||||
log.Fatalf("Migration failed: %v", err)
|
||||
}
|
||||
fmt.Println("Migration completed successfully")
|
||||
return
|
||||
}
|
||||
|
||||
// 执行健康检查
|
||||
if *healthCheck {
|
||||
if err := application.HealthCheck(); err != nil {
|
||||
log.Fatalf("Health check failed: %v", err)
|
||||
}
|
||||
fmt.Println("Health check passed")
|
||||
return
|
||||
}
|
||||
|
||||
// 默认:启动应用程序服务器
|
||||
logger := application.GetLogger()
|
||||
logger.Info("Starting TYAPI Server...")
|
||||
|
||||
if err := application.Run(); err != nil {
|
||||
log.Fatalf("Application failed to start: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// printVersion 打印版本信息
|
||||
func printVersion() {
|
||||
fmt.Printf("TYAPI Server\n")
|
||||
fmt.Printf("Version: %s\n", version)
|
||||
fmt.Printf("Commit: %s\n", commit)
|
||||
fmt.Printf("Built: %s\n", date)
|
||||
fmt.Printf("Go Version: %s\n", getGoVersion())
|
||||
}
|
||||
|
||||
// getGoVersion 获取Go版本
|
||||
func getGoVersion() string {
|
||||
return fmt.Sprintf("%s %s/%s",
|
||||
os.Getenv("GO_VERSION"),
|
||||
os.Getenv("GOOS"),
|
||||
os.Getenv("GOARCH"))
|
||||
}
|
||||
|
||||
// 信号处理相关的辅助函数
|
||||
|
||||
// handleSignals 处理系统信号(这个函数在app包中已经实现,这里只是示例)
|
||||
func handleSignals() {
|
||||
// 信号处理逻辑已经在 app.Application 中实现
|
||||
// 这里保留作为参考
|
||||
}
|
||||
|
||||
// init 初始化函数
|
||||
func init() {
|
||||
// 设置日志格式
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
// 环境变量检查
|
||||
if os.Getenv("APP_ENV") == "" {
|
||||
os.Setenv("APP_ENV", "development")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user