feat: 初始化考培练系统项目
- 从服务器拉取完整代码 - 按框架规范整理项目结构 - 配置 Drone CI 测试环境部署 - 包含后端(FastAPI)、前端(Vue3)、管理端 技术栈: Vue3 + TypeScript + FastAPI + MySQL
This commit is contained in:
91
frontend/docker/default.conf
Normal file
91
frontend/docker/default.conf
Normal file
@@ -0,0 +1,91 @@
|
||||
# 考培练系统前端 Nginx 配置
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
root /usr/share/nginx/html;
|
||||
index index.html index.htm;
|
||||
|
||||
# 日志配置
|
||||
access_log /var/log/nginx/access.log;
|
||||
error_log /var/log/nginx/error.log;
|
||||
|
||||
# 静态资源缓存配置
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
||||
expires 1y;
|
||||
add_header Cache-Control "public, immutable";
|
||||
add_header Vary Accept-Encoding;
|
||||
access_log off;
|
||||
}
|
||||
|
||||
# HTML 文件不缓存
|
||||
location ~* \.html$ {
|
||||
expires -1;
|
||||
add_header Cache-Control "no-cache, no-store, must-revalidate";
|
||||
add_header Pragma "no-cache";
|
||||
}
|
||||
|
||||
# API 代理配置
|
||||
location /api/ {
|
||||
proxy_pass http://kaopeilian-backend:8000;
|
||||
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;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
proxy_read_timeout 300s;
|
||||
proxy_connect_timeout 300s;
|
||||
proxy_send_timeout 300s;
|
||||
}
|
||||
|
||||
# WebSocket 代理配置
|
||||
location /ws/ {
|
||||
proxy_pass http://kaopeilian-backend:8000;
|
||||
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;
|
||||
proxy_read_timeout 86400;
|
||||
}
|
||||
|
||||
# SPA 路由支持
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
}
|
||||
|
||||
# 健康检查端点
|
||||
location /health {
|
||||
access_log off;
|
||||
return 200 "healthy\n";
|
||||
add_header Content-Type text/plain;
|
||||
}
|
||||
|
||||
# 安全配置
|
||||
location ~ /\. {
|
||||
deny all;
|
||||
access_log off;
|
||||
log_not_found off;
|
||||
}
|
||||
|
||||
location ~ ~$ {
|
||||
deny all;
|
||||
access_log off;
|
||||
log_not_found off;
|
||||
}
|
||||
|
||||
# 错误页面配置
|
||||
error_page 404 /index.html;
|
||||
error_page 500 502 503 504 /50x.html;
|
||||
|
||||
location = /50x.html {
|
||||
root /usr/share/nginx/html;
|
||||
}
|
||||
}
|
||||
61
frontend/docker/nginx.conf
Normal file
61
frontend/docker/nginx.conf
Normal file
@@ -0,0 +1,61 @@
|
||||
# Nginx 主配置文件
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
error_log /var/log/nginx/error.log notice;
|
||||
pid /var/run/nginx.pid;
|
||||
|
||||
# 事件模块配置
|
||||
events {
|
||||
worker_connections 1024;
|
||||
use epoll;
|
||||
multi_accept on;
|
||||
}
|
||||
|
||||
# HTTP 模块配置
|
||||
http {
|
||||
# MIME 类型配置
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
# 日志格式
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||
|
||||
access_log /var/log/nginx/access.log main;
|
||||
|
||||
# 基础配置
|
||||
sendfile on;
|
||||
tcp_nopush on;
|
||||
tcp_nodelay on;
|
||||
keepalive_timeout 65;
|
||||
types_hash_max_size 2048;
|
||||
client_max_body_size 20M;
|
||||
|
||||
# Gzip 压缩配置
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_proxied any;
|
||||
gzip_comp_level 6;
|
||||
gzip_types
|
||||
text/plain
|
||||
text/css
|
||||
text/xml
|
||||
text/javascript
|
||||
application/json
|
||||
application/javascript
|
||||
application/xml+rss
|
||||
application/atom+xml
|
||||
image/svg+xml;
|
||||
|
||||
# 安全头配置
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "no-referrer-when-downgrade" always;
|
||||
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
|
||||
|
||||
# 包含虚拟主机配置
|
||||
include /etc/nginx/conf.d/*.conf;
|
||||
}
|
||||
75
frontend/docker/scripts/build.sh
Executable file
75
frontend/docker/scripts/build.sh
Executable file
@@ -0,0 +1,75 @@
|
||||
#!/bin/bash
|
||||
# 构建脚本
|
||||
|
||||
set -e
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# 函数定义
|
||||
log() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
warn() {
|
||||
echo -e "${YELLOW}[WARN]${NC} $1"
|
||||
}
|
||||
|
||||
error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# 检查 Docker 是否安装
|
||||
if ! command -v docker &> /dev/null; then
|
||||
error "Docker 未安装,请先安装 Docker"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查 docker-compose 是否安装
|
||||
if ! command -v docker-compose &> /dev/null; then
|
||||
warn "docker-compose 未安装,尝试使用 docker compose"
|
||||
DOCKER_COMPOSE="docker compose"
|
||||
else
|
||||
DOCKER_COMPOSE="docker-compose"
|
||||
fi
|
||||
|
||||
# 获取参数
|
||||
ENVIRONMENT=${1:-production}
|
||||
VERSION=${2:-latest}
|
||||
REGISTRY=${3:-""}
|
||||
|
||||
log "开始构建考培练系统前端..."
|
||||
log "环境: $ENVIRONMENT"
|
||||
log "版本: $VERSION"
|
||||
|
||||
# 设置镜像名称
|
||||
IMAGE_NAME="kaopeilian-frontend"
|
||||
if [ -n "$REGISTRY" ]; then
|
||||
FULL_IMAGE_NAME="$REGISTRY/$IMAGE_NAME:$VERSION"
|
||||
else
|
||||
FULL_IMAGE_NAME="$IMAGE_NAME:$VERSION"
|
||||
fi
|
||||
|
||||
# 构建镜像
|
||||
log "构建 Docker 镜像: $FULL_IMAGE_NAME"
|
||||
|
||||
if [ "$ENVIRONMENT" = "development" ]; then
|
||||
docker build --target development -t "$FULL_IMAGE_NAME-dev" .
|
||||
log "开发环境镜像构建完成: $FULL_IMAGE_NAME-dev"
|
||||
else
|
||||
docker build --target production -t "$FULL_IMAGE_NAME" .
|
||||
log "生产环境镜像构建完成: $FULL_IMAGE_NAME"
|
||||
fi
|
||||
|
||||
# 清理悬挂镜像
|
||||
log "清理悬挂镜像..."
|
||||
docker image prune -f
|
||||
|
||||
log "构建完成!"
|
||||
|
||||
# 显示镜像信息
|
||||
log "镜像信息:"
|
||||
docker images | grep "$IMAGE_NAME" | head -5
|
||||
132
frontend/docker/scripts/deploy.sh
Executable file
132
frontend/docker/scripts/deploy.sh
Executable file
@@ -0,0 +1,132 @@
|
||||
#!/bin/bash
|
||||
# 部署脚本
|
||||
|
||||
set -e
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# 函数定义
|
||||
log() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
warn() {
|
||||
echo -e "${YELLOW}[WARN]${NC} $1"
|
||||
}
|
||||
|
||||
error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
# 获取参数
|
||||
ENVIRONMENT=${1:-production}
|
||||
ACTION=${2:-up}
|
||||
|
||||
log "考培练系统前端部署脚本"
|
||||
log "环境: $ENVIRONMENT"
|
||||
log "操作: $ACTION"
|
||||
|
||||
# 检查 docker-compose 是否安装
|
||||
if ! command -v docker-compose &> /dev/null; then
|
||||
if command -v docker &> /dev/null && docker compose version &> /dev/null; then
|
||||
DOCKER_COMPOSE="docker compose"
|
||||
else
|
||||
error "docker-compose 或 docker compose 未找到"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
DOCKER_COMPOSE="docker-compose"
|
||||
fi
|
||||
|
||||
# 设置配置文件
|
||||
if [ "$ENVIRONMENT" = "development" ]; then
|
||||
COMPOSE_FILE="docker-compose.dev.yml"
|
||||
else
|
||||
COMPOSE_FILE="docker-compose.yml"
|
||||
fi
|
||||
|
||||
# 检查配置文件是否存在
|
||||
if [ ! -f "$COMPOSE_FILE" ]; then
|
||||
error "配置文件 $COMPOSE_FILE 不存在"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 执行操作
|
||||
case $ACTION in
|
||||
up)
|
||||
log "启动服务..."
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" up -d
|
||||
;;
|
||||
down)
|
||||
log "停止服务..."
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" down
|
||||
;;
|
||||
restart)
|
||||
log "重启服务..."
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" down
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" up -d
|
||||
;;
|
||||
logs)
|
||||
log "查看日志..."
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" logs -f
|
||||
;;
|
||||
ps)
|
||||
log "查看服务状态..."
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" ps
|
||||
;;
|
||||
build)
|
||||
log "构建并启动服务..."
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" up -d --build
|
||||
;;
|
||||
clean)
|
||||
log "清理服务和数据..."
|
||||
warn "这将删除所有容器和数据卷,确定继续吗?(y/N)"
|
||||
read -r response
|
||||
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" down -v --rmi all
|
||||
docker system prune -f
|
||||
else
|
||||
log "操作已取消"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
error "未知操作: $ACTION"
|
||||
echo "可用操作: up, down, restart, logs, ps, build, clean"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# 显示服务状态
|
||||
if [ "$ACTION" = "up" ] || [ "$ACTION" = "restart" ] || [ "$ACTION" = "build" ]; then
|
||||
log "等待服务启动..."
|
||||
sleep 5
|
||||
|
||||
log "服务状态:"
|
||||
$DOCKER_COMPOSE -f "$COMPOSE_FILE" ps
|
||||
|
||||
if [ "$ENVIRONMENT" = "development" ]; then
|
||||
info "开发环境访问地址:"
|
||||
info "前端: http://localhost:3001"
|
||||
info "后端: http://localhost:8000"
|
||||
info "数据库: localhost:3307"
|
||||
info "Redis: localhost:6380"
|
||||
info "邮件服务: http://localhost:8025"
|
||||
else
|
||||
info "生产环境访问地址:"
|
||||
info "前端: http://localhost:3001"
|
||||
info "后端: http://localhost:8000"
|
||||
info "数据库: localhost:3306"
|
||||
info "Redis: localhost:6379"
|
||||
fi
|
||||
fi
|
||||
|
||||
log "部署完成!"
|
||||
Reference in New Issue
Block a user