🔴 高优先级 (6项全部完成): - 数据库事务支持 (InsertMessageWithLog) - SQL注入修复 (参数化查询) - 配置验证 (Validate方法) - 会话密钥强化 (长度验证) - 签名验证增强 (SignVerificationResult) - 密码哈希支持 (bcrypt) 🟡 中优先级 (15项全部完成): - 连接池配置 (MaxOpenConns, MaxIdleConns) - 查询优化 (范围查询, 索引) - 健康检查增强 (/health 端点) - API版本控制 (/api/v1/*) - 认证中间件 (RequireAuth, RequireAPIAuth) - 定时任务优化 (robfig/cron) - 配置文件示例 (config.example.yaml) - 常量定义 (config/constants.go) - 开发文档 (DEVELOPMENT.md) 🟢 低优先级 (9项全部完成): - Docker支持 (Dockerfile, docker-compose.yml) - Makefile构建脚本 - 优化报告 (OPTIMIZATION_REPORT.md) - 密码哈希工具 (tools/password_hash.go) - 14个新文件 - 30项优化100%完成 版本: v2.0.0
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package handlers
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"time"
|
||
|
||
"sms-receiver-go/config"
|
||
"sms-receiver-go/database"
|
||
)
|
||
|
||
// HealthCheck 健康检查端点
|
||
func HealthCheck(startTime time.Time) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
cfg := config.Get()
|
||
db := database.GetDB()
|
||
|
||
// 检查数据库连接
|
||
dbStatus := "ok"
|
||
if db == nil {
|
||
dbStatus = "disconnected"
|
||
} else if err := db.Ping(); err != nil {
|
||
dbStatus = "error: " + err.Error()
|
||
}
|
||
|
||
// 获取基本统计
|
||
var totalMessages int64
|
||
if dbStatus == "ok" {
|
||
db.QueryRow("SELECT COUNT(*) FROM sms_messages").Scan(&totalMessages)
|
||
}
|
||
|
||
response := map[string]interface{}{
|
||
"status": "ok",
|
||
"app_name": cfg.App.Name,
|
||
"version": cfg.App.Version,
|
||
"database": dbStatus,
|
||
"total_messages": totalMessages,
|
||
"uptime": time.Since(startTime).String(),
|
||
}
|
||
|
||
// 如果数据库有问题,返回503
|
||
statusCode := http.StatusOK
|
||
if dbStatus != "ok" {
|
||
response["status"] = "degraded"
|
||
statusCode = http.StatusServiceUnavailable
|
||
}
|
||
|
||
w.Header().Set("Content-Type", "application/json")
|
||
w.WriteHeader(statusCode)
|
||
json.NewEncoder(w).Encode(response)
|
||
}
|
||
}
|