mirror of
https://github.com/LeRoid-hub/Mensa-API.git
synced 2025-01-31 11:44:55 +00:00
fixed Cache + implemented Cache
This commit is contained in:
parent
268d3132d5
commit
b8234748ad
@ -1,11 +1,11 @@
|
|||||||
export class Cache {
|
export default class Cache {
|
||||||
cachedData: any;
|
cachedData: any;
|
||||||
lastUsed: Date;
|
lastUsed: Date;
|
||||||
lifeTime: number;
|
lifeTime: number;
|
||||||
mensa: string;
|
mensa: string;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.cachedData = {};
|
this.cachedData = null;
|
||||||
this.lastUsed = new Date();
|
this.lastUsed = new Date();
|
||||||
this.lifeTime = 1000 * 30;
|
this.lifeTime = 1000 * 30;
|
||||||
this.mensa = "";
|
this.mensa = "";
|
||||||
@ -16,7 +16,7 @@ export class Cache {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.cachedData === null) {
|
if (this.cachedData == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
src/index.ts
19
src/index.ts
@ -2,32 +2,43 @@
|
|||||||
import express, { Express, Request, Response} from "express";
|
import express, { Express, Request, Response} from "express";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser, { text } from "body-parser";
|
||||||
import morgan from "morgan";
|
import morgan from "morgan";
|
||||||
|
|
||||||
import fetch from './fetch.js';
|
import fetch from './fetch.js';
|
||||||
|
import Cache from './cache.js';
|
||||||
|
import stripHtml from "./stripper.js";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const app: Express = express().use(cors({ origin: '*' })).use(bodyParser.json());
|
const app: Express = express().use(cors({ origin: '*' }));
|
||||||
app.use(morgan('combined'))
|
app.use(morgan('combined'))
|
||||||
|
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
|
|
||||||
const baseUrl = "https://www.imensa.de/";
|
const baseUrl = "https://www.imensa.de/";
|
||||||
|
|
||||||
|
const cache = new Cache();
|
||||||
|
|
||||||
app.get("/", (req: Request, res: Response) => {
|
app.get("/", (req: Request, res: Response) => {
|
||||||
res.send("Mensa API");
|
res.send("Mensa API");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/api/:Ort/:Mensa", (req: Request, res: Response) => {
|
app.get("/api/:Ort", (req: Request, res: Response) => {
|
||||||
if (req.params.Ort === null) {
|
if (req.params.Ort === null) {
|
||||||
return res.send("Invalid request");
|
return res.send("Invalid request");
|
||||||
}
|
}
|
||||||
|
let cachedData = cache.get();
|
||||||
|
if (cachedData !== null) {
|
||||||
|
return res.send(cachedData);
|
||||||
|
}else {
|
||||||
let url = baseUrl + req.params.Ort.toLowerCase();
|
let url = baseUrl + req.params.Ort.toLowerCase();
|
||||||
fetch(url).then((data) => {
|
fetch(url).then((data) => {
|
||||||
res.send(data);
|
let stripedData = stripHtml(data);
|
||||||
|
cache.set(req.params.Ort, stripedData);
|
||||||
|
res.send(stripedData);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
export default function stripHtml(html: string): string {
|
||||||
|
return html;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user