refactor: 重构后端架构,采用 Go 风格四层设计模式
Some checks failed
部署后端服务 / 🧪 测试后端 (push) Failing after 1m37s
部署后端服务 / 🚀 构建并部署 (push) Has been skipped
部署后端服务 / 🔄 回滚部署 (push) Has been skipped

## 主要变更

### 🏗️ 架构重构
- 采用简洁的四层架构:API → Service → Repository → Model
- 遵循 Go 语言最佳实践和命名规范
- 实现依赖注入和接口导向设计
- 统一错误处理和响应格式

### 📁 目录结构优化
- 删除重复模块 (application/, domain/, infrastructure/ 等)
- 规范化命名 (使用 Go 风格的 snake_case)
- 清理无关文件 (package.json, node_modules/ 等)
- 新增规范化的测试目录结构

### 📚 文档系统
- 为每个模块创建详细的 CLAUDE.md 指导文件
- 包含开发规范、最佳实践和使用示例
- 支持模块化开发,缩短上下文长度

### 🔧 开发规范
- 统一接口命名规范 (UserServicer, PhotoRepositoryr)
- 标准化错误处理机制
- 完善的测试策略 (单元测试、集成测试、性能测试)
- 规范化的配置管理

### 🗂️ 新增文件
- cmd/server/ - 服务启动入口和配置
- internal/model/ - 数据模型层 (entity, dto, request)
- pkg/ - 共享工具包 (logger, response, validator)
- tests/ - 完整测试结构
- docs/ - API 文档和架构设计
- .gitignore - Git 忽略文件配置

### 🗑️ 清理内容
- 删除 Node.js 相关文件 (package.json, node_modules/)
- 移除重复的架构目录
- 清理临时文件和构建产物
- 删除重复的文档文件

## 影响
- 提高代码可维护性和可扩展性
- 统一开发规范,提升团队协作效率
- 优化项目结构,符合 Go 语言生态标准
- 完善文档体系,降低上手难度
This commit is contained in:
xujiang
2025-07-10 11:20:59 +08:00
parent 540593f1dc
commit a2f2f66f88
40 changed files with 9682 additions and 1798 deletions

View File

@ -0,0 +1,186 @@
package main
import (
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/cors"
)
func main() {
// 创建 Gin 引擎
r := gin.Default()
// 配置 CORS
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:3000", "http://localhost:3002", "http://localhost:3003"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
// 健康检查
r.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "ok",
"timestamp": time.Now().Unix(),
"version": "1.0.0",
})
})
// 模拟登录接口
r.POST("/api/v1/auth/login", func(c *gin.Context) {
var req struct {
Username string `json:"username"`
Password string `json:"password"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 简单验证
if req.Username == "admin" && req.Password == "admin123" {
c.JSON(http.StatusOK, gin.H{
"user": gin.H{
"id": "1",
"username": "admin",
"email": "admin@example.com",
"role": "admin",
"isActive": true,
},
"access_token": "mock-jwt-token",
"expires_in": 86400,
})
} else {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
}
})
// 模拟照片列表接口
r.GET("/api/v1/photos", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"data": []gin.H{
{
"id": "1",
"title": "Sample Photo 1",
"description": "This is a sample photo",
"url": "https://picsum.photos/800/600?random=1",
"status": "published",
"createdAt": time.Now().Format(time.RFC3339),
},
{
"id": "2",
"title": "Sample Photo 2",
"description": "This is another sample photo",
"url": "https://picsum.photos/800/600?random=2",
"status": "published",
"createdAt": time.Now().Format(time.RFC3339),
},
},
"total": 2,
"page": 1,
"limit": 10,
"totalPages": 1,
})
})
// 模拟仪表板统计接口
r.GET("/api/v1/dashboard/stats", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"photos": gin.H{
"total": 156,
"thisMonth": 23,
"today": 5,
},
"categories": gin.H{
"total": 12,
"active": 10,
},
"tags": gin.H{
"total": 45,
},
"users": gin.H{
"total": 8,
"active": 7,
},
})
})
// 模拟分类列表接口
r.GET("/api/v1/categories", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"data": []gin.H{
{
"id": "1",
"name": "风景",
"slug": "landscape",
"description": "自然风景摄影",
"photoCount": 25,
"isActive": true,
},
{
"id": "2",
"name": "人像",
"slug": "portrait",
"description": "人物肖像摄影",
"photoCount": 18,
"isActive": true,
},
},
"total": 2,
})
})
// 模拟标签列表接口
r.GET("/api/v1/tags", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"data": []gin.H{
{
"id": "1",
"name": "日落",
"slug": "sunset",
"photoCount": 12,
},
{
"id": "2",
"name": "城市",
"slug": "city",
"photoCount": 8,
},
},
"total": 2,
})
})
// 模拟用户列表接口
r.GET("/api/v1/users", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"data": []gin.H{
{
"id": "1",
"username": "admin",
"email": "admin@example.com",
"role": "admin",
"isActive": true,
},
{
"id": "2",
"username": "editor",
"email": "editor@example.com",
"role": "editor",
"isActive": true,
},
},
"total": 2,
})
})
// 启动服务器
log.Println("🚀 Simple backend server starting on :8080")
log.Fatal(r.Run(":8080"))
}