|
1 | 1 | import { NextResponse } from 'next/server' |
2 | | -import { DiscoveredPage } from '@/lib/types' |
| 2 | +import { DiscoveredPage, CrawlRequest } from '@/lib/types' // Added CrawlRequest |
3 | 3 |
|
4 | 4 | export async function POST(request: Request) { |
5 | 5 | try { |
6 | | - const { pages } = await request.json() |
| 6 | + // Extract pages and job_id from the request |
| 7 | + const { pages, job_id }: CrawlRequest = await request.json() |
7 | 8 |
|
8 | | - if (!Array.isArray(pages)) { |
| 9 | + // Validate input |
| 10 | + if (!Array.isArray(pages) || !job_id) { |
9 | 11 | return NextResponse.json( |
10 | | - { error: 'Pages array is required' }, |
| 12 | + { error: 'Pages array and job_id are required' }, |
11 | 13 | { status: 400 } |
12 | 14 | ) |
13 | 15 | } |
14 | 16 |
|
15 | | - // TODO: Replace with actual Crawl4AI Python backend call |
16 | | - // For now, return mock markdown data for testing the UI |
17 | | - const mockMarkdown = `# Documentation |
18 | | -${pages.map((page: DiscoveredPage) => ` |
19 | | -## ${page.title || 'Untitled Page'} |
20 | | -URL: ${page.url} |
21 | | -
|
22 | | -This is mock content for ${page.title || 'this page'}. |
23 | | -It will be replaced with actual crawled content from the Crawl4AI backend. |
24 | | -
|
25 | | ----`).join('\n')} |
26 | | -` |
27 | | - |
28 | | - // Simulate network delay and processing time |
29 | | - await new Promise(resolve => setTimeout(resolve, 2000)) |
30 | | - |
31 | | - return NextResponse.json({ |
32 | | - markdown: mockMarkdown, |
33 | | - stats: { |
34 | | - pagesCrawled: pages.length, |
35 | | - totalWords: mockMarkdown.split(/\s+/).length, |
36 | | - dataSize: `${Math.round(mockMarkdown.length / 1024)} KB` |
37 | | - } |
38 | | - }) |
| 17 | + // Define backend URL |
| 18 | + const INTERNAL_BACKEND_URL = process.env.BACKEND_URL || 'http://backend:24125'; |
| 19 | + const backendCrawlUrl = `${INTERNAL_BACKEND_URL}/api/crawl`; |
| 20 | + |
| 21 | + console.log(`Proxying crawl request for job_id: ${job_id} to ${backendCrawlUrl} with ${pages.length} pages.`); |
| 22 | + |
| 23 | + // Make the actual backend call |
| 24 | + const response = await fetch(backendCrawlUrl, { |
| 25 | + method: 'POST', |
| 26 | + headers: { |
| 27 | + 'Content-Type': 'application/json', |
| 28 | + }, |
| 29 | + body: JSON.stringify({ pages, job_id }), |
| 30 | + }); |
| 31 | + |
| 32 | + // Handle backend error response |
| 33 | + if (!response.ok) { |
| 34 | + const errorText = await response.text(); |
| 35 | + console.error(`Backend crawl request failed for job_id: ${job_id}. Status: ${response.status}, Body: ${errorText}`); |
| 36 | + return NextResponse.json( |
| 37 | + { error: `Backend request failed: ${response.statusText}`, details: errorText }, |
| 38 | + { status: response.status } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // Parse and forward the successful backend response |
| 43 | + const backendData = await response.json(); |
| 44 | + console.log(`Backend crawl request successful for job_id: ${job_id}. Response:`, backendData); |
| 45 | + return NextResponse.json(backendData); // Forward backend response directly |
| 46 | + |
39 | 47 | } catch (error) { |
40 | | - console.error('Error in crawl route:', error) |
| 48 | + console.error('Error in Next.js /api/crawl route:', error) |
| 49 | + // Ensure error is an instance of Error before accessing message |
| 50 | + const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred'; |
41 | 51 | return NextResponse.json( |
42 | | - { error: 'Failed to crawl pages' }, |
| 52 | + { error: 'Failed to proxy crawl request', details: errorMessage }, |
43 | 53 | { status: 500 } |
44 | 54 | ) |
45 | 55 | } |
|
0 commit comments