现状: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。
21 lines
794 B
Go
21 lines
794 B
Go
package dto
|
|
|
|
// HealthResponse 是 /health 端点的 JSON 响应。
|
|
// 数值类信息(请求统计、错误)迁移到 /metrics 端点,
|
|
// 这里只保留运行期最关键的状态。
|
|
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"`
|
|
ConfigStatus ConfigStatusResponse `json:"config_status"`
|
|
}
|
|
|
|
type ConfigStatusResponse struct {
|
|
AllRequiredVarsSet bool `json:"all_required_vars_set"`
|
|
ConfigError bool `json:"config_error"`
|
|
}
|