This commit is contained in:
Artemy 2023-08-14 13:52:29 +03:00
commit ac366d57f6
4 changed files with 9 additions and 6 deletions

View File

@ -1,5 +1,6 @@
import { IConfigService } from "./config/config.interface";
import { ConfigService } from "./config/config.service";
import NodeCache from "node-cache";
import Fastify from "fastify";
import middie from "@fastify/middie";
@ -9,10 +10,12 @@ import parseRoute from "./routes/parseRoute";
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 +25,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();
}

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();