1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
import { revalidateTag } from 'next/cache';
import { NextRequest, NextResponse } from 'next/server';
/**
* 결재 시스템 캐시 무효화 API
*
* 백그라운드 프로세스(폴링 서비스)에서 request 컨텍스트 없이
* 캐시를 무효화하기 위한 API 라우트
*
* 사용법:
* await fetch('/api/revalidate/approval', {
* method: 'POST',
* headers: { 'Content-Type': 'application/json' },
* body: JSON.stringify({ tags: ['approval-logs'] })
* });
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { tags, secret } = body;
// 선택적 보안: 환경 변수로 시크릿 키 검증
// 내부 서버에서만 호출되므로 필수는 아님
if (process.env.REVALIDATION_SECRET && secret !== process.env.REVALIDATION_SECRET) {
return NextResponse.json(
{ success: false, message: 'Invalid secret' },
{ status: 401 }
);
}
// 캐시 태그 무효화
if (Array.isArray(tags)) {
for (const tag of tags) {
revalidateTag(tag);
console.log(`[Cache Revalidation] Tag revalidated: ${tag}`);
}
} else if (typeof tags === 'string') {
revalidateTag(tags);
console.log(`[Cache Revalidation] Tag revalidated: ${tags}`);
}
return NextResponse.json({
success: true,
revalidated: true,
tags: Array.isArray(tags) ? tags : [tags],
timestamp: new Date().toISOString(),
});
} catch (error) {
console.error('[Cache Revalidation] Error:', error);
return NextResponse.json(
{
success: false,
message: error instanceof Error ? error.message : 'Revalidation failed',
},
{ status: 500 }
);
}
}
// GET 요청으로도 사용 가능 (개발/테스트용)
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const tag = searchParams.get('tag');
const secret = searchParams.get('secret');
if (process.env.REVALIDATION_SECRET && secret !== process.env.REVALIDATION_SECRET) {
return NextResponse.json(
{ success: false, message: 'Invalid secret' },
{ status: 401 }
);
}
if (!tag) {
return NextResponse.json(
{ success: false, message: 'Tag parameter is required' },
{ status: 400 }
);
}
try {
revalidateTag(tag);
console.log(`[Cache Revalidation] Tag revalidated: ${tag}`);
return NextResponse.json({
success: true,
revalidated: true,
tag,
timestamp: new Date().toISOString(),
});
} catch (error) {
console.error('[Cache Revalidation] Error:', error);
return NextResponse.json(
{
success: false,
message: error instanceof Error ? error.message : 'Revalidation failed',
},
{ status: 500 }
);
}
}
|