diff --git a/src/app.ts b/src/app.ts index 65f5a5d..8c1f59f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -9,9 +9,10 @@ import fastifySwagger from "@fastify/swagger"; import fastifySwaggerUi from "@fastify/swagger-ui"; import ejs from "ejs"; -import getRoute from "./routes/browser/get"; -import parseRoute from "./routes/api/parse"; import indexRoute from "./routes/browser/index"; +import getRoute from "./routes/browser/get"; +import proxyRoute from "./routes/browser/proxy"; +import parseRoute from "./routes/api/parse"; import rawHtml from "./routes/api/raw-html"; import publicConfig from "./publicConfig"; @@ -54,6 +55,7 @@ class App { fastify.register(indexRoute); fastify.register(getRoute); + fastify.register(proxyRoute); fastify.register(parseRoute); fastify.register(rawHtml); diff --git a/src/routes/browser/proxy.ts b/src/routes/browser/proxy.ts new file mode 100644 index 0000000..4947abf --- /dev/null +++ b/src/routes/browser/proxy.ts @@ -0,0 +1,18 @@ +import { FastifyInstance } from "fastify"; +import { IProxySchema, ProxySchema } from "../../types/requests/browser"; +import axios from "../../types/axios"; + +export default async function proxyRoute(fastify: FastifyInstance) { + fastify.get( + "/proxy", + { schema: ProxySchema }, + async (request, reply) => { + const response = await axios.get(request.query.url); + const mime: string | undefined = response.headers["content-type"]?.toString(); + const clen: string | undefined = response.headers["content-length"]?.toString(); + mime && reply.header("Content-Type", mime); + clen && reply.header("Content-Length", Number(clen)); + return reply.send(response.data); + } + ); +} diff --git a/src/types/requests/browser.ts b/src/types/requests/browser.ts index e9891ed..4507b11 100644 --- a/src/types/requests/browser.ts +++ b/src/types/requests/browser.ts @@ -6,10 +6,9 @@ export interface IGetSchema { Querystring: IGetQuerySchema; } -export const indexSchema = { - produces: ["text/html"], - hide: true -}; +export interface IProxySchema { + Querystring: IProxyQuerySchema; +} export const getQuerySchema = { type: "object", @@ -32,9 +31,32 @@ export const getQuerySchema = { } as const; export type IGetQuerySchema = FromSchema; +export const proxyQuerySchema = { + type: "object", + required: ["url"], + properties: { + url: { + type: "string", + description: "URL", + }, + } +} as const; +export type IProxyQuerySchema = FromSchema; + +export const indexSchema = { + hide: true, + produces: ["text/html"], +}; + export const GetSchema: FastifySchema = { description: "Get page", hide: true, querystring: getQuerySchema, produces: ["text/html", "text/plain"], }; + +export const ProxySchema: FastifySchema = { + description: "Proxy resource", + hide: true, + querystring: proxyQuerySchema, +}