准备部署

This commit is contained in:
AgentLabCn
2026-06-15 21:21:20 +08:00
parent 4167714149
commit 6c22cf9ef7
17 changed files with 898 additions and 2 deletions
+34
View File
@@ -0,0 +1,34 @@
# 排除无需进入镜像的文件,减小体积、避免泄密
__pycache__/
*.pyc
*.pyo
*.pyd
.venv/
venv/
env/
.Python
# 环境变量(含密钥,绝不能打进镜像)
.env
.env.*
# 运行时数据
uploads/
dist/
build/
*.egg-info/
# 测试与缓存
.pytest_cache/
.mypy_cache/
.ruff_cache/
.coverage
htmlcov/
# 版本控制与文档(镜像不需要)
.git/
.gitignore
# Dockerfile 本身
Dockerfile
Dockerfile.prod
+37
View File
@@ -0,0 +1,37 @@
# ============================================================
# 青叶 —— 生产环境后端镜像
# ------------------------------------------------------------
# 与开发用 Dockerfile 的区别:
# 1. 不切换国内镜像源(使用 Debian / PyPI 官方源,适合全球部署)
# 2. 运行命令固定为「单进程 uvicorn + proxy-headers」,不带 --reload
# 3. 不挂载源码(运行镜像内 COPY 进来的代码)
#
# ⚠️ 关键约束:必须单进程!WebSocket 连接管理器(ConnectionManager)是
# 进程内存单例。若改用 --workers N 或 gunicorn 多 worker,连接会被拆分到
# 不同进程,导致跨用户实时消息(聊天、撤回、好友请求等)无法投递。
# 水平扩展前需先把 manager 迁移到 Redis pub/sub。
# ============================================================
FROM python:3.12-slim
WORKDIR /app
# 安装系统依赖(使用官方源,便于全球部署)
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# 安装 Python 依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 复制项目代码(backend/.dockerignore 已排除 __pycache__/.env/uploads 等)
COPY . .
# 创建上传目录
RUN mkdir -p /app/uploads
EXPOSE 8000
# 生产运行:单 worker + proxy-headers(信任 Nginx 转发的 X-Forwarded-*
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers"]
+5
View File
@@ -73,4 +73,9 @@ class ConnectionManager:
# 全局单例
# ⚠️ 重要约束:本管理器为「进程内存」单例 —— active_connections 只存在于
# 当前进程。因此后端必须以「单进程」方式运行(生产镜像 Dockerfile.prod
# 已固定为单 worker uvicorn)。若使用 --workers N 或 gunicorn 多 worker
# 连接会分散到不同进程,跨用户 / 跨标签页的实时消息(聊天、撤回、好友请求、
# 互动通知等)将无法投递。水平扩展前需先将其迁移到基于 Redis 的 pub/sub。
manager = ConnectionManager()