feat: handlers
This commit is contained in:
parent
d02317c587
commit
e15fb2a07b
4
package-lock.json
generated
4
package-lock.json
generated
@ -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": {
|
||||
|
@ -1,6 +1,7 @@
|
||||
{
|
||||
"name": "dottxt",
|
||||
"name": "txtdot",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"description": "",
|
||||
"main": "dist/app.js",
|
||||
"dependencies": {
|
||||
|
23
src/app.ts
23
src/app.ts
@ -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")}`);
|
||||
});
|
||||
|
6
src/handlers/handler.interface.ts
Normal file
6
src/handlers/handler.interface.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface IHandlerOutput {
|
||||
content: string;
|
||||
textContent: string;
|
||||
title: string;
|
||||
lang: string;
|
||||
}
|
16
src/handlers/main.ts
Normal file
16
src/handlers/main.ts
Normal 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>;
|
||||
}
|
22
src/handlers/readability.ts
Normal file
22
src/handlers/readability.ts
Normal 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,
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user