refactor(env, api, i18n): 更新环境配置与API代理逻辑
修改 .env.example,优化API配置说明并明确线上环境要求。更新多语言错误提示,确保用户在未启用API代理时获得清晰反馈。重构API代理逻辑,移除开发代理文件,简化代码结构,提升可维护性。
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;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"submit": "Log in",
|
||||
"submitting": "Signing in…",
|
||||
"captchaLoadFailed": "Failed to load captcha. Check the API or network.",
|
||||
"apiBaseMissingToast": "API proxy is not enabled: make sure /api is forwarded to Laravel",
|
||||
"apiBaseMissingToast": "API proxy is not enabled: check LOTTERY_API_UPSTREAM points to Laravel",
|
||||
"captchaRequired": "Refresh the captcha first",
|
||||
"welcome": "Welcome, {{name}}",
|
||||
"networkFailed": "Network request failed",
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"submit": "लगइन",
|
||||
"submitting": "लगइन हुँदैछ…",
|
||||
"captchaLoadFailed": "क्याप्चा लोड गर्न सकिएन। API वा नेटवर्क जाँच गर्नुहोस्।",
|
||||
"apiBaseMissingToast": "API proxy सक्षम छैन: /api Laravel मा forward भएको छ कि छैन जाँच गर्नुहोस्",
|
||||
"apiBaseMissingToast": "API proxy सक्षम छैन: LOTTERY_API_UPSTREAM Laravel तर्फ छ कि छैन जाँच गर्नुहोस्",
|
||||
"captchaRequired": "पहिले क्याप्चा रिफ्रेस गर्नुहोस्",
|
||||
"welcome": "स्वागत छ, {{name}}",
|
||||
"networkFailed": "नेटवर्क अनुरोध असफल भयो",
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"submit": "登录",
|
||||
"submitting": "登录中…",
|
||||
"captchaLoadFailed": "无法获取验证码,请检查接口或网络",
|
||||
"apiBaseMissingToast": "API 代理未启用:请确认宝塔或本地 Next 已转发 /api 到 Laravel",
|
||||
"apiBaseMissingToast": "API 代理未启用:请检查 LOTTERY_API_UPSTREAM 是否指向 Laravel",
|
||||
"captchaRequired": "请先刷新验证码",
|
||||
"welcome": "欢迎,{{name}}",
|
||||
"networkFailed": "网络请求失败",
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
const DEFAULT_LOTTERY_API_ORIGIN = "http://127.0.0.1:8000";
|
||||
|
||||
/** Laravel 根地址(无尾部 `/`),仅供 Next 本地开发代理使用。 */
|
||||
/** Laravel 根地址(无尾部 `/`),供 Next 代理与 middleware 服务端请求。 */
|
||||
export function lotteryApiOrigin(): string {
|
||||
const configured =
|
||||
process.env.LOTTERY_API_UPSTREAM?.trim() ||
|
||||
process.env.NEXT_PUBLIC_LOTTERY_API_BASE_URL?.trim();
|
||||
const configured = process.env.LOTTERY_API_UPSTREAM?.trim();
|
||||
return (configured || DEFAULT_LOTTERY_API_ORIGIN).replace(/\/$/, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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