diff --git a/src/app.ts b/src/app.ts index a82d779..c1780da 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,18 +1,25 @@ 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 handlePage from "./handlers/main"; + import Fastify from "fastify"; import middie from "@fastify/middie"; + import { Cached, EngineRequest, GetRequest } from "./schema/requests.types"; + class App { config: IConfigService; cache: NodeCache; + constructor() { this.config = new ConfigService(); this.cache = new NodeCache({ stdTTL: 100, checkperiod: 120 }); } + async init() { const fastify = Fastify({ logger: true, @@ -22,9 +29,8 @@ class App { fastify.use((req, res, next) => { const url = req.originalUrl || req.url || "/"; - const purge = req.query.purge ? true : false; - if (purge) { + if (req.query.purge) { this.cache.del(url); next(); } @@ -46,7 +52,7 @@ class App { ? "text/html; charset=utf-8" : "text/plain; charset=utf-8"; - const parsed = await getCorrespondingReaderView(url); + const parsed = await handlePage(url); const content = type === "html" ? parsed?.content : parsed?.textContent; this.cache.set(req.originalUrl || req.url, { @@ -66,6 +72,7 @@ class App { content: parsed, contentType: "text/json", }); + return parsed; }); diff --git a/src/config/config.service.ts b/src/config/config.service.ts index a96f195..cb775c9 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -3,8 +3,10 @@ import { IConfigService } from "./config.interface"; export class ConfigService implements IConfigService { private config: DotenvParseOutput; + constructor() { const { error, parsed } = config(); + if (error) { throw new Error(".env file not found"); } @@ -18,6 +20,7 @@ export class ConfigService implements IConfigService { get(key: string): string { const res = this.config[key]; + if (!res) { throw new Error(`Key ${key} not found`); } diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 4da040d..8d00030 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -1,11 +1,8 @@ import { IHandlerOutput } from "./handler.interface"; import { readability } from "./readability"; -export default function getCorrespondingReaderView( - url: string -): Promise { +export default function handlePage(url: string): Promise { const host = new URL(url).hostname; - return fallback[host]?.(url) || fallback["*"](url); } diff --git a/src/handlers/readability.ts b/src/handlers/readability.ts index 0a6f39a..cafc43a 100644 --- a/src/handlers/readability.ts +++ b/src/handlers/readability.ts @@ -6,6 +6,7 @@ import { IHandlerOutput } from "./handler.interface"; export async function readability(url: string): Promise { const response = await axios.get(url); const dom = new JSDOM(response.data, { url: url }); + const reader = new Readability(dom.window.document); const parsed = reader.parse();