增加不同环境的配置

This commit is contained in:
aaron 2025-01-13 22:10:28 +08:00
parent 1fa9a3acc7
commit 877d7c035b
4 changed files with 42 additions and 26 deletions

View File

@ -1,7 +1,14 @@
# 构建阶段 # 构建阶段
FROM node:16-alpine as builder FROM node:16 as builder
# 设置构建参数
ARG BUILD_ENV=testing
ARG API_URL
# 设置环境变量
ENV NODE_ENV=${BUILD_ENV}
ENV VUE_APP_API_URL=${API_URL}
# 设置工作目录
WORKDIR /app WORKDIR /app
# 复制 package.json 和 package-lock.json # 复制 package.json 和 package-lock.json
@ -13,23 +20,18 @@ RUN npm install
# 复制源代码 # 复制源代码
COPY . . COPY . .
# 构建项目 # 构建应用
RUN npm run build RUN npm run build
# 生产阶段 # 生产阶段
FROM node:16-alpine FROM nginx:alpine
# 设置工作目录 # 复制 nginx 配置
WORKDIR /app COPY nginx.conf /etc/nginx/conf.d/default.conf
# 安装 serve 包来提供静态文件服务 # 从构建阶段复制构建结果
RUN npm install -g serve COPY --from=builder /app/dist /usr/share/nginx/html
# 从构建阶段复制构建产物 EXPOSE 80
COPY --from=builder /app/dist ./dist
# 暴露端口 CMD ["nginx", "-g", "daemon off;"]
EXPOSE 3000
# 启动服务
CMD ["serve", "-s", "dist", "-l", "3000"]

View File

@ -3,21 +3,21 @@ server {
server_name localhost; server_name localhost;
root /usr/share/nginx/html; root /usr/share/nginx/html;
index index.html index.htm; index index.html;
# 支持 history 路由模式
location / { location / {
try_files $uri $uri/ /index.html; try_files $uri $uri/ /index.html;
} }
# 配置 API 代理(根据实际需求修改) # 缓存静态资源
location /api { location /assets {
proxy_pass http://backend-service; # 替换为实际的后端服务地址 expires 1y;
proxy_set_header Host $host; add_header Cache-Control "public, no-transform";
proxy_set_header X-Real-IP $remote_addr;
} }
error_page 500 502 503 504 /50x.html; # 禁止访问 . 文件
location = /50x.html { location ~ /\. {
root /usr/share/nginx/html; deny all;
} }
} }

13
src/config/env.js Normal file
View File

@ -0,0 +1,13 @@
const env = {
development: {
API_URL: 'http://localhost:8080'
},
testing: {
API_URL: process.env.VUE_APP_API_URL || 'https://api-dev.ibtc.work'
},
production: {
API_URL: process.env.VUE_APP_API_URL || 'http://api.example.com'
}
}
export default env[process.env.NODE_ENV || 'development']

View File

@ -1,9 +1,10 @@
import axios from 'axios' import axios from 'axios'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import config from '@/config/env'
const request = axios.create({ const request = axios.create({
baseURL: 'http://127.0.0.1:8000', baseURL: config.API_URL,
timeout: 5000, timeout: 10000,
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }