mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-02-21 00:44:41 +08:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
|
|
import { getAvailableApiSites, getCacheTime } from '@/lib/config';
|
|
import { searchFromApi } from '@/lib/downstream';
|
|
|
|
export const runtime = 'edge';
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const query = searchParams.get('q');
|
|
|
|
if (!query) {
|
|
const cacheTime = await getCacheTime();
|
|
return NextResponse.json(
|
|
{ results: [] },
|
|
{
|
|
headers: {
|
|
'Cache-Control': `public, max-age=${cacheTime}`,
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
const apiSites = await getAvailableApiSites();
|
|
const searchPromises = apiSites.map((site) => searchFromApi(site, query));
|
|
|
|
try {
|
|
const results = await Promise.all(searchPromises);
|
|
const flattenedResults = results.flat();
|
|
const cacheTime = getCacheTime();
|
|
|
|
return NextResponse.json(
|
|
{ results: flattenedResults },
|
|
{
|
|
headers: {
|
|
'Cache-Control': `public, max-age=${cacheTime}`,
|
|
},
|
|
}
|
|
);
|
|
} catch (error) {
|
|
return NextResponse.json({ error: '搜索失败' }, { status: 500 });
|
|
}
|
|
}
|