55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { reconstructPortfolioHistory } from '@/actions/snapshots';
|
|
import { revalidatePath } from 'next/cache';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
export const fetchCache = 'force-no-store';
|
|
export const runtime = 'nodejs';
|
|
export const maxDuration = 3600;
|
|
|
|
export async function POST(req: Request) {
|
|
const rebuildSecret = process.env.REBUILD_SECRET || process.env.CRON_SECRET;
|
|
const authHeader = req.headers.get('Authorization');
|
|
|
|
if (!rebuildSecret) {
|
|
return NextResponse.json(
|
|
{ error: 'REBUILD_SECRET or CRON_SECRET not configured' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
if (authHeader !== `Bearer ${rebuildSecret}`) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
console.log('[Rebuild Snapshots] Starting full rebuild...');
|
|
|
|
const result = await reconstructPortfolioHistory();
|
|
|
|
console.log('[Rebuild Snapshots] Rebuild complete:', result);
|
|
|
|
// 清除整个大盘页面的所有服务端缓存
|
|
revalidatePath('/', 'layout');
|
|
revalidatePath('/dashboard', 'page');
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '历史快照全量重建完成',
|
|
daysReconstructed: result.daysReconstructed,
|
|
});
|
|
} catch (error) {
|
|
console.error('[Rebuild Snapshots] Rebuild failed:', error);
|
|
return NextResponse.json(
|
|
{
|
|
error: '重建失败',
|
|
details: String(error),
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|