feat: engine choose
This commit is contained in:
parent
219e3ee716
commit
5325941078
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user