107 lines
3.1 KiB
TypeScript
107 lines
3.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { cookies } from "next/headers";
|
|
import { errorToResponse } from "@kri/common/http";
|
|
import { createServerApi } from "@/lib/http-server";
|
|
import { readReportBatch } from "@/lib/report-contract";
|
|
import {
|
|
isProjectReadPath,
|
|
projectReadQuery,
|
|
isReportStreamPath,
|
|
} from "@/lib/project-proxy-policy";
|
|
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
|
import type { RouteContext } from "@/types/common";
|
|
|
|
export async function GET(request: NextRequest, context: RouteContext) {
|
|
const { slug = [] } = await context.params;
|
|
if (!isProjectReadPath(slug))
|
|
return NextResponse.json(
|
|
{ message: "지원하지 않는 경로입니다." },
|
|
{ status: 404 },
|
|
);
|
|
if (!(await cookies()).get(CookieKeys.AuthToken)?.value)
|
|
return NextResponse.json(
|
|
{ message: "로그인이 필요합니다." },
|
|
{ status: 401 },
|
|
);
|
|
try {
|
|
const serverApi = await createServerApi();
|
|
const result = await serverApi.get<unknown>(
|
|
"/project/" + slug.map(encodeURIComponent).join("/"),
|
|
{
|
|
query: projectReadQuery(slug, request.nextUrl.searchParams),
|
|
cache: "no-store",
|
|
},
|
|
);
|
|
return NextResponse.json(result, {
|
|
headers: { "Cache-Control": "no-store" },
|
|
});
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
}
|
|
|
|
export const maxDuration = 660;
|
|
|
|
export async function POST(request: NextRequest, context: RouteContext) {
|
|
const { slug = [] } = await context.params;
|
|
if (!isReportStreamPath(slug))
|
|
return NextResponse.json(
|
|
{ message: "지원하지 않는 경로입니다." },
|
|
{ status: 404 },
|
|
);
|
|
if (!(await cookies()).get(CookieKeys.AuthToken)?.value)
|
|
return NextResponse.json(
|
|
{ message: "로그인이 필요합니다." },
|
|
{ status: 401 },
|
|
);
|
|
let body;
|
|
try {
|
|
body = readReportBatch(await request.json());
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{
|
|
message:
|
|
error instanceof SyntaxError
|
|
? "집계 요청을 확인해 주세요."
|
|
: error instanceof Error
|
|
? error.message
|
|
: "집계 조건을 확인해 주세요.",
|
|
},
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
try {
|
|
const serverApi = await createServerApi();
|
|
const upstream = await serverApi.streamPost(
|
|
"/project/" + slug.map(encodeURIComponent).join("/"),
|
|
body,
|
|
{
|
|
signal: request.signal,
|
|
timeoutMs: 600_000,
|
|
idleTimeoutMs: 600_000,
|
|
headers: { Accept: "application/x-ndjson" },
|
|
cache: "no-store",
|
|
},
|
|
);
|
|
if (
|
|
!upstream.body ||
|
|
!upstream.headers.get("content-type")?.includes("application/x-ndjson")
|
|
) {
|
|
await upstream.body?.cancel();
|
|
return NextResponse.json(
|
|
{ message: "집계 스트림 응답을 확인해 주세요." },
|
|
{ status: 502 },
|
|
);
|
|
}
|
|
return new Response(upstream.body, {
|
|
headers: {
|
|
"Content-Type": "application/x-ndjson; charset=utf-8",
|
|
"Cache-Control": "no-store",
|
|
"X-Accel-Buffering": "no",
|
|
},
|
|
});
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
}
|