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 }); } }