feat: 添加 CORS 支持,处理预检请求并更新 API 响应头
This commit is contained in:
@@ -1,23 +1,31 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import { getAvailableApiSites, getCacheTime } from '@/lib/config';
|
||||
import { addCorsHeaders, handleOptionsRequest } from '@/lib/cors';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
// 处理OPTIONS预检请求(OrionTV客户端需要)
|
||||
export async function OPTIONS() {
|
||||
return handleOptionsRequest();
|
||||
}
|
||||
|
||||
// OrionTV 兼容接口
|
||||
export async function GET() {
|
||||
try {
|
||||
const apiSites = await getAvailableApiSites();
|
||||
const cacheTime = await getCacheTime();
|
||||
|
||||
return NextResponse.json(apiSites, {
|
||||
const response = NextResponse.json(apiSites, {
|
||||
headers: {
|
||||
'Cache-Control': `public, max-age=${cacheTime}, s-maxage=${cacheTime}`,
|
||||
'CDN-Cache-Control': `public, s-maxage=${cacheTime}`,
|
||||
'Vercel-CDN-Cache-Control': `public, s-maxage=${cacheTime}`,
|
||||
},
|
||||
});
|
||||
return addCorsHeaders(response);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: '获取资源失败' }, { status: 500 });
|
||||
const response = NextResponse.json({ error: '获取资源失败' }, { status: 500 });
|
||||
return addCorsHeaders(response);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user