From 8fba06bca38905a5198724e212d60843e5ebef1a Mon Sep 17 00:00:00 2001 From: vipg Date: Wed, 12 Nov 2025 17:10:40 +0800 Subject: [PATCH] add --- backend/country/deploy.sh | 4 +- frontend/asset-assistant-system/Dockerfile | 17 ++++++ frontend/asset-assistant-system/deploy.sh | 63 ++++++++++++++++++++++ frontend/asset-assistant-system/nginx.conf | 37 +++++++++++++ 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 frontend/asset-assistant-system/Dockerfile create mode 100644 frontend/asset-assistant-system/deploy.sh create mode 100644 frontend/asset-assistant-system/nginx.conf diff --git a/backend/country/deploy.sh b/backend/country/deploy.sh index 2d59748..6dbdbba 100644 --- a/backend/country/deploy.sh +++ b/backend/country/deploy.sh @@ -19,7 +19,7 @@ IMAGE_NAME="country-api" IMAGE_TAG="1.0.0" FULL_IMAGE="${IMAGE_NAME}:${IMAGE_TAG}" COMPOSE_PROJECT_NAME="country_service" -DOCKER_COMPOSE_FILE="./docker-compose.yaml" # ✅ 关键修复:等号两侧无空格 +DOCKER_COMPOSE_FILE="./docker-compose.yaml" SRC_DIR="./src" DOCKERFILE_PATH="${SRC_DIR}/Dockerfile" @@ -83,4 +83,4 @@ else exit 1 fi -log_info "===== 构建脚本执行完成 =====" \ No newline at end of file +log_info "===== 构建脚本执行完成 =====" diff --git a/frontend/asset-assistant-system/Dockerfile b/frontend/asset-assistant-system/Dockerfile new file mode 100644 index 0000000..7f742fa --- /dev/null +++ b/frontend/asset-assistant-system/Dockerfile @@ -0,0 +1,17 @@ +# 生产阶段:使用指定版本的 Nginx Alpine 镜像(大小写统一) +FROM nginx:1.29.3-alpine AS production-stage + +# 清除 Nginx 默认静态文件 +RUN rm -rf /usr/share/nginx/html/* + +# 复制项目所有文件到 Nginx 静态文件目录(适配你的项目结构) +COPY . /usr/share/nginx/html + +# 复制自定义 Nginx 配置(解决 SPA 路由刷新 404 问题,必须添加) +COPY ./nginx.conf /etc/nginx/conf.d/default.conf + +# 暴露 80 端口 +EXPOSE 80 + +# 启动 Nginx(前台运行) +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/frontend/asset-assistant-system/deploy.sh b/frontend/asset-assistant-system/deploy.sh new file mode 100644 index 0000000..49f6ab5 --- /dev/null +++ b/frontend/asset-assistant-system/deploy.sh @@ -0,0 +1,63 @@ +#!/bin/bash +set -euo pipefail # 更严格的错误检查:未定义变量报错、管道错误传递 + +# 定义日志函数(带时间戳和级别) +log_info() { + echo "[$(date +'%Y-%m-%d %H:%M:%S')] [INFO] $1" +} + +log_warn() { + echo "[$(date +'%Y-%m-%d %H:%M:%S')] [WARN] $1" >&2 +} + +log_error() { + echo "[$(date +'%Y-%m-%d %H:%M:%S')] [ERROR] $1" >&2 +} + +# 定义配置常量(等号两侧无空格!集中管理,便于修改) +IMAGE_NAME="asset-assistant-frontend-system" +IMAGE_TAG="1.0.0" +FULL_IMAGE="${IMAGE_NAME}:${IMAGE_TAG}" +DOCKERFILE_PATH="./Dockerfile" +SERVICE_NAME="asset-assistant-frontend" # 服务容器名称,便于管理 +HOST_PORT=8080 # 主机映射端口 +CONTAINER_PORT=80 # 容器内部端口 + +log_info "===== 开始执行构建脚本 =====" + +# 步骤1:停止并删除现有容器(忽略不存在的情况) +log_info "检查并停止现有容器: ${SERVICE_NAME}" +if sudo docker rm -f "${SERVICE_NAME}" >/dev/null 2>&1; then + log_info "容器 ${SERVICE_NAME} 已停止并删除" +else + log_warn "容器 ${SERVICE_NAME} 不存在,跳过删除步骤" +fi + +# 步骤2:删除现有镜像(忽略不存在的情况) +log_info "尝试删除现有镜像: ${FULL_IMAGE}" +if sudo docker rmi -f "${FULL_IMAGE}" >/dev/null 2>&1; then + log_info "镜像 ${FULL_IMAGE} 删除成功" +else + log_warn "镜像 ${FULL_IMAGE} 不存在或无法删除,跳过删除步骤" +fi + +# 步骤3:构建新镜像 +log_info "开始构建新镜像: ${FULL_IMAGE}(Dockerfile位于${DOCKERFILE_PATH})" +if sudo docker build -t "${FULL_IMAGE}" -f Dockerfile .; then + log_info "镜像 ${FULL_IMAGE} 构建成功" +else + log_error "镜像 ${FULL_IMAGE} 构建失败" + exit 1 +fi + +# 步骤4:启动服务 +log_info "启动新容器: ${SERVICE_NAME}(${HOST_PORT}:${CONTAINER_PORT})" +# 启动容器(后台运行、端口映射、命名容器) +if sudo docker run -d -p "${HOST_PORT}:${CONTAINER_PORT}" --name "${SERVICE_NAME}" "${FULL_IMAGE}"; then + log_info "容器 ${SERVICE_NAME} 启动成功,访问地址: http://localhost:${HOST_PORT}" +else + log_error "容器 ${SERVICE_NAME} 启动失败" + exit 1 +fi + +log_info "===== 构建脚本执行完成 =====" diff --git a/frontend/asset-assistant-system/nginx.conf b/frontend/asset-assistant-system/nginx.conf new file mode 100644 index 0000000..135bfd1 --- /dev/null +++ b/frontend/asset-assistant-system/nginx.conf @@ -0,0 +1,37 @@ +server { + listen 80; + server_name localhost; + root /usr/share/nginx/html; + index index.html; # 入口文件指向项目根目录的 index.html + + # 支持 SPA 路由重写(关键:解决刷新404) + location / { + try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache"; # 入口文件不缓存 + } + + # 静态资源缓存配置(JS/CSS/图片等) + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|otf)$ { + expires 7d; # 缓存7天 + add_header Cache-Control "public, max-age=604800"; + add_header Access-Control-Allow-Origin "*"; # 允许跨域(可选) + } + + # 禁止访问隐藏文件(如 .git、.env 等) + location ~ /\. { + deny all; + access_log off; + log_not_found off; + } + + # 优化 Nginx 响应速度 + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + + # 关闭不必要的日志 + access_log off; + error_log /var/log/nginx/error.log warn; +} \ No newline at end of file