From 4913e96586def3c032cceeed0cce9eeb748eb511 Mon Sep 17 00:00:00 2001 From: Junhui Chen Date: Mon, 14 Jul 2025 21:02:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8E=A5=E5=90=8E=E7=AB=AF=E7=BA=BF?= =?UTF-8?q?=E4=B8=8Aapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 1 + .env.production | 1 + .gitea/workflows/prod.yaml | 4 +++- app.config.js | 9 +++++++++ dockerfiles/prod.Dockerfile | 17 +++++++++++++++++ lib/server-api-util.ts | 9 +++++---- 6 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 .env create mode 100644 .env.production create mode 100644 app.config.js create mode 100644 dockerfiles/prod.Dockerfile diff --git a/.env b/.env new file mode 100644 index 0000000..53a6945 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +API_ENDPOINT=http://192.168.31.115:18080/api \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..a102646 --- /dev/null +++ b/.env.production @@ -0,0 +1 @@ +API_ENDPOINT=https://api.memorywake.com/api \ No newline at end of file diff --git a/.gitea/workflows/prod.yaml b/.gitea/workflows/prod.yaml index d9b8a6b..957e503 100644 --- a/.gitea/workflows/prod.yaml +++ b/.gitea/workflows/prod.yaml @@ -23,12 +23,14 @@ jobs: run: | echo "Building the Docker image..." tag_name=${{ gitea.ref_name }} - docker build -t hub.fairclip.cn/memowake/memowake-front:${tag_name} . + tag_name=$(echo "${{ gitea.ref_name }}" | tr '/' '-') + docker build -t hub.fairclip.cn/memowake/memowake-front:${tag_name} -f dockerfiles/prod.Dockerfile . docker push hub.fairclip.cn/memowake/memowake-front:${tag_name} echo "Docker image built successfully!" - name: Deploy run: | echo "Deploying the project..." tag_name=${{ gitea.ref_name }} + tag_name=$(echo "${{ gitea.ref_name }}" | tr '/' '-') bash ./scripts/prod_deploy.sh ${tag_name} echo "Deploy successful!" \ No newline at end of file diff --git a/app.config.js b/app.config.js new file mode 100644 index 0000000..5c730f6 --- /dev/null +++ b/app.config.js @@ -0,0 +1,9 @@ +// app.config.js +console.log("API_ENDPOINT from process.env:", process.env.API_ENDPOINT); + +export default ({ config }) => ({ + ...config, + extra: { + API_ENDPOINT: process.env.API_ENDPOINT || "http://192.168.31.115:18080/api" + } +}); \ No newline at end of file diff --git a/dockerfiles/prod.Dockerfile b/dockerfiles/prod.Dockerfile new file mode 100644 index 0000000..9210a8f --- /dev/null +++ b/dockerfiles/prod.Dockerfile @@ -0,0 +1,17 @@ +# 第一阶段:构建 Expo Web 静态文件 +FROM docker.fairclip.cn/node:22 AS builder +WORKDIR /app +COPY package.json . +# 设置npm源 +RUN npm config set registry http://192.168.31.115:8081/repository/npm/ +RUN npm install -g expo-cli && npm install +COPY . . +RUN cp .env.production .env +RUN npx expo export -p web + +# 第二阶段:使用 nginx 作为 Web 服务器 +FROM docker.fairclip.cn/nginx:1.29-alpine +COPY --from=builder /app/dist /usr/share/nginx/html +COPY nginx.conf /etc/nginx/nginx.conf +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/lib/server-api-util.ts b/lib/server-api-util.ts index 18d045c..7793c1c 100644 --- a/lib/server-api-util.ts +++ b/lib/server-api-util.ts @@ -23,13 +23,14 @@ export interface PagedResult { } // 获取.env文件中的变量 -export const API_ENDPOINT = "http://192.168.31.115:18080/api" + +export const API_ENDPOINT = () => process.env.API_ENDPOINT || "http://192.168.31.115:18080/api"; // 更新 access_token 的逻辑 - 用于React组件中 export const useAuthToken = async(message: string | null) => { try { const { login } = useAuth(); - const response = await fetch(`${API_ENDPOINT}/v1/iam/access-token-refresh`); + const response = await fetch(`${API_ENDPOINT()}/v1/iam/access-token-refresh`); const apiResponse: ApiResponse = await response.json(); // 如果接口报错,页面弹出来错误信息 @@ -71,7 +72,7 @@ export const refreshAuthToken = async(message: string | null): Promise // 退出刷新会重新填充数据 let response; - response = await fetch(`${API_ENDPOINT}/v1/iam/access-token-refresh`, { + response = await fetch(`${API_ENDPOINT()}/v1/iam/access-token-refresh`, { method: "POST", body: JSON.stringify({ "refresh_token": cookie, @@ -139,7 +140,7 @@ export const fetchApi = async ( headers.set('Authorization', `Bearer ${token}`); } - const url = `${API_ENDPOINT}/v1${path}`; + const url = `${API_ENDPOINT()}/v1${path}`; const response = await fetch(url, { ...options, headers,