refactor: routes

This commit is contained in:
Artemy 2023-08-14 13:51:46 +03:00
parent 3a1fac2b65
commit 82b53b7dc5
6 changed files with 78 additions and 50 deletions

View File

@ -1,11 +1,11 @@
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 Fastify from "fastify";
import middie from "@fastify/middie";
import { Cached, EngineRequest, GetRequest } from "./schema/requests.types";
import { Cached } from "./types/requests";
import getRoute from "./routes/getRoute";
import parseRoute from "./routes/parseRoute";
class App {
config: IConfigService;
cache: NodeCache;
@ -38,36 +38,8 @@ class App {
}
});
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 getCorrespondingReaderView(url);
const content = type === "html" ? parsed?.content : parsed?.textContent;
this.cache.set(req.originalUrl || req.url, {
content,
contentType: contentType,
});
res.type(contentType);
return content;
});
fastify.get("/readability", async (req: EngineRequest) => {
const url = req.query.url;
const parsed = await readability(url);
this.cache.set(req.originalUrl || req.url, {
content: parsed,
contentType: "text/json",
});
return parsed;
});
fastify.register(getRoute(this.cache));
fastify.register(parseRoute(this.cache));
fastify.listen({ port: Number(this.config.get("PORT")) }, () => {
console.log(`Listening on port ${this.config.get("PORT")}`);

View File

@ -9,10 +9,13 @@ export default function getCorrespondingReaderView(
return fallback[host]?.(url) || fallback["*"](url);
}
const fallback: Fallback = {
"*": readability,
export const engines: Engines = {
readability,
};
interface Fallback {
const fallback: Engines = {
"*": engines.readability,
};
interface Engines {
[host: string]: (url: string) => Promise<IHandlerOutput>;
}

28
src/routes/getRoute.ts Normal file
View File

@ -0,0 +1,28 @@
import NodeCache from "node-cache";
import getCorrespondingReaderView from "../handlers/main";
import { GetRequest } from "../types/requests";
import { FastifyInstance } from "fastify";
export default function getRoute(cache: NodeCache) {
return async (fastify: FastifyInstance) => {
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 getCorrespondingReaderView(url);
const content = type === "html" ? parsed?.content : parsed?.textContent;
cache.set(req.originalUrl || req.url, {
content,
contentType: contentType,
});
res.type(contentType);
return content;
});
};
}

20
src/routes/parseRoute.ts Normal file
View File

@ -0,0 +1,20 @@
import NodeCache from "node-cache";
import { EngineRequest } from "../types/requests";
import { FastifyInstance } from "fastify";
import { engines } from "../handlers/main";
export default function parseRoute(cache: NodeCache) {
return async (fastify: FastifyInstance) => {
fastify.get("/parse", async (req: EngineRequest) => {
const url = req.query.url;
const engine = req.query.engine || "readability";
const parsed = await engines[engine](url);
cache.set(req.originalUrl || req.url, {
content: parsed,
contentType: "text/json",
});
return parsed;
});
};
}

View File

@ -1,14 +0,0 @@
import { FastifyRequest } from "fastify";
export type GetRequest = FastifyRequest<{
Querystring: { url: string; type?: string };
}>;
export type EngineRequest = FastifyRequest<{
Querystring: { url: string };
}>;
export type Cached = {
content: string;
contentType: string;
};

19
src/types/requests.ts Normal file
View File

@ -0,0 +1,19 @@
import { FastifyInstance, FastifyRequest } from "fastify";
import NodeCache from "node-cache";
export type GetRequest = FastifyRequest<{
Querystring: { url: string; type?: string };
}>;
export type EngineRequest = FastifyRequest<{
Querystring: { url: string; engine?: string };
}>;
export type Cached = {
content: string;
contentType: string;
};
export interface IFastifyInstance extends FastifyInstance {
cache: NodeCache;
}