78 lines
2.5 KiB
TypeScript
Executable File
78 lines
2.5 KiB
TypeScript
Executable File
import { NextRequest, NextResponse } from 'next/server'
|
|
|
|
const WIKI_SERVICE_URL = process.env.WIKI_SERVICE_URL || 'http://wiki:8080'
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ path?: string[] }> }
|
|
) {
|
|
try {
|
|
const resolvedParams = await params
|
|
const path = resolvedParams.path || []
|
|
const searchParams = request.nextUrl.searchParams.toString()
|
|
const queryString = searchParams ? `?${searchParams}` : ''
|
|
const wikiPath = path.length > 0 ? `/${path.join('/')}` : '/'
|
|
|
|
// Ensure no double slashes
|
|
const baseUrl = WIKI_SERVICE_URL.endsWith('/') ? WIKI_SERVICE_URL.slice(0, -1) : WIKI_SERVICE_URL
|
|
const wikiUrl = `${baseUrl}${wikiPath}${queryString}`
|
|
|
|
console.log(`[Wiki Proxy] Fetching: ${wikiUrl}`)
|
|
|
|
const response = await fetch(wikiUrl, {
|
|
headers: {
|
|
'User-Agent': request.headers.get('user-agent') || 'Next.js',
|
|
},
|
|
// Add timeout
|
|
signal: AbortSignal.timeout(10000), // 10 second timeout
|
|
})
|
|
|
|
if (!response.ok) {
|
|
console.error(`Wiki service returned ${response.status}: ${response.statusText} for ${wikiUrl}`)
|
|
return new NextResponse(`Wiki not found: ${response.status} ${response.statusText}`, {
|
|
status: response.status
|
|
})
|
|
}
|
|
|
|
const contentType = response.headers.get('content-type') || 'text/html'
|
|
const content = await response.text()
|
|
|
|
// Pass through headers
|
|
const headers = new Headers()
|
|
headers.set('Content-Type', contentType)
|
|
|
|
// Handle CORS if needed
|
|
headers.set('Access-Control-Allow-Origin', '*')
|
|
|
|
// Preserve cache headers
|
|
const cacheControl = response.headers.get('cache-control')
|
|
if (cacheControl) {
|
|
headers.set('Cache-Control', cacheControl)
|
|
}
|
|
|
|
return new NextResponse(content, {
|
|
status: response.status,
|
|
headers,
|
|
})
|
|
} catch (error: any) {
|
|
console.error('Wiki proxy error:', error)
|
|
return new NextResponse(`Error loading wiki: ${error.message || 'Unknown error'}`, {
|
|
status: 500
|
|
})
|
|
}
|
|
}
|
|
|
|
// Handle other HTTP methods
|
|
export async function POST(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
|
|
return GET(request, { params })
|
|
}
|
|
|
|
export async function PUT(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
|
|
return GET(request, { params })
|
|
}
|
|
|
|
export async function DELETE(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
|
|
return GET(request, { params })
|
|
}
|
|
|