diff --git a/.env.example b/.env.example index bf7a0be..4550698 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,7 @@ POSTGRES_PASSWORD=postgres POSTGRES_DB=personal_brand POSTGRES_PORT=5432 API_PORT=3001 +API_PREFIX= ADMIN_PORT=3000 SESSION_SECRET=replace-with-a-long-random-value DEFAULT_BRAND_ID=00000000-0000-0000-0000-000000000001 diff --git a/apps/admin/.env.example b/apps/admin/.env.example new file mode 100644 index 0000000..16f18e9 --- /dev/null +++ b/apps/admin/.env.example @@ -0,0 +1,4 @@ +VITE_APP_BASE_PATH=/admin/ +VITE_API_BASE_URL=/api +VITE_DEV_API_TARGET=http://localhost:3001 +ADMIN_PORT=3000 diff --git a/apps/admin/src/lib/api.ts b/apps/admin/src/lib/api.ts index c19edb1..8d04539 100644 --- a/apps/admin/src/lib/api.ts +++ b/apps/admin/src/lib/api.ts @@ -1,6 +1,6 @@ import { ApiClient } from "@personal-brand/api-client"; -export const apiBaseUrl = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:3001"; +export const apiBaseUrl = import.meta.env.VITE_API_BASE_URL ?? "/api"; export async function authenticatedFetch( input: RequestInfo | URL, init?: RequestInit, diff --git a/apps/admin/src/vite-env.d.ts b/apps/admin/src/vite-env.d.ts index eb29c32..dc8570a 100644 --- a/apps/admin/src/vite-env.d.ts +++ b/apps/admin/src/vite-env.d.ts @@ -1,5 +1,6 @@ interface ImportMetaEnv { readonly VITE_API_BASE_URL?: string; + readonly VITE_APP_BASE_PATH?: string; } interface ImportMeta { diff --git a/apps/admin/vite.config.ts b/apps/admin/vite.config.ts index e35a410..ee60e48 100644 --- a/apps/admin/vite.config.ts +++ b/apps/admin/vite.config.ts @@ -1,4 +1,18 @@ import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; -export default defineConfig({ plugins: [react()], build: { outDir: "dist" } }); +export default defineConfig({ + plugins: [react()], + base: process.env.VITE_APP_BASE_PATH ?? "/", + server: { + host: "0.0.0.0", + port: Number(process.env.ADMIN_PORT ?? 3000), + proxy: { + "/api": { + target: process.env.VITE_DEV_API_TARGET ?? "http://localhost:3001", + rewrite: (path) => path.replace(/^\/api/, ""), + }, + }, + }, + build: { outDir: "dist" }, +}); diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index f6c086e..b86cf62 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -458,6 +458,8 @@ export async function createApp(): Promise { new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }), ); app.useGlobalFilters(new ApiExceptionFilter()); + const apiPrefix = process.env.API_PREFIX?.replace(/^\/+|\/+$/g, ""); + if (apiPrefix) app.setGlobalPrefix(apiPrefix); app.use(new RequestIdMiddleware().use.bind(new RequestIdMiddleware())); app.enableCors(); return app; diff --git a/docs/deployment.md b/docs/deployment.md new file mode 100644 index 0000000..0ba85c5 --- /dev/null +++ b/docs/deployment.md @@ -0,0 +1,30 @@ +# 部署说明 + +## 本地开发 + +API 默认监听 `http://localhost:3001`,管理后台默认监听 `http://localhost:3000`。后台开发服务器会把 `/api` 请求代理到 API,并移除 `/api` 前缀,因此本地不需要额外配置跨域。 + +## Nginx 反向代理 + +生产环境建议使用同源路径: + +```nginx +location /admin/ { + try_files $uri $uri/ /admin/index.html; +} + +location /api/ { + proxy_pass http://127.0.0.1:3001/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Request-ID $request_id; +} +``` + +构建管理后台时使用: + +```bash +VITE_APP_BASE_PATH=/admin/ VITE_API_BASE_URL=/api pnpm --filter @personal-brand/admin build +``` + +如果希望 API 服务自身也带前缀,可设置 `API_PREFIX=api`。使用 Nginx 去前缀转发时保持默认空值即可,避免重复 `/api/api`。