Merge pull request #1 from TxtDot/dc09

Formatting
This commit is contained in:
Andrey 2023-08-14 14:51:20 +04:00 committed by GitHub
commit 0491089b91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 8 deletions

View File

@ -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;
});

View File

@ -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`);
}

View File

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

View File

@ -6,6 +6,7 @@ import { IHandlerOutput } from "./handler.interface";
export async function readability(url: string): Promise<IHandlerOutput> {
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();