基于 DDD (领域驱动设计) 架构的短链接管理平台,支持自定义卡片、批量创建、来源统计。
📖 DDD 核心概念说明:CORE.md
# 1. 克隆项目
cd /workspace/shortlink-platform
# 2. 复制环境变量文件
cp .env.example .env
# 3. 启动所有服务
docker-compose up -d
# 4. 访问应用
# API 文档: http://localhost:8000/api/docs
# 管理后台: http://localhost:8000
# 1. 安装依赖 (需要 uv,参考 https://docs.astral.sh/uv/getting-started/installation/)
uv sync
# 2. 配置环境变量
cp .env.example .env
# 3. 启动 PostgreSQL 和 Redis (或使用 Docker)
docker-compose up -d postgres redis
# 4. 初始化数据库
uv run python scripts/init_db.py
# 5. 启动服务
uv run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
shortlink-platform/
├── app/
│ ├── domain/ # 领域层
│ │ ├── shared/ # 共享内核
│ │ ├── user/ # 用户子域
│ │ ├── short_link/ # 短链子域 (核心)
│ │ ├── access_log/ # 访问日志子域
│ │ └── custom_domain/ # 自定义域名子域
│ ├── application/ # 应用层 (CQRS)
│ │ ├── commands/ # 命令
│ │ ├── queries/ # 查询
│ │ ├── handlers/ # 命令/查询处理器
│ │ └── dto/ # 数据传输对象
│ ├── infrastructure/ # 基础设施层
│ │ ├── persistence/ # 数据持久化
│ │ ├── security/ # 安全 (JWT, 密码加密)
│ │ ├── cache/ # 缓存 (Redis)
│ │ └── config/ # 配置
│ ├── interfaces/ # 接口层
│ │ ├── api/ # REST API
│ │ └── web/ # Web 页面
│ └── main.py # 应用入口
├── docker/ # Docker 配置
├── scripts/ # 脚本
├── tests/ # 测试
└── docker-compose.yml
# 注册
curl -X POST http://localhost:8000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{"username": "test", "email": "test@example.com", "password": "123456"}'
# 登录
curl -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username_or_email": "test", "password": "123456"}'
curl -X POST http://localhost:8000/api/v1/links \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"original_url": "https://example.com",
"title": "Example 网站",
"description": "这是一个示例网站",
"platform": "wechat"
}'
# 直接访问 (重定向)
curl http://localhost:8000/abc123
# 预览 (查看卡片)
curl http://localhost:8000/abc123/preview
包含业务逻辑和领域模型:
使用 CQRS 模式:
提供技术实现:
对外暴露接口:
参考 .env.example 配置环境变量:
| 变量 | 说明 | 默认值 |
|---|---|---|
| DATABASE_URL | PostgreSQL 连接字符串 | postgresql+asyncpg://postgres:postgres@postgres:5432/shortlink |
| REDIS_URL | Redis 连接字符串 | redis://redis:6379/0 |
| SECRET_KEY | JWT 密钥 | your-super-secret-key |
| ACCESS_TOKEN_EXPIRE_MINUTES | Token 过期时间 | 1440 (24小时) |
| BASE_URL | 应用基础 URL | http://localhost:8000 |
MIT