Files
erp/wiki/erp-server.md
iven 5901ee82f0 feat: complete Phase 1 infrastructure
- erp-core: error types, shared types, event bus, ErpModule trait
- erp-server: config loading, database/Redis connections, migrations
- erp-server/migration: tenants table with SeaORM
- apps/web: Vite + React 18 + TypeScript + Ant Design 5 + TailwindCSS
- Web frontend: main layout with sidebar, header, routing
- Docker: PostgreSQL 16 + Redis 7 development environment
- All workspace crates compile successfully (cargo check passes)
2026-04-11 01:07:31 +08:00

2.6 KiB
Raw Blame History

erp-server

设计思想

erp-server 是 L3 层——唯一的组装点。它不包含业务逻辑,只负责把所有业务模块组装成可运行的服务。

核心决策:

  • 配置优先 — 使用 config crate 从 TOML 文件 + 环境变量加载,ERP__ 前缀覆盖(如 ERP__DATABASE__URL
  • 启动序列严格有序 — 配置 → 日志 → 数据库 → 迁移 → Redis → 路由 → 监听,每步失败即终止
  • 单一入口 — 所有模块通过 ModuleRegistry 注册server 本身不直接 import 业务模块的类型

代码逻辑

启动流程 (main.rs)

1. AppConfig::load()           ← config/default.toml + 环境变量
2. init_tracing(level)         ← JSON 格式日志
3. db::connect(&db_config)     ← SeaORM 连接池 (max=20, min=5)
4. Migrator::up(&db_conn)      ← 运行所有待执行迁移
5. redis::Client::open(url)    ← Redis 客户端(当前未使用)
6. Router::new()               ← 当前仅有 404 fallback
7. bind(host, port).serve()    ← 启动 HTTP 服务

配置结构

AppConfig
├── server: ServerConfig     { host: "0.0.0.0", port: 3000 }
├── database: DatabaseConfig { url, max_connections: 20, min_connections: 5 }
├── redis: RedisConfig       { url: "redis://localhost:6379" }
├── jwt: JwtConfig           { secret, access_token_ttl, refresh_token_ttl }
└── log: LogConfig           { level: "info" }

当前状态

  • 数据库连接池正常工作
  • 迁移自动执行
  • 没有注册任何路由 — 仅返回 404
  • 没有使用 ModuleRegistry — 未集成业务模块
  • Redis 客户端已创建但未执行任何命令
  • 缺少 CORS、压缩、请求追踪中间件

关联模块

  • erp-core — 提供 AppError、ErpModule trait、ModuleRegistry
  • database — 迁移文件通过 erp-server-migration crate 引用
  • infrastructure — Docker 提供 PostgreSQL 和 Redis 服务
  • frontend — Vite 代理 /api 请求到 server 的 3000 端口

关键文件

文件 职责
crates/erp-server/src/main.rs 服务启动入口
crates/erp-server/src/config.rs 5 个配置 struct + 加载逻辑
crates/erp-server/src/db.rs SeaORM 连接池配置
crates/erp-server/config/default.toml 默认配置值
crates/erp-server/Cargo.toml 依赖声明

待完成 (Phase 1 剩余)

  1. 实例化 ModuleRegistry 并注册模块
  2. 添加 CORS 中间件tower-http
  3. 添加请求追踪中间件
  4. 将 Redis 连接注入 AppState
  5. 健康检查端点 (/api/v1/health)