diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e0aca8e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,40 @@ +# 忽略不需要的文件 +.git +.gitignore +node_modules +__pycache__ +*.pyc +*.pyo +*.pyd +.Python +env/ +venv/ +.env +.env.local +.DS_Store +Thumbs.db + +# 开发文件 +README.md +ROADMAP.md +CONTRIBUTING.md +.github/ +.vscode/ +.idea/ +*.md + +# 构建产物 +web/dist/ +web/node_modules/ +dist/ +build/ + +# 日志 +*.log +logs/ + +# 测试 +tests/ +pytest_cache/ +.coverage +htmlcov/ diff --git a/.gitignore b/.gitignore index 6e9712c..2169fc0 100644 --- a/.gitignore +++ b/.gitignore @@ -152,4 +152,16 @@ local_config.yml 台本/ 笔记/ -tmp*/ \ No newline at end of file +tmp*/ + +############################# +# Docker +############################# + +# Docker data directories +saves/ +logs/ + +# Docker environment files +.env + diff --git a/Dockerfile.backend b/Dockerfile.backend new file mode 100644 index 0000000..7693b9a --- /dev/null +++ b/Dockerfile.backend @@ -0,0 +1,32 @@ +# 后端 Dockerfile +FROM python:3.12-slim + +# 设置工作目录 +WORKDIR /app + +# 安装系统依赖 +RUN apt-get update && apt-get install -y \ + gcc \ + g++ \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# 复制依赖文件 +COPY requirements.txt . + +# 安装 Python 依赖 清华园镜像源 +RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple + +# 复制项目代码 +COPY src/ ./src/ +COPY static/ ./static/ +COPY assets/ ./assets/ + +# 创建必要的目录 +RUN mkdir -p /app/assets/saves /app/logs + +# 暴露端口 +EXPOSE 8002 + +# 启动命令 +CMD ["uvicorn", "src.server.main:app", "--host", "0.0.0.0", "--port", "8002"] diff --git a/Dockerfile.frontend b/Dockerfile.frontend new file mode 100644 index 0000000..6053da6 --- /dev/null +++ b/Dockerfile.frontend @@ -0,0 +1,30 @@ +# 前端 Dockerfile (多阶段构建) +FROM node:22-alpine AS builder + +# 设置工作目录 +WORKDIR /app + +# 复制 package.json 和 package-lock.json +COPY web/package.json web/package-lock.json* ./ + +# 安装依赖 +RUN npm ci + +# 复制前端源代码 +COPY web/ . +# 构建前端 +RUN npm run build + +# 生产阶段 - 使用 Nginx +FROM nginx:alpine + +# 复制构建产物 +COPY --from=builder /app/dist /usr/share/nginx/html + +# 复制 Nginx 配置 +COPY nginx.conf /etc/nginx/conf.d/default.conf + +# 暴露端口 +EXPOSE 80 + +# Nginx 会自动启动 diff --git a/README.md b/README.md index 39f4e4f..a35024f 100644 --- a/README.md +++ b/README.md @@ -108,9 +108,32 @@ ## 🚀 使用方法 ### ⚙️ 运行步骤 + +#### 方式一:Docker Compose 一键部署(推荐) + +如果你已经安装了 Docker,这是最简单的方式: + +# 1. 克隆项目 +```bash + +git clone https://github.com/AI-Cultivation/cultivation-world-simulator.git +cd cultivation-world-simulator +``` +# 2. 启动服务(自动构建并运行) +```bash +docker-compose up -d --build +``` +# 3. 访问应用 + +前端:`http://localhost:8123` +后端 API:`http://localhost:8002` +``` + +#### 方式二:手动安装运行 + 1. 克隆项目到本地: ```bash - git clone https://github.com/your-username/cultivation-world-simulator.git + git clone https://github.com/AI-Cultivation/cultivation-world-simulator.git cd cultivation-world-simulator ``` @@ -118,7 +141,7 @@ ```bash # 后端依赖 pip install -r requirements.txt - + # 前端依赖 (需Node.js环境) cd web && npm install ``` @@ -126,7 +149,7 @@ 3. 配置LLM: **推荐方式:在前端直接配置(支持快速填充预设)** - + 前端LLM配置 也可以在 `static/local_config.yml` 中手动配置(OpenAI兼容格式): diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a4086f2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +version: '3.8' + +services: + # 后端服务 + backend: + build: + context: . + dockerfile: Dockerfile.backend + container_name: cultivation-backend + ports: + - "8002:8002" + environment: + - PYTHONUNBUFFERED=1 + restart: unless-stopped + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8002/api/state"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 30s + volumes: + - ./assets/saves:/app/assets/saves + - ./logs:/app/logs + # 前端服务 + frontend: + build: + context: . + dockerfile: Dockerfile.frontend + container_name: cultivation-frontend + ports: + - "8123:80" + depends_on: + - backend + restart: unless-stopped + healthcheck: + test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:80/"] + interval: 30s + timeout: 10s + retries: 3 + diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..6fd2e8f --- /dev/null +++ b/nginx.conf @@ -0,0 +1,41 @@ +server { + listen 80; + server_name localhost; + + # 前端静态文件 + location / { + root /usr/share/nginx/html; + index index.html; + try_files $uri $uri/ /index.html; + } + + # 代理 API 请求到后端 + location /api { + proxy_pass http://backend:8002; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # 代理 WebSocket 连接 + location /ws { + proxy_pass http://backend:8002; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # 代理游戏资源 + location /assets { + proxy_pass http://backend:8002; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +}