feat: proxy embed assets to in-cluster project57 [deploy]
This commit is contained in:
1 parent
f717f1d481
commit
fe18e608dc
8 files changed
+67
-36
No files matched your search
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
# A 站(project57)源站,用于加载 aurora.js 以及图片 / API
|
# 仅本地开发:Vite 把 /embed /images /api/contact 代理到本机 A 站
|
||||||
VITE_A_ORIGIN=http://localhost:3000
|
VITE_A_ORIGIN=http://localhost:3000
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
# A 站(project57)线上源站。构建时写入,B 站用它加载 aurora.js / 图片 / 表单 API。
|
|
||||||
VITE_A_ORIGIN=https://swissicapital.net
|
|
||||||
@@ -126,14 +126,7 @@ jobs:
|
|||||||
|| pnpm install --frozen-lockfile --prefer-offline --registry=https://registry.npmjs.org
|
|| pnpm install --frozen-lockfile --prefer-offline --registry=https://registry.npmjs.org
|
||||||
|
|
||||||
- name: Build Vite application
|
- name: Build Vite application
|
||||||
env:
|
|
||||||
VITE_A_ORIGIN: ${{ secrets.VITE_A_ORIGIN }}
|
|
||||||
run: |
|
run: |
|
||||||
if [ -z "${VITE_A_ORIGIN}" ]; then
|
|
||||||
echo "ℹ️ Secret VITE_A_ORIGIN 未配置,改用 .env.production"
|
|
||||||
else
|
|
||||||
echo "VITE_A_ORIGIN=${VITE_A_ORIGIN}"
|
|
||||||
fi
|
|
||||||
pnpm build
|
pnpm build
|
||||||
echo "✅ Build completed"
|
echo "✅ Build completed"
|
||||||
if [ -d "dist" ] && [ -f "dist/index.html" ]; then
|
if [ -d "dist" ] && [ -f "dist/index.html" ]; then
|
||||||
|
|||||||
+7
-2
@@ -1,5 +1,10 @@
|
|||||||
# CI 中先 pnpm build 产出 dist/,镜像只负责托管静态文件
|
# CI 中先 pnpm build 产出 dist/;运行时由 nginx 托管,并把 /embed /images /api/contact 反代到 A 站
|
||||||
FROM nginx:1.27-alpine
|
FROM nginx:1.27-alpine
|
||||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
|
||||||
|
# 集群内 A 站地址,可被 K8s env 覆盖
|
||||||
|
ENV A_UPSTREAM=http://project57-deploy.ns-project57.svc.cluster.local:3000
|
||||||
|
|
||||||
|
COPY nginx.conf /etc/nginx/templates/default.conf.template
|
||||||
COPY dist /usr/share/nginx/html
|
COPY dist /usr/share/nginx/html
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
@@ -1,22 +1,24 @@
|
|||||||
# project57-x
|
# project57-x
|
||||||
|
|
||||||
B 站宿主:跨域加载 A 站(project57)打包的前台 SPA。
|
B 站宿主:浏览器只访问本站。A 站(project57)**不需要公网域名**。
|
||||||
|
|
||||||
CI 对齐 **project0**(本地 `projectBase`):提交信息带 `[deploy]` 会构建镜像并部署到 K8s。
|
- 页面从 **同源** 加载 `/embed/aurora.js`
|
||||||
本仓是 Vite 静态站,构建产物为 `dist/`,Docker 用 nginx 监听 **3000**,并提供 `/api/health` 给探针。
|
- nginx(生产)或 Vite proxy(本地)把 `/embed`、`/images`、`/api/contact` 转到集群内 A 站
|
||||||
|
- 以后给 B 站绑域名即可,不用改前端
|
||||||
|
|
||||||
仓库 Secrets 需配置(与 project0 相同,外加 A 站地址):
|
CI 对齐 project0:提交带 `[deploy]` 会构建镜像并部署。nginx 监听 **3000**,`/api/health` 给探针。
|
||||||
|
|
||||||
- `LFS_PAT`
|
仓库 Secrets:`LFS_PAT`、`KUBECONFIG_BASE64`。
|
||||||
- `KUBECONFIG_BASE64`
|
|
||||||
- `VITE_A_ORIGIN`(可选):覆盖 `.env.production` 里的 A 站地址
|
|
||||||
- 默认生产源站:`https://swissicapital.net`(加载 `/embed/aurora.js`)
|
|
||||||
|
|
||||||
A 站需已部署嵌入包(`pnpm build` 会先跑 `build:embed`)。
|
K8s 可设置环境变量覆盖 A 站地址(默认已按 CI 命名规则写好):
|
||||||
|
|
||||||
|
```
|
||||||
|
A_UPSTREAM=http://project57-deploy.ns-project57.svc.cluster.local:3000
|
||||||
|
```
|
||||||
|
|
||||||
## 本地运行
|
## 本地运行
|
||||||
|
|
||||||
1. A 站先打包嵌入脚本并启动:
|
1. A 站打包并启动:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd ../project57
|
cd ../project57
|
||||||
@@ -24,13 +26,11 @@ pnpm build:embed
|
|||||||
pnpm dev
|
pnpm dev
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 本仓库安装并启动(默认 http://localhost:4173):
|
2. 本仓库:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm install
|
pnpm install
|
||||||
pnpm dev
|
pnpm dev
|
||||||
```
|
```
|
||||||
|
|
||||||
3. 若 A 站端口不是 `3000`,复制 `.env.example` 为 `.env`,改 `VITE_A_ORIGIN`。
|
默认 http://localhost:4173 ,并代理到 `http://localhost:3000`。A 站端口不同则改 `.env` 里的 `VITE_A_ORIGIN`。
|
||||||
|
|
||||||
页面会加载 `${VITE_A_ORIGIN}/embed/aurora.js`,图片和联系表单也走 A 站。路由为 hash,例如 `http://localhost:4173/#/kontakt`。
|
|
||||||
+25
@@ -10,6 +10,31 @@ server {
|
|||||||
return 200 "ok";
|
return 200 "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# A 站无公网域名:浏览器只访问 B 站,由 B 反代到集群内 project57
|
||||||
|
location /embed/ {
|
||||||
|
proxy_pass ${A_UPSTREAM}/embed/;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
location /images/ {
|
||||||
|
proxy_pass ${A_UPSTREAM}/images/;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
location = /api/contact {
|
||||||
|
proxy_pass ${A_UPSTREAM}/api/contact;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
}
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-10
@@ -1,7 +1,3 @@
|
|||||||
const A_ORIGIN = (
|
|
||||||
import.meta.env.VITE_A_ORIGIN || "https://swissicapital.net"
|
|
||||||
).replace(/\/$/, "");
|
|
||||||
|
|
||||||
const errorEl = document.getElementById("aurora-error");
|
const errorEl = document.getElementById("aurora-error");
|
||||||
|
|
||||||
function showError(message: string) {
|
function showError(message: string) {
|
||||||
@@ -22,25 +18,27 @@ function loadScript(src: string): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function boot() {
|
async function boot() {
|
||||||
|
const origin = window.location.origin;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await loadScript(`${A_ORIGIN}/embed/aurora.js?v=1`);
|
await loadScript(`${origin}/embed/aurora.js?v=1`);
|
||||||
} catch {
|
} catch {
|
||||||
showError(
|
showError(
|
||||||
`无法加载 A 站脚本:${A_ORIGIN}/embed/aurora.js。请先在 project57 执行 pnpm build:embed 并启动 A 站,或修改 .env 中的 VITE_A_ORIGIN。`
|
`无法加载前台脚本:${origin}/embed/aurora.js。请确认 B 站已反代到 A 站,且 A 站已部署 embed 产物。`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const api = window.AuroraSite;
|
const api = window.AuroraSite;
|
||||||
if (!api?.mount) {
|
if (!api?.mount) {
|
||||||
showError("A 站脚本已加载,但未找到 AuroraSite.mount。");
|
showError("脚本已加载,但未找到 AuroraSite.mount。");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
api.mount("#aurora-root", {
|
api.mount("#aurora-root", {
|
||||||
assetBase: A_ORIGIN,
|
assetBase: origin,
|
||||||
apiBase: A_ORIGIN,
|
apiBase: origin,
|
||||||
cssUrl: `${A_ORIGIN}/embed/aurora.css?v=1`,
|
cssUrl: `${origin}/embed/aurora.css?v=1`,
|
||||||
history: "hash",
|
history: "hash",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,24 @@
|
|||||||
import { defineConfig } from "vite";
|
import { defineConfig } from "vite";
|
||||||
|
|
||||||
|
const aOrigin = process.env.VITE_A_ORIGIN || "http://localhost:3000";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
server: {
|
server: {
|
||||||
port: 4173,
|
port: 4173,
|
||||||
strictPort: true,
|
strictPort: true,
|
||||||
|
proxy: {
|
||||||
|
"/embed": aOrigin,
|
||||||
|
"/images": aOrigin,
|
||||||
|
"/api/contact": aOrigin,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
preview: {
|
preview: {
|
||||||
port: 4173,
|
port: 4173,
|
||||||
strictPort: true,
|
strictPort: true,
|
||||||
|
proxy: {
|
||||||
|
"/embed": aOrigin,
|
||||||
|
"/images": aOrigin,
|
||||||
|
"/api/contact": aOrigin,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user