This commit is contained in:
2026-04-11 13:24:41 +08:00
parent b12a84e388
commit 4dc6cb249b
19 changed files with 291 additions and 0 deletions

21
.env Normal file
View File

@@ -0,0 +1,21 @@
# 数据库配置
DB_HOST=pc-3nsh5mzbet7w5l890.rwlb.rds.aliyuncs.com
DB_PORT=3306
DB_USER=u1admin
DB_PASSWORD=6y6K!aFg
DB_NAME=employee_performance
# JWT 配置
JWT_SECRET=dev_jwt_secret_please_change_in_production
# FastGPT API 配置
FASTGPT_API_KEY=fastgpt-oEipxYa5BfVaeGDj74iAXi8YSkWyye07lTNYuj7yydsEKAc4Hp2Z2RDbxsxc4TuZ
FASTGPT_API_URL=https://cloud.fastgpt.cn/api/v1/chat/completions
FASTGPT_MODEL=gpt-4
# 代理配置Node.js 不自动读取系统代理,需手动配置)
#HTTPS_PROXY=http://127.0.0.1:7890
# 服务器配置
PORT=3001
NODE_ENV=development

42
backend/.dockerignore Normal file
View File

@@ -0,0 +1,42 @@
# 依赖目录
node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# 构建输出
dist
build
# 环境文件(在运行时通过环境变量或挂载提供)
.env
.env.local
.env.development
.env.production
# 测试相关
coverage
.nyc_output
# 编辑器文件
.vscode
.idea
*.swp
*.swo
# 系统文件
.DS_Store
Thumbs.db
# Git
.git
.gitignore
# Docker相关
.dockerignore
Dockerfile
docker-compose.yml
# 日志文件
logs
*.log

84
backend/DOCKER-README.md Normal file
View File

@@ -0,0 +1,84 @@
# Docker 部署说明
## 1. 仅构建后端镜像
```bash
# 在 backend 目录中
cd backend
# 构建 Docker 镜像
docker build -t employee-performance-backend:latest .
# 运行容器
docker run -d \
-p 3001:3001 \
-e DB_HOST=localhost \
-e DB_PORT=3306 \
-e DB_USER=performance_user \
-e DB_PASSWORD=userpassword123 \
-e DB_NAME=employee_performance \
-e JWT_SECRET=your_jwt_secret_here_change_in_production \
-e FASTGPT_API_KEY=your_fastgpt_api_key_here \
--name employee-performance-backend \
employee-performance-backend:latest
```
## 2. 使用 Docker Compose推荐
```bash
# 在项目根目录(包含 docker-compose.yml
cd ..
# 启动所有服务MySQL + 后端)
docker-compose up -d
# 查看日志
docker-compose logs -f
# 停止服务
docker-compose down
# 停止并删除数据卷
docker-compose down -v
```
## 3. 环境变量配置
复制 `.env.example``.env` 并根据需要修改:
```bash
cd backend
cp .env.example .env
```
或者通过 Docker Compose 的环境变量直接配置(见 docker-compose.yml
## 4. 数据库初始化
Docker Compose 会自动:
1. 创建 MySQL 8.0 容器
2. 初始化数据库结构(使用 `src/db/init.sql`
3. 插入测试数据(使用 `src/db/seed.sql`
## 5. 健康检查
后端服务提供健康检查端点:
```
GET http://localhost:3001/health
```
Docker 容器已配置健康检查,确保服务正常运行。
## 6. 构建优化
Dockerfile 使用多阶段构建:
- 第一阶段:安装依赖并构建 TypeScript
- 第二阶段:仅复制运行所需文件,减小镜像大小
## 7. 生产环境注意事项
1. **修改默认密码**:在 docker-compose.yml 中修改 MySQL 密码
2. **设置强 JWT_SECRET**:使用强密钥替换默认值
3. **配置 FastGPT API**:提供有效的 API 密钥
4. **数据持久化**MySQL 数据存储在 `mysql_data` 卷中
5. **网络安全**:生产环境应考虑使用 Docker 网络隔离

53
backend/Dockerfile Normal file
View File

@@ -0,0 +1,53 @@
# 构建阶段
FROM node:20-alpine AS builder
# 设置工作目录
WORKDIR /app
# 复制package.json和package-lock.json
COPY package*.json ./
# 安装依赖包括devDependencies因为需要typescript等构建工具
RUN npm ci --only=production --ignore-scripts && \
npm ci --only=development --ignore-scripts
# 复制源代码
COPY . .
# 构建TypeScript
RUN npm run build
# 运行时阶段
FROM node:20-alpine AS runtime
# 设置工作目录
WORKDIR /app
# 创建非root用户
RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001 -G nodejs
# 复制package.json
COPY --from=builder --chown=nodejs:nodejs /app/package*.json ./
# 安装仅生产依赖
RUN npm ci --only=production --ignore-scripts
# 复制构建产物
COPY --from=builder --chown=nodejs:nodejs /app/dist ./dist
# 复制可能需要的其他文件(如数据库初始化脚本)
COPY --from=builder --chown=nodejs:nodejs /app/src/db ./src/db
# 切换用户
USER nodejs
# 暴露端口默认3001可通过环境变量覆盖
EXPOSE 3001
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "require('http').get('http://localhost:3001/health', (r) => {if(r.statusCode===200)process.exit(0);process.exit(1)}).on('error',()=>process.exit(1))"
# 启动命令
CMD ["npm", "start"]

View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system/backend

View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system/backend

View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system/backend

14
create-db-user.sql Normal file
View File

@@ -0,0 +1,14 @@
-- 创建新的数据库用户(如果你能以 root 身份登录 MySQL
-- 在 MySQL 命令行中执行此脚本
-- 创建用户(密码设置为 123456
CREATE USER IF NOT EXISTS 'emp_user'@'localhost' IDENTIFIED BY '123456';
-- 授予权限
GRANT ALL PRIVILEGES ON employee_performance.* TO 'emp_user'@'localhost';
-- 刷新权限
FLUSH PRIVILEGES;
-- 显示用户
SELECT User, Host FROM mysql.user WHERE User = 'emp_user';

64
docker-compose.yml Normal file
View File

@@ -0,0 +1,64 @@
version: '3.8'
services:
# MySQL数据库
mysql:
image: mysql:8.0
container_name: employee-performance-mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: rootpassword123
MYSQL_DATABASE: employee_performance
MYSQL_USER: performance_user
MYSQL_PASSWORD: userpassword123
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
- ./backend/src/db/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./backend/src/db/seed.sql:/docker-entrypoint-initdb.d/seed.sql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-prootpassword123"]
timeout: 20s
retries: 10
start_period: 30s
networks:
- performance-network
# 后端应用
backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: employee-performance-backend
restart: unless-stopped
environment:
DB_HOST: mysql
DB_PORT: 3306
DB_USER: performance_user
DB_PASSWORD: userpassword123
DB_NAME: employee_performance
JWT_SECRET: your_jwt_secret_here_change_in_production
FASTGPT_API_KEY: your_fastgpt_api_key_here
FASTGPT_API_URL: https://api.fastgpt.in/api/v1/chat/completions
FASTGPT_MODEL: gpt-4
PORT: 3001
NODE_ENV: production
ports:
- "3001:3001"
depends_on:
mysql:
condition: service_healthy
networks:
- performance-network
# 如果需要在容器内运行数据库初始化脚本,可以取消下面的注释
# volumes:
# - ./backend/src/db:/app/src/db:ro
networks:
performance-network:
driver: bridge
volumes:
mysql_data:
driver: local

1
tmpclaude-1560-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-5530-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-709e-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-7f74-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-8f6e-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-9841-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-9b0e-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-a768-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-aa47-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system

1
tmpclaude-e0aa-cwd Normal file
View File

@@ -0,0 +1 @@
/c/Users/99095/Desktop/优一科技/performance-evaluation-system