refactor: routes
This commit is contained in:
parent
3a1fac2b65
commit
82b53b7dc5
38
src/app.ts
38
src/app.ts
@ -1,11 +1,11 @@
|
|||||||
import { IConfigService } from "./config/config.interface";
|
import { IConfigService } from "./config/config.interface";
|
||||||
import { ConfigService } from "./config/config.service";
|
import { ConfigService } from "./config/config.service";
|
||||||
import NodeCache from "node-cache";
|
import NodeCache from "node-cache";
|
||||||
import { readability } from "./handlers/readability";
|
|
||||||
import getCorrespondingReaderView from "./handlers/main";
|
|
||||||
import Fastify from "fastify";
|
import Fastify from "fastify";
|
||||||
import middie from "@fastify/middie";
|
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 {
|
class App {
|
||||||
config: IConfigService;
|
config: IConfigService;
|
||||||
cache: NodeCache;
|
cache: NodeCache;
|
||||||
@ -38,36 +38,8 @@ class App {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.get("/get", async (req: GetRequest, res) => {
|
fastify.register(getRoute(this.cache));
|
||||||
const url = req.query.url;
|
fastify.register(parseRoute(this.cache));
|
||||||
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.listen({ port: Number(this.config.get("PORT")) }, () => {
|
fastify.listen({ port: Number(this.config.get("PORT")) }, () => {
|
||||||
console.log(`Listening on port ${this.config.get("PORT")}`);
|
console.log(`Listening on port ${this.config.get("PORT")}`);
|
||||||
|
@ -9,10 +9,13 @@ export default function getCorrespondingReaderView(
|
|||||||
return fallback[host]?.(url) || fallback["*"](url);
|
return fallback[host]?.(url) || fallback["*"](url);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fallback: Fallback = {
|
export const engines: Engines = {
|
||||||
"*": readability,
|
readability,
|
||||||
};
|
};
|
||||||
|
|
||||||
interface Fallback {
|
const fallback: Engines = {
|
||||||
|
"*": engines.readability,
|
||||||
|
};
|
||||||
|
interface Engines {
|
||||||
[host: string]: (url: string) => Promise<IHandlerOutput>;
|
[host: string]: (url: string) => Promise<IHandlerOutput>;
|
||||||
}
|
}
|
||||||
|
28
src/routes/getRoute.ts
Normal file
28
src/routes/getRoute.ts
Normal 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
20
src/routes/parseRoute.ts
Normal 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;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
@ -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
19
src/types/requests.ts
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user