rwadurian/backend/services/identity-service
hailin c29c185a03 identity_service_context first commit 2025-11-24 06:09:06 +00:00
..
prisma identity_service_context first commit 2025-11-24 06:09:06 +00:00
src identity_service_context first commit 2025-11-24 06:09:06 +00:00
test identity_service_context first commit 2025-11-24 06:09:06 +00:00
.env.example identity_service_context first commit 2025-11-24 06:09:06 +00:00
.eslintrc.js identity_service_context first commit 2025-11-24 06:09:06 +00:00
.gitignore identity_service_context first commit 2025-11-24 06:09:06 +00:00
.prettierrc identity_service_context first commit 2025-11-24 06:09:06 +00:00
Dockerfile identity_service_context first commit 2025-11-24 06:09:06 +00:00
Identity_Service_Context_dir_Architecture.png first commit 2025-11-23 21:21:44 -08:00
README.md identity_service_context first commit 2025-11-24 06:09:06 +00:00
docker-compose.yml identity_service_context first commit 2025-11-24 06:09:06 +00:00
nest-cli.json identity_service_context first commit 2025-11-24 06:09:06 +00:00
package.json identity_service_context first commit 2025-11-24 06:09:06 +00:00
tsconfig.build.json identity_service_context first commit 2025-11-24 06:09:06 +00:00
tsconfig.json identity_service_context first commit 2025-11-24 06:09:06 +00:00

README.md

Identity Service

RWA平台用户身份管理微服务 - 基于NestJS + Prisma + Clean Architecture

技术栈

  • 框架: NestJS 10
  • ORM: Prisma 5
  • 数据库: PostgreSQL 15
  • 缓存: Redis 7 (ioredis)
  • 消息队列: Kafka
  • 区块链: ethers.js 6
  • 架构: Clean Architecture / Hexagonal Architecture / DDD / CQRS

功能特性

  • 自动创建账户首次打开APP
  • 多设备支持最多5个设备同时登录
  • 助记词生成与恢复
  • 手机号绑定与恢复
  • 三链钱包地址派生KAVA/DST/BSC
  • JWT Token认证
  • Token自动刷新账户永不过期
  • Kafka事件发布
  • 死信队列与自动重试

目录结构

src/
├── api/                    # 表现层
│   ├── controllers/        # HTTP控制器
│   └── dto/                # 数据传输对象
├── application/            # 应用层
│   ├── commands/           # 命令处理器
│   ├── queries/            # 查询处理器
│   └── services/           # 应用服务
├── domain/                 # 领域层
│   ├── aggregates/         # 聚合根
│   ├── entities/           # 实体
│   ├── value-objects/      # 值对象
│   ├── events/             # 领域事件
│   ├── repositories/       # 仓储接口
│   └── services/           # 领域服务
├── infrastructure/         # 基础设施层
│   ├── persistence/        # 数据持久化
│   ├── redis/              # Redis缓存
│   ├── kafka/              # Kafka消息
│   └── external/           # 外部服务
└── shared/                 # 共享层
    ├── decorators/         # 装饰器
    ├── guards/             # 守卫
    ├── filters/            # 过滤器
    └── exceptions/         # 异常

快速开始

1. 安装依赖

npm install

2. 配置环境变量

cp .env.example .env
# 编辑.env文件配置数据库连接等

3. 数据库迁移

# 生成Prisma客户端
npm run prisma:generate

# 运行迁移
npm run prisma:migrate

4. 启动服务

# 开发模式
npm run start:dev

# 生产模式
npm run build
npm run start:prod

Docker部署

启动所有服务

docker-compose up -d

仅启动依赖服务

docker-compose up -d postgres redis kafka zookeeper

API文档

启动服务后访问: http://localhost:3000/api/docs

主要API接口

方法 路径 描述 认证
POST /api/v1/user/auto-create 自动创建账户
POST /api/v1/user/recover-by-mnemonic 助记词恢复
POST /api/v1/user/recover-by-phone 手机号恢复
POST /api/v1/user/refresh-token 刷新Token
POST /api/v1/user/send-sms-code 发送验证码
POST /api/v1/user/bind-phone 绑定手机号
GET /api/v1/user/my-profile 我的资料
GET /api/v1/user/my-devices 我的设备
DELETE /api/v1/user/remove-device 移除设备
POST /api/v1/user/logout 退出登录

Kafka Topics

Topic 描述
identity.user-account.created 用户账户创建
identity.device.added 设备添加
identity.device.removed 设备移除
identity.phone.bound 手机号绑定
identity.kyc.submitted KYC提交
identity.kyc.approved KYC通过
identity.kyc.rejected KYC拒绝
identity.account.frozen 账户冻结
identity.wallet.bound 钱包绑定

测试

# 单元测试
npm run test

# E2E测试
npm run test:e2e

# 测试覆盖率
npm run test:cov

开发命令

# 格式化代码
npm run format

# Lint检查
npm run lint

# 打开Prisma Studio
npm run prisma:studio

环境变量

变量 描述 默认值
PORT 服务端口 3000
DATABASE_URL 数据库连接 -
REDIS_HOST Redis主机 localhost
REDIS_PORT Redis端口 6379
JWT_SECRET JWT密钥 -
JWT_ACCESS_EXPIRATION AccessToken有效期 2h
JWT_REFRESH_EXPIRATION RefreshToken有效期 30d
KAFKA_BROKERS Kafka地址 localhost:9092

License

MIT