feat: handlers

This commit is contained in:
Artemy 2023-08-13 21:39:23 +03:00
parent d02317c587
commit e15fb2a07b
6 changed files with 61 additions and 13 deletions

4
package-lock.json generated
View File

@ -1,11 +1,11 @@
{
"name": "dottxt",
"name": "txtdot",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "dottxt",
"name": "txtdot",
"version": "1.0.0",
"license": "ISC",
"dependencies": {

View File

@ -1,6 +1,7 @@
{
"name": "dottxt",
"name": "txtdot",
"version": "1.0.0",
"private": true,
"description": "",
"main": "dist/app.js",
"dependencies": {

View File

@ -1,10 +1,9 @@
import { IConfigService } from "./config/config.interface";
import { ConfigService } from "./config/config.service";
import express from "express";
import axios from "axios";
import { JSDOM } from "jsdom";
import { Readability } from "@mozilla/readability";
import NodeCache from "node-cache";
import { readability } from "./handlers/readability";
import minify from "./handlers/main";
class App {
config: IConfigService;
@ -37,12 +36,8 @@ class App {
const url = (req.query.url || "/nothing") as string;
const type = (req.query.type || "html") as string;
axios
.get(url)
.then((response) => {
const dom = new JSDOM(response.data, { url: url });
const reader = new Readability(dom.window.document);
const parsed = reader.parse();
minify(url)
.then((parsed) => {
const content =
type === "html" ? parsed?.content : parsed?.textContent;
@ -50,10 +45,18 @@ class App {
res.send(content);
})
.catch((err) => {
res.status(500).send(err);
res.status(500).send({ error: err.message });
});
});
app.get("/readability", async (req, res) => {
const url = (req.query.url || "/nothing") as string;
const parsed = await readability(url);
this.cache.set(req.originalUrl || req.url, parsed);
res.send(parsed);
});
app.listen(this.config.get("PORT"), () => {
console.log(`Listening on port ${this.config.get("PORT")}`);
});

View File

@ -0,0 +1,6 @@
export interface IHandlerOutput {
content: string;
textContent: string;
title: string;
lang: string;
}

16
src/handlers/main.ts Normal file
View File

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

View File

@ -0,0 +1,22 @@
import { Readability } from "@mozilla/readability";
import axios from "axios";
import { JSDOM } from "jsdom";
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();
if (!parsed) {
throw new Error("Failed to parse [readability]");
}
return {
content: parsed.content,
textContent: parsed.textContent,
title: parsed.title,
lang: parsed.lang,
};
}