diff --git a/src/app.ts b/src/app.ts index a82d779..75737bd 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,11 +1,11 @@ import { IConfigService } from "./config/config.interface"; import { ConfigService } from "./config/config.service"; import NodeCache from "node-cache"; -import { readability } from "./handlers/readability"; -import getCorrespondingReaderView from "./handlers/main"; import Fastify from "fastify"; import middie from "@fastify/middie"; -import { Cached, EngineRequest, GetRequest } from "./schema/requests.types"; +import { Cached } from "./types/requests"; +import getRoute from "./routes/getRoute"; +import parseRoute from "./routes/parseRoute"; class App { config: IConfigService; cache: NodeCache; @@ -38,36 +38,8 @@ class App { } }); - fastify.get("/get", async (req: GetRequest, res) => { - const url = req.query.url; - const type = req.query.type || "html"; - const contentType = - type === "html" - ? "text/html; charset=utf-8" - : "text/plain; charset=utf-8"; - - const parsed = await getCorrespondingReaderView(url); - const content = type === "html" ? parsed?.content : parsed?.textContent; - - this.cache.set(req.originalUrl || req.url, { - content, - contentType: contentType, - }); - - res.type(contentType); - return content; - }); - - fastify.get("/readability", async (req: EngineRequest) => { - const url = req.query.url; - const parsed = await readability(url); - - this.cache.set(req.originalUrl || req.url, { - content: parsed, - contentType: "text/json", - }); - return parsed; - }); + fastify.register(getRoute(this.cache)); + fastify.register(parseRoute(this.cache)); fastify.listen({ port: Number(this.config.get("PORT")) }, () => { console.log(`Listening on port ${this.config.get("PORT")}`); diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 4da040d..1204102 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -9,10 +9,13 @@ export default function getCorrespondingReaderView( return fallback[host]?.(url) || fallback["*"](url); } -const fallback: Fallback = { - "*": readability, +export const engines: Engines = { + readability, }; -interface Fallback { +const fallback: Engines = { + "*": engines.readability, +}; +interface Engines { [host: string]: (url: string) => Promise; } diff --git a/src/routes/getRoute.ts b/src/routes/getRoute.ts new file mode 100644 index 0000000..4e3d198 --- /dev/null +++ b/src/routes/getRoute.ts @@ -0,0 +1,28 @@ +import NodeCache from "node-cache"; +import getCorrespondingReaderView from "../handlers/main"; +import { GetRequest } from "../types/requests"; +import { FastifyInstance } from "fastify"; + +export default function getRoute(cache: NodeCache) { + return async (fastify: FastifyInstance) => { + fastify.get("/get", async (req: GetRequest, res) => { + const url = req.query.url; + const type = req.query.type || "html"; + const contentType = + type === "html" + ? "text/html; charset=utf-8" + : "text/plain; charset=utf-8"; + + const parsed = await getCorrespondingReaderView(url); + const content = type === "html" ? parsed?.content : parsed?.textContent; + + cache.set(req.originalUrl || req.url, { + content, + contentType: contentType, + }); + + res.type(contentType); + return content; + }); + }; +} diff --git a/src/routes/parseRoute.ts b/src/routes/parseRoute.ts new file mode 100644 index 0000000..e23d7a1 --- /dev/null +++ b/src/routes/parseRoute.ts @@ -0,0 +1,20 @@ +import NodeCache from "node-cache"; +import { EngineRequest } from "../types/requests"; +import { FastifyInstance } from "fastify"; +import { engines } from "../handlers/main"; + +export default function parseRoute(cache: NodeCache) { + return async (fastify: FastifyInstance) => { + fastify.get("/parse", async (req: EngineRequest) => { + const url = req.query.url; + const engine = req.query.engine || "readability"; + const parsed = await engines[engine](url); + + cache.set(req.originalUrl || req.url, { + content: parsed, + contentType: "text/json", + }); + return parsed; + }); + }; +} diff --git a/src/schema/requests.types.ts b/src/schema/requests.types.ts deleted file mode 100644 index 0906ba0..0000000 --- a/src/schema/requests.types.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { FastifyRequest } from "fastify"; - -export type GetRequest = FastifyRequest<{ - Querystring: { url: string; type?: string }; -}>; - -export type EngineRequest = FastifyRequest<{ - Querystring: { url: string }; -}>; - -export type Cached = { - content: string; - contentType: string; -}; diff --git a/src/types/requests.ts b/src/types/requests.ts new file mode 100644 index 0000000..b775236 --- /dev/null +++ b/src/types/requests.ts @@ -0,0 +1,19 @@ +import { FastifyInstance, FastifyRequest } from "fastify"; +import NodeCache from "node-cache"; + +export type GetRequest = FastifyRequest<{ + Querystring: { url: string; type?: string }; +}>; + +export type EngineRequest = FastifyRequest<{ + Querystring: { url: string; engine?: string }; +}>; + +export type Cached = { + content: string; + contentType: string; +}; + +export interface IFastifyInstance extends FastifyInstance { + cache: NodeCache; +}