refactor: 更新环境配置与 API 代理逻辑
- 修改 .env.example,优化玩家端配置说明,明确本地与线上环境的使用方式。 - 重构 API 代理逻辑,移除过时的开发代理文件,统一使用 Next.js 代理 API 请求。 - 更新 CSP 配置,简化连接来源说明,提升安全性。 - 调整 lottery-api-base.ts 中的注释,增强代码可读性。
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { type NextRequest } from "next/server";
|
||||
|
||||
import { proxyLotteryApiInDev } from "@/lib/lottery-api-dev-proxy";
|
||||
import { proxyLotteryApi } from "@/lib/lottery-api-proxy";
|
||||
|
||||
type RouteContext = { params: Promise<{ path: string[] }> };
|
||||
|
||||
async function handle(request: NextRequest, context: RouteContext) {
|
||||
const { path } = await context.params;
|
||||
return proxyLotteryApiInDev(request, path);
|
||||
return proxyLotteryApi(request, path);
|
||||
}
|
||||
|
||||
export const GET = handle;
|
||||
|
||||
@@ -65,7 +65,7 @@ export function generateCSP(extraParentOrigins: string[] = []): string {
|
||||
// 字体允许同源
|
||||
"font-src": ["'self'"],
|
||||
|
||||
// 连接允许同源;线上 API 由宝塔挂在同源 /api 下
|
||||
// 连接允许同源;API 由 Next 代理到 LOTTERY_API_UPSTREAM
|
||||
"connect-src": [
|
||||
"'self'",
|
||||
// WebSocket 连接
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const DEFAULT_LOTTERY_API_ORIGIN = "http://127.0.0.1:8000";
|
||||
|
||||
/** Laravel 根地址(无尾部 `/`),仅供 Next 本地开发代理与 middleware 服务端请求。 */
|
||||
/** Laravel 根地址(无尾部 `/`),供 Next 代理与 middleware 服务端请求。 */
|
||||
export function lotteryApiOrigin(): string {
|
||||
const configured = process.env.LOTTERY_API_UPSTREAM?.trim();
|
||||
return (configured || DEFAULT_LOTTERY_API_ORIGIN).replace(/\/$/, "");
|
||||
@@ -9,8 +9,7 @@ export function lotteryApiOrigin(): string {
|
||||
/**
|
||||
* axios `baseURL`:
|
||||
* - 浏览器始终请求同源 `/api/v1`
|
||||
* - 线上由宝塔转发 `/api/*` 到 Laravel
|
||||
* - 本地开发由 `app/api/[...path]/route.ts` 临时代理到 `LOTTERY_API_UPSTREAM`
|
||||
* - Next `app/api/[...path]/route.ts` 转发到 `LOTTERY_API_UPSTREAM`(本地/线上同一套)
|
||||
*/
|
||||
export function resolveLotteryApiV1Base(): string {
|
||||
return "/api/v1";
|
||||
|
||||
@@ -20,15 +20,11 @@ const STRIP_RESPONSE_HEADERS = [
|
||||
"content-length",
|
||||
];
|
||||
|
||||
/** 本地开发:/api/* → Laravel(Turbopack 下 next.config rewrites 常不生效)。 */
|
||||
export async function proxyLotteryApiInDev(
|
||||
/** 浏览器请求同源 `/api/*`,Next 转发到 `LOTTERY_API_UPSTREAM`(本地/线上同一套)。 */
|
||||
export async function proxyLotteryApi(
|
||||
request: NextRequest,
|
||||
pathSegments: string[],
|
||||
): Promise<NextResponse> {
|
||||
if (process.env.NODE_ENV !== "development") {
|
||||
return NextResponse.json({ msg: "Not Found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const path = pathSegments.join("/");
|
||||
const target = `${lotteryApiOrigin()}/api/${path}${request.nextUrl.search}`;
|
||||
|
||||
Reference in New Issue
Block a user