chore: 注入构建时版本信息到 /health 端点
现状:controller/tts.go:232 硬编码 Version: "2.0.0 (v3 API)",
不会随代码变化而更新,/health 无法反映实际跑的代码。
修复:四文件改动,实现构建时 ldflags 注入。
1. 新建 version/version.go,声明两个包级变量:
Version (默认 "dev")
Commit (默认 "dev")
2. dto/health.go:HealthResponse 加 Commit 字段(JSON 输出多一字段)
3. controller/tts.go:HealthResponse 用 version.Version / version.Commit
替代硬编码字面量
4. Dockerfile:加 ARG VERSION=dev ARG COMMIT=dev,
go build 时通过 -ldflags 注入到 version 包的两个变量
效果:
- 本地 go build (不传 ldflags) → version=dev commit=dev
- 开发 build (git describe) → version=v0.2.0-5-g4abcd5 commit=g4abcd5
- release build (打 tag 后) → version=v0.2.1 commit=<对应 hash>
测试环境(develop 分支,无 tag)显示距离上次 release 几个 commit +
具体 hash;生产环境(main + tag)显示干净 semver。
This commit is contained in:
@@ -7,6 +7,7 @@ type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
Service string `json:"service"`
|
||||
Version string `json:"version"`
|
||||
Commit string `json:"commit"`
|
||||
Uptime string `json:"uptime"`
|
||||
StartTime string `json:"start_time"`
|
||||
Memory map[string]interface{} `json:"memory"`
|
||||
|
||||
Reference in New Issue
Block a user