refactor: fastify migration
This commit is contained in:
parent
e15fb2a07b
commit
25dbad0f3b
981
package-lock.json
generated
981
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -5,10 +5,11 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/app.js",
|
"main": "dist/app.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@fastify/middie": "^8.3.0",
|
||||||
"@mozilla/readability": "^0.4.4",
|
"@mozilla/readability": "^0.4.4",
|
||||||
"axios": "^1.4.0",
|
"axios": "^1.4.0",
|
||||||
"dotenv": "^16.3.1",
|
"dotenv": "^16.3.1",
|
||||||
"express": "^4.18.2",
|
"fastify": "^4.21.0",
|
||||||
"jsdom": "^22.1.0",
|
"jsdom": "^22.1.0",
|
||||||
"node-cache": "^5.1.2"
|
"node-cache": "^5.1.2"
|
||||||
},
|
},
|
||||||
|
76
src/app.ts
76
src/app.ts
@ -1,10 +1,10 @@
|
|||||||
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 express from "express";
|
|
||||||
import NodeCache from "node-cache";
|
import NodeCache from "node-cache";
|
||||||
import { readability } from "./handlers/readability";
|
import { readability } from "./handlers/readability";
|
||||||
import minify from "./handlers/main";
|
import minify from "./handlers/main";
|
||||||
|
import Fastify, { FastifyRequest } from "fastify";
|
||||||
|
import middie from "@fastify/middie";
|
||||||
class App {
|
class App {
|
||||||
config: IConfigService;
|
config: IConfigService;
|
||||||
cache: NodeCache;
|
cache: NodeCache;
|
||||||
@ -12,11 +12,15 @@ class App {
|
|||||||
this.config = new ConfigService();
|
this.config = new ConfigService();
|
||||||
this.cache = new NodeCache({ stdTTL: 100, checkperiod: 120 });
|
this.cache = new NodeCache({ stdTTL: 100, checkperiod: 120 });
|
||||||
}
|
}
|
||||||
init() {
|
async init() {
|
||||||
const app = express();
|
const fastify = Fastify({
|
||||||
|
logger: true,
|
||||||
|
});
|
||||||
|
|
||||||
app.use((req, res, next) => {
|
await fastify.register(middie);
|
||||||
const url = req.originalUrl || req.url;
|
|
||||||
|
fastify.use((req, res, next) => {
|
||||||
|
const url = req.originalUrl || req.url || "/";
|
||||||
const purge = req.query.purge ? true : false;
|
const purge = req.query.purge ? true : false;
|
||||||
|
|
||||||
if (purge) {
|
if (purge) {
|
||||||
@ -24,44 +28,64 @@ class App {
|
|||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const cached = this.cache.get(url);
|
const cached: Cached | undefined = this.cache.get(url);
|
||||||
if (cached) {
|
if (cached) {
|
||||||
res.send(cached);
|
res.setHeader("content-type", `${cached.contentType}; charset=utf-8`);
|
||||||
|
res.end(cached.content);
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/get", (req, res) => {
|
fastify.get("/get", async (req: GetRequest, res) => {
|
||||||
const url = (req.query.url || "/nothing") as string;
|
const url = req.query.url;
|
||||||
const type = (req.query.type || "html") as string;
|
const type = req.query.type || "html";
|
||||||
|
const contentType =
|
||||||
|
type === "html"
|
||||||
|
? "text/html; charset=utf-8"
|
||||||
|
: "text/plain; charset=utf-8";
|
||||||
|
|
||||||
minify(url)
|
const parsed = await minify(url);
|
||||||
.then((parsed) => {
|
const content = type === "html" ? parsed?.content : parsed?.textContent;
|
||||||
const content =
|
|
||||||
type === "html" ? parsed?.content : parsed?.textContent;
|
|
||||||
|
|
||||||
this.cache.set(req.originalUrl || req.url, content);
|
this.cache.set(req.originalUrl || req.url, {
|
||||||
res.send(content);
|
content,
|
||||||
})
|
contentType: contentType,
|
||||||
.catch((err) => {
|
});
|
||||||
res.status(500).send({ error: err.message });
|
|
||||||
});
|
res.type(contentType);
|
||||||
|
return content;
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/readability", async (req, res) => {
|
fastify.get("/readability", async (req: EngineRequest) => {
|
||||||
const url = (req.query.url || "/nothing") as string;
|
const url = req.query.url;
|
||||||
const parsed = await readability(url);
|
const parsed = await readability(url);
|
||||||
|
|
||||||
this.cache.set(req.originalUrl || req.url, parsed);
|
this.cache.set(req.originalUrl || req.url, {
|
||||||
res.send(parsed);
|
content: parsed,
|
||||||
|
contentType: "text/json",
|
||||||
|
});
|
||||||
|
return parsed;
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(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")}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetRequest = FastifyRequest<{
|
||||||
|
Querystring: { url: string; type?: string };
|
||||||
|
}>;
|
||||||
|
|
||||||
|
type EngineRequest = FastifyRequest<{
|
||||||
|
Querystring: { url: string };
|
||||||
|
}>;
|
||||||
|
|
||||||
|
type Cached = {
|
||||||
|
content: string;
|
||||||
|
contentType: string;
|
||||||
|
};
|
||||||
|
|
||||||
const app = new App();
|
const app = new App();
|
||||||
app.init();
|
app.init();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user