feat: engine choose

This commit is contained in:
Artemy 2023-08-14 15:02:52 +03:00
parent 219e3ee716
commit 5325941078
2 changed files with 10 additions and 5 deletions

View File

@ -1,7 +1,14 @@
import { IHandlerOutput } from "./handler.interface"; import { IHandlerOutput } from "./handler.interface";
import { readability } from "./readability"; import { readability } from "./readability";
export default function handlePage(url: string): Promise<IHandlerOutput> { export default function handlePage(
url: string,
engine?: string
): Promise<IHandlerOutput> {
if (engine) {
return engines[engine](url);
}
const host = new URL(url).hostname; const host = new URL(url).hostname;
return fallback[host]?.(url) || fallback["*"](url); return fallback[host]?.(url) || fallback["*"](url);
} }

View File

@ -1,14 +1,12 @@
import NodeCache from "node-cache"; import NodeCache from "node-cache";
import { EngineRequest } from "../types/requests"; import { EngineRequest } from "../types/requests";
import { FastifyInstance } from "fastify"; import { FastifyInstance } from "fastify";
import { engines } from "../handlers/main"; import handlePage from "../handlers/main";
export default function parseRoute(cache: NodeCache) { export default function parseRoute(cache: NodeCache) {
return async (fastify: FastifyInstance) => { return async (fastify: FastifyInstance) => {
fastify.get("/parse", async (req: EngineRequest) => { fastify.get("/parse", async (req: EngineRequest) => {
const url = req.query.url; const parsed = await handlePage(req.query.url, req.query.engine);
const engine = req.query.engine || "readability";
const parsed = await engines[engine](url);
cache.set(req.originalUrl || req.url, { cache.set(req.originalUrl || req.url, {
content: parsed, content: parsed,