feat: v2.0.0 完整代码优化升级
🔴 高优先级 (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
This commit is contained in:
54
main.go
54
main.go
@@ -15,9 +15,13 @@ import (
|
||||
"sms-receiver-go/handlers"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 记录启动时间
|
||||
startTime := time.Now()
|
||||
|
||||
// 命令行参数
|
||||
configPath := flag.String("config", "config.yaml", "配置文件路径")
|
||||
templatesPath := flag.String("templates", "templates", "模板目录路径")
|
||||
@@ -37,7 +41,9 @@ func main() {
|
||||
defer database.Close()
|
||||
|
||||
// 初始化会话存储
|
||||
auth.Init(cfg.Security.SecretKey)
|
||||
if err := auth.Init(cfg.Security.SecretKey); err != nil {
|
||||
log.Fatalf("初始化会话存储失败: %v", err)
|
||||
}
|
||||
|
||||
// 初始化模板
|
||||
if err := handlers.InitTemplates(*templatesPath); err != nil {
|
||||
@@ -58,15 +64,19 @@ func main() {
|
||||
r.HandleFunc("/logs", handlers.Logs)
|
||||
r.HandleFunc("/statistics", handlers.Statistics)
|
||||
|
||||
// API 路由
|
||||
// API 路由(v1)
|
||||
apiV1 := r.PathPrefix("/api/v1").Subrouter()
|
||||
apiV1.HandleFunc("/receive", handlers.ReceiveSMS)
|
||||
apiV1.HandleFunc("/messages", handlers.APIGetMessages)
|
||||
apiV1.HandleFunc("/statistics", handlers.APIStatistics)
|
||||
|
||||
// 兼容旧版 API(无版本号)
|
||||
r.HandleFunc("/api/receive", handlers.ReceiveSMS)
|
||||
r.HandleFunc("/api/messages", handlers.APIGetMessages)
|
||||
r.HandleFunc("/api/statistics", handlers.APIStatistics)
|
||||
|
||||
// 健康检查
|
||||
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("OK"))
|
||||
})
|
||||
r.HandleFunc("/health", handlers.HealthCheck(startTime))
|
||||
|
||||
// 配置服务器
|
||||
server := &http.Server{
|
||||
@@ -78,7 +88,8 @@ func main() {
|
||||
}
|
||||
|
||||
// 启动后台清理任务
|
||||
go startCleanupTask(cfg)
|
||||
cronInstance := startCleanupTask(cfg)
|
||||
defer cronInstance.Stop()
|
||||
|
||||
// 优雅关闭
|
||||
go func() {
|
||||
@@ -97,21 +108,34 @@ func main() {
|
||||
}
|
||||
|
||||
// startCleanupTask 启动定期清理任务
|
||||
func startCleanupTask(cfg *config.Config) {
|
||||
func startCleanupTask(cfg *config.Config) *cron.Cron {
|
||||
if !cfg.SMS.AutoCleanup {
|
||||
return
|
||||
log.Println("自动清理功能未启用")
|
||||
return cron.New(cron.WithSeconds())
|
||||
}
|
||||
|
||||
// 每天凌晨 3 点执行清理
|
||||
for {
|
||||
now := time.Now()
|
||||
next := time.Date(now.Year(), now.Month(), now.Day()+1, 3, 0, 0, 0, now.Location())
|
||||
time.Sleep(next.Sub(now))
|
||||
// 创建 cron 实例
|
||||
c := cron.New(cron.WithSeconds())
|
||||
|
||||
if _, err := database.CleanupOldMessages(cfg.SMS.CleanupDays); err != nil {
|
||||
// 每天凌晨 3 点执行清理任务
|
||||
_, err := c.AddFunc("0 0 3 * * *", func() {
|
||||
log.Println("开始执行自动清理任务...")
|
||||
deleted, err := database.CleanupOldMessages(cfg.SMS.CleanupDays)
|
||||
if err != nil {
|
||||
log.Printf("清理旧消息失败: %v", err)
|
||||
} else {
|
||||
log.Println("自动清理旧消息完成")
|
||||
log.Printf("自动清理旧消息完成: 删除 %d 条记录", deleted)
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Printf("添加清理任务失败: %v", err)
|
||||
return c
|
||||
}
|
||||
|
||||
// 启动 cron 服务
|
||||
c.Start()
|
||||
log.Println("自动清理任务已启动: 每天 03:00 执行")
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user