Go-ES 项目采用领域驱动设计(DDD)架构,目录结构清晰,各层次职责分明。以下是项目的主要目录结构:
go-es/
├── cmd/ # 应用程序入口
│ └── api/ # API 服务入口
├── config/ # 配置
├── domain/ # 领域层
│ ├── entity/ # 实体
│ ├── repository/ # 仓储接口
│ └── service/ # 领域服务
├── infrastructure/ # 基础设施层
│ ├── persistence/ # 持久化
│ ├── elasticsearch/ # ES 操作
│ └── repository/ # 仓储实现
├── interfaces/ # 接口层
│ ├── api/ # API
│ │ ├── handler/ # 处理器
│ │ ├── middleware/ # 中间件
│ │ └── router/ # 路由
│ └── dto/ # 数据传输对象
├── application/ # 应用层
│ └── service/ # 应用服务
├── docs/ # 文档
└── pkg/ # 公共包
└── utils/ # 工具函数
领域层是整个应用的核心,包含业务逻辑和规则,与技术实现细节无关。
domain/entity/
目录包含核心业务实体,如 Product
:
domain/repository/
目录定义了仓储接口,为领域层提供数据访问抽象:
ProductRepository
接口定义了产品数据的存取操作domain/service/
目录包含领域服务,处理不适合放在单个实体中的业务逻辑:
ProductService
实现了产品相关的核心业务逻辑应用层负责协调领域对象完成用户用例,是领域层与接口层之间的桥梁。
application/service/
目录包含应用服务:
ProductAppService
处理产品相关的用户场景接口层负责处理来自外部的请求,包括 API 接口、请求参数解析等。
interfaces/api/handler/
目录包含 API 处理器:
ProductHandler
处理产品相关的 HTTP 请求interfaces/dto/
目录包含数据传输对象:
interfaces/api/router/
目录设置 API 路由:
interfaces/api/middleware/
目录包含 HTTP 中间件:
基础设施层提供技术实现,支持上层业务逻辑的运行。
infrastructure/persistence/
目录处理数据库连接:
infrastructure/elasticsearch/
目录封装 Elasticsearch 操作:
EsClient
提供基础的 ES 操作ProductIndexer
实现产品索引管理infrastructure/repository/
目录实现领域层定义的仓储接口:
ProductRepositoryImpl
实现了产品仓储接口cmd/api/main.go
是应用程序的入口点:
config/
目录处理应用配置:
以下是核心组件的关系图,展示了各部分如何协同工作:
│
├── HTTP请求 ────────────┐
│ ↓
├── Router ────────► Handler ────────┐
│ │ │
├── Middleware ◄────────┘ │
│ ↓
├── DTO ◄───────► Application Service ◄───┐
│ │ │
│ ↓ │
├── Entity ◄────── Domain Service │
│ │ │
│ ↓ │
├── Repository Interface │
│ ↑ │
│ │ │
├── Repository Implementation ────────────┘
│ │
│ ↓
├── Database / Elasticsearch
│
这种分层架构使得代码更易于理解、测试和维护,同时也便于团队协作开发。