2023-08-13 20:38:33 +03:00
|
|
|
import { IConfigService } from "./config/config.interface";
|
|
|
|
import { ConfigService } from "./config/config.service";
|
|
|
|
import NodeCache from "node-cache";
|
2023-08-13 21:39:23 +03:00
|
|
|
import { readability } from "./handlers/readability";
|
|
|
|
import minify from "./handlers/main";
|
2023-08-14 13:05:34 +03:00
|
|
|
import Fastify from "fastify";
|
2023-08-14 13:03:19 +03:00
|
|
|
import middie from "@fastify/middie";
|
2023-08-14 13:05:34 +03:00
|
|
|
import { Cached, EngineRequest, GetRequest } from "./schema/requests.types";
|
2023-08-13 20:38:33 +03:00
|
|
|
class App {
|
|
|
|
config: IConfigService;
|
|
|
|
cache: NodeCache;
|
|
|
|
constructor() {
|
|
|
|
this.config = new ConfigService();
|
|
|
|
this.cache = new NodeCache({ stdTTL: 100, checkperiod: 120 });
|
|
|
|
}
|
2023-08-14 13:03:19 +03:00
|
|
|
async init() {
|
|
|
|
const fastify = Fastify({
|
|
|
|
logger: true,
|
|
|
|
});
|
2023-08-13 20:38:33 +03:00
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
await fastify.register(middie);
|
|
|
|
|
|
|
|
fastify.use((req, res, next) => {
|
|
|
|
const url = req.originalUrl || req.url || "/";
|
2023-08-13 20:38:33 +03:00
|
|
|
const purge = req.query.purge ? true : false;
|
|
|
|
|
|
|
|
if (purge) {
|
|
|
|
this.cache.del(url);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
const cached: Cached | undefined = this.cache.get(url);
|
2023-08-13 20:38:33 +03:00
|
|
|
if (cached) {
|
2023-08-14 13:03:19 +03:00
|
|
|
res.setHeader("content-type", `${cached.contentType}; charset=utf-8`);
|
|
|
|
res.end(cached.content);
|
2023-08-13 20:38:33 +03:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
fastify.get("/get", async (req: GetRequest, res) => {
|
|
|
|
const url = req.query.url;
|
|
|
|
const type = req.query.type || "html";
|
|
|
|
const contentType =
|
|
|
|
type === "html"
|
|
|
|
? "text/html; charset=utf-8"
|
|
|
|
: "text/plain; charset=utf-8";
|
|
|
|
|
|
|
|
const parsed = await minify(url);
|
|
|
|
const content = type === "html" ? parsed?.content : parsed?.textContent;
|
2023-08-13 20:38:33 +03:00
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
this.cache.set(req.originalUrl || req.url, {
|
|
|
|
content,
|
|
|
|
contentType: contentType,
|
|
|
|
});
|
2023-08-13 20:38:33 +03:00
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
res.type(contentType);
|
|
|
|
return content;
|
2023-08-13 20:38:33 +03:00
|
|
|
});
|
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
fastify.get("/readability", async (req: EngineRequest) => {
|
|
|
|
const url = req.query.url;
|
2023-08-13 21:39:23 +03:00
|
|
|
const parsed = await readability(url);
|
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
this.cache.set(req.originalUrl || req.url, {
|
|
|
|
content: parsed,
|
|
|
|
contentType: "text/json",
|
|
|
|
});
|
|
|
|
return parsed;
|
2023-08-13 21:39:23 +03:00
|
|
|
});
|
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
fastify.listen({ port: Number(this.config.get("PORT")) }, () => {
|
2023-08-13 20:38:33 +03:00
|
|
|
console.log(`Listening on port ${this.config.get("PORT")}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const app = new App();
|
|
|
|
app.init();
|