created proxy for parser backend
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export const PROXY_PATHS = {
|
||||
saveParserBackend: '/api/proxy/saveParserBackend',
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
/// Resend request to backend.
|
||||
/// Body is being sent as stream.
|
||||
export async function redirectRequest(
|
||||
req: NextRequest,
|
||||
proxyUrl: string,
|
||||
backendBaseUrl: string
|
||||
): Promise<NextResponse> {
|
||||
const reqUrl = req.url // full URL e.g. http://localhost:3000/api/proxy?id=123&foo=bar
|
||||
|
||||
// Find position of proxyPath in reqUrl
|
||||
const proxyIndex = reqUrl.indexOf(proxyUrl)
|
||||
if (proxyIndex === -1) {
|
||||
return new NextResponse('Invalid proxy path', { status: 400 })
|
||||
}
|
||||
|
||||
// Replace '/api/proxy/parserBackend' with backend URL, preserve everything after
|
||||
// e.g. '/api/proxy/extra/path?id=123' -> 'http://your-backend-server/extra/path?id=123'
|
||||
const backendUrl = backendBaseUrl + reqUrl.substring(proxyIndex + proxyUrl.length)
|
||||
console.log('redirecting', req.url, 'to', backendUrl)
|
||||
|
||||
const backendRes = await fetch(backendUrl, {
|
||||
method: req.method,
|
||||
headers: req.headers,
|
||||
body: req.body,
|
||||
// @ts-ignore
|
||||
duplex: 'half', // some property required by nodejs but not defined in @types/node
|
||||
})
|
||||
|
||||
return new NextResponse(backendRes.body, {
|
||||
status: backendRes.status,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Content-Type': backendRes.headers.get('content-type') || 'application/octet-stream',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Browsers send OPTIONS request sometimes to check webserver allowed headers and methods.
|
||||
/// My backend doesn't support such requests so use proxy to answer to it that all headers and methods are allowed
|
||||
export async function responseToOPTIONS(req: NextRequest): Promise<NextResponse> {
|
||||
console.log('proxy responding to CORS OPTIONS', req.url)
|
||||
|
||||
return new NextResponse(null, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, HEAD',
|
||||
'Access-Control-Allow-Headers':
|
||||
req.headers.get('access-control-request-headers') || 'Content-Type, Authorization',
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { redirectRequest, responseToOPTIONS as respondToOPTIONS } from '@/app/api/proxy/proxyFunctions'
|
||||
import { appConfig } from '@/app/lib/configLoader'
|
||||
import { PROXY_PATHS } from '../../paths'
|
||||
|
||||
async function redirect(req: NextRequest): Promise<NextResponse> {
|
||||
return await redirectRequest(req, PROXY_PATHS.saveParserBackend, appConfig.services.saveParserBackend.url)
|
||||
}
|
||||
|
||||
export const GET = redirect
|
||||
export const POST = redirect
|
||||
export const PUT = redirect
|
||||
export const PATCH = redirect
|
||||
export const DELETE = redirect
|
||||
export const HEAD = redirect
|
||||
export const OPTIONS = respondToOPTIONS
|
||||
Reference in New Issue
Block a user