Skip to content

Commit 78a952e

Browse files
committed
refactor: consolidate reissue API methods into a single handler and improve warm-up logic
1 parent f1c4ab4 commit 78a952e

File tree

1 file changed

+64
-62
lines changed

1 file changed

+64
-62
lines changed

apps/ticket/src/app/api/reissue/route.ts

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,81 @@ import { NextResponse } from "next/server";
33

44
import { API_URL } from "@/data/constants";
55

6-
export async function HEAD() {
7-
return new Response(null, { status: 200 });
8-
}
9-
10-
/**
11-
* vercel Lambda 깨우기 용 ping API (5분 주기로 호출)
12-
* 브라우저에서 주기적으로 호출하여 Lambda 깨우기 (Cold Start 방지)
13-
*
14-
* NOTE:
15-
* vercel cron 에서 무료 요금제로는 최소 하루 단위로 호출이 가능하여
16-
* 외부 서비스 UptimeRobot 사용
17-
*
18-
* @see https://dashboard.uptimerobot.com/monitors/802448042
19-
*/
20-
export async function GET(req: Request) {
21-
const { searchParams } = new URL(req.url);
22-
23-
if (searchParams.get("warm") === "y") {
24-
// 내부 POST 호출 → POST handler까지 깨우기
25-
await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/reissue?warm=y`, {
26-
method: "POST",
27-
});
28-
29-
return new Response("warm", { status: 200 });
6+
export async function handler(req: Request) {
7+
if (req.method === "HEAD") {
8+
return new Response(null, { status: 200 });
309
}
3110

32-
return new Response("Method Not Allowed!", { status: 405 });
33-
}
34-
35-
export async function POST(req: Request) {
36-
const { searchParams } = new URL(req.url);
37-
38-
// warm 호출이면 실제 로직 skip
39-
if (searchParams.get("warm") === "y") {
40-
return NextResponse.json({ warm: true });
11+
/**
12+
* vercel Lambda 깨우기 용 ping API (5분 주기로 호출)
13+
* 브라우저에서 주기적으로 호출하여 Lambda 깨우기 (Cold Start 방지)
14+
*
15+
* NOTE:
16+
* vercel cron 에서 무료 요금제로는 최소 하루 단위로 호출이 가능하여
17+
* 외부 서비스 UptimeRobot 사용
18+
*
19+
* @see https://dashboard.uptimerobot.com/monitors/802448042
20+
*/
21+
if (req.method === "GET") {
22+
const { searchParams } = new URL(req.url);
23+
24+
if (searchParams.get("warm") === "y") {
25+
// 내부 POST 호출 → POST handler까지 깨우기
26+
await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/reissue?warm=y`, {
27+
method: "POST",
28+
});
29+
30+
return new Response("warm", { status: 200 });
31+
}
32+
33+
return new Response("Method Not Allowed!", { status: 405 });
4134
}
4235

43-
const cookiesStore = await cookies();
44-
45-
const apiRes = await fetch(
46-
process.env.NEXT_PUBLIC_TICKET_API_BASE_URL + API_URL.USER.REISSUE_ACCESS_TOKEN,
47-
{
48-
method: "POST",
49-
headers: {
50-
"Content-Type": "application/json",
51-
Cookie: `accessToken=${cookiesStore.get("accessToken")?.value}; refreshToken=${cookiesStore.get("refreshToken")?.value}`,
36+
if (req.method === "POST") {
37+
const { searchParams } = new URL(req.url);
38+
39+
// warm 호출이면 실제 로직 skip
40+
if (searchParams.get("warm") === "y") {
41+
return NextResponse.json({ warm: true });
42+
}
43+
44+
const cookiesStore = await cookies();
45+
46+
const apiRes = await fetch(
47+
process.env.NEXT_PUBLIC_TICKET_API_BASE_URL + API_URL.USER.REISSUE_ACCESS_TOKEN,
48+
{
49+
method: "POST",
50+
headers: {
51+
"Content-Type": "application/json",
52+
Cookie: `accessToken=${cookiesStore.get("accessToken")?.value}; refreshToken=${cookiesStore.get("refreshToken")?.value}`,
53+
},
54+
credentials: "include",
5255
},
53-
credentials: "include",
54-
},
55-
);
56+
);
57+
58+
const setCookies = apiRes.headers.getSetCookie();
5659

57-
const setCookies = apiRes.headers.getSetCookie();
60+
const res = NextResponse.json(await apiRes.json(), {
61+
status: apiRes.status,
62+
});
5863

59-
const res = NextResponse.json(await apiRes.json(), {
60-
status: apiRes.status,
61-
});
64+
// API 서버가 내려준 모든 Set-Cookie를 그대로 전달
65+
for (const cookie of setCookies) {
66+
// 기존 쿠키 그대로 전달
67+
// SSR 환경에서 브라우저 쿠키 사용할 수 있도록 하기 위함
68+
res.headers.append("Set-Cookie", cookie);
6269

63-
// API 서버가 내려준 모든 Set-Cookie를 그대로 전달
64-
for (const cookie of setCookies) {
65-
// 기존 쿠키 그대로 전달
66-
// SSR 환경에서 브라우저 쿠키 사용할 수 있도록 하기 위함
67-
res.headers.append("Set-Cookie", cookie);
70+
// Domain 추가한 쿠키 한 번 더 전달
71+
// 브라우저 단에서 자동으로 쿠키가 포함한 요청이 갈 수 있도록 하기 위함
72+
res.headers.append("Set-Cookie", `${cookie}; Domain=.permitseoul.com`);
73+
}
6874

69-
// Domain 추가한 쿠키 한 번 더 전달
70-
// 브라우저 단에서 자동으로 쿠키가 포함한 요청이 갈 수 있도록 하기 위함
71-
res.headers.append("Set-Cookie", `${cookie}; Domain=.permitseoul.com`);
72-
}
75+
if (!res.ok) {
76+
clearAuthCookies(res);
77+
}
7378

74-
if (!res.ok) {
75-
clearAuthCookies(res);
79+
return res;
7680
}
77-
78-
return res;
7981
}
8082

8183
function clearAuthCookies(res: NextResponse) {

0 commit comments

Comments
 (0)