Proxying
This commit is contained in:
parent
1fc1d8e88b
commit
47e4db1fc8
@ -9,9 +9,10 @@ import fastifySwagger from "@fastify/swagger";
|
|||||||
import fastifySwaggerUi from "@fastify/swagger-ui";
|
import fastifySwaggerUi from "@fastify/swagger-ui";
|
||||||
import ejs from "ejs";
|
import ejs from "ejs";
|
||||||
|
|
||||||
import getRoute from "./routes/browser/get";
|
|
||||||
import parseRoute from "./routes/api/parse";
|
|
||||||
import indexRoute from "./routes/browser/index";
|
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 rawHtml from "./routes/api/raw-html";
|
||||||
|
|
||||||
import publicConfig from "./publicConfig";
|
import publicConfig from "./publicConfig";
|
||||||
@ -54,6 +55,7 @@ class App {
|
|||||||
|
|
||||||
fastify.register(indexRoute);
|
fastify.register(indexRoute);
|
||||||
fastify.register(getRoute);
|
fastify.register(getRoute);
|
||||||
|
fastify.register(proxyRoute);
|
||||||
fastify.register(parseRoute);
|
fastify.register(parseRoute);
|
||||||
fastify.register(rawHtml);
|
fastify.register(rawHtml);
|
||||||
|
|
||||||
|
18
src/routes/browser/proxy.ts
Normal file
18
src/routes/browser/proxy.ts
Normal file
@ -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<IProxySchema>(
|
||||||
|
"/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);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
@ -6,10 +6,9 @@ export interface IGetSchema {
|
|||||||
Querystring: IGetQuerySchema;
|
Querystring: IGetQuerySchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const indexSchema = {
|
export interface IProxySchema {
|
||||||
produces: ["text/html"],
|
Querystring: IProxyQuerySchema;
|
||||||
hide: true
|
}
|
||||||
};
|
|
||||||
|
|
||||||
export const getQuerySchema = {
|
export const getQuerySchema = {
|
||||||
type: "object",
|
type: "object",
|
||||||
@ -32,9 +31,32 @@ export const getQuerySchema = {
|
|||||||
} as const;
|
} as const;
|
||||||
export type IGetQuerySchema = FromSchema<typeof getQuerySchema>;
|
export type IGetQuerySchema = FromSchema<typeof getQuerySchema>;
|
||||||
|
|
||||||
|
export const proxyQuerySchema = {
|
||||||
|
type: "object",
|
||||||
|
required: ["url"],
|
||||||
|
properties: {
|
||||||
|
url: {
|
||||||
|
type: "string",
|
||||||
|
description: "URL",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} as const;
|
||||||
|
export type IProxyQuerySchema = FromSchema<typeof proxyQuerySchema>;
|
||||||
|
|
||||||
|
export const indexSchema = {
|
||||||
|
hide: true,
|
||||||
|
produces: ["text/html"],
|
||||||
|
};
|
||||||
|
|
||||||
export const GetSchema: FastifySchema = {
|
export const GetSchema: FastifySchema = {
|
||||||
description: "Get page",
|
description: "Get page",
|
||||||
hide: true,
|
hide: true,
|
||||||
querystring: getQuerySchema,
|
querystring: getQuerySchema,
|
||||||
produces: ["text/html", "text/plain"],
|
produces: ["text/html", "text/plain"],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const ProxySchema: FastifySchema = {
|
||||||
|
description: "Proxy resource",
|
||||||
|
hide: true,
|
||||||
|
querystring: proxyQuerySchema,
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user