From dad9f46d86703781e198be6234f32cbac9c6f115 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 24 Apr 2024 20:07:24 +0100 Subject: [PATCH] Set Cache-Control headers in route controllers Signed-off-by: Fred Boniface --- app.js | 9 +++------ src/controllers/issue.controllers.js | 1 + src/controllers/ldb.controllers.js | 6 ++++++ src/controllers/pis.controllers.ts | 2 ++ src/controllers/ref.controllers.js | 4 ++++ src/controllers/stats.controllers.js | 4 ++++ src/controllers/train.controllers.js | 3 +++ src/middlewares/setHeaders.middlewares.ts | 10 ---------- src/utils/cacheHeader.utils.ts | 9 +++++++++ 9 files changed, 32 insertions(+), 16 deletions(-) delete mode 100644 src/middlewares/setHeaders.middlewares.ts create mode 100644 src/utils/cacheHeader.utils.ts diff --git a/app.js b/app.js index 8e38ac3..e33b89a 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,4 @@ // OwlBoard - © Fred Boniface 2022-2023 - Licensed under GPLv3 (or later) - -import { setCacheHeaders } from "./src/middlewares/setHeaders.middlewares"; - // Please see the included LICENSE file const mode = process.env.NODE_ENV || "development"; @@ -78,15 +75,15 @@ app.use("/api/v1/auth/test", authenticate, (req, res) => }) ); // Returns 401 if auth failed, 200 if successful. -// Post middleware -app.use(setCacheHeaders) - // Number of proxies: app.set("trust proxy", 4); mode === "development" ? app.get("/api/v1/ip", (req, res) => res.send(req.ip)) : null; +// Disable etags +app.set('etag', false) + // Start Express app.listen(srvPort, srvListen, (error) => { if (!error) { diff --git a/src/controllers/issue.controllers.js b/src/controllers/issue.controllers.js index 748ffcf..9bb4941 100644 --- a/src/controllers/issue.controllers.js +++ b/src/controllers/issue.controllers.js @@ -4,6 +4,7 @@ const log = require("../utils/logs.utils"); async function post(req, res, next) { try { log.out(`issueControllers.post: Request Body: ${JSON.stringify(req.body)}`); + setCache(res, "no-store") res.json(await issue.processor(req.body)); } catch (err) { console.error("Controller Error", err.message); diff --git a/src/controllers/ldb.controllers.js b/src/controllers/ldb.controllers.js index 9dc04b2..83f92f9 100644 --- a/src/controllers/ldb.controllers.js +++ b/src/controllers/ldb.controllers.js @@ -1,5 +1,7 @@ const ldb = require("../services/ldb.services"); +import { setCache } from "../utils/cacheHeader.utils"; + async function getTrain(req, res, next) { // API v2 Only if (!req.isAuthed) { @@ -7,6 +9,7 @@ async function getTrain(req, res, next) { err.status = 401; throw err; } + setCache(res, "private", 180) let type = req.params.searchType; let id = req.params.id; try { @@ -42,11 +45,14 @@ async function getStation(req, res, next) { err.status = 401; return next(err); } + setCache(res, "public", 120) res.json(await ldb.get(id, true)); } else { + setCache(res, "public", 240) res.json(await ldb.get(id, false)); } } catch (err) { + setCache(res, "no-store") console.error("Unknown Error", err.message); err.status = 500; next(err); diff --git a/src/controllers/pis.controllers.ts b/src/controllers/pis.controllers.ts index 09a73b0..98b1131 100644 --- a/src/controllers/pis.controllers.ts +++ b/src/controllers/pis.controllers.ts @@ -1,5 +1,6 @@ import { Request, Response, NextFunction } from "express"; import { findPisByOrigDest, findPisByCode } from "../services/pis.services"; +import { setCache } from "../utils/cacheHeader.utils"; async function byStartEndCRS(req: Request, res: Response, next: NextFunction) { // if (!req.isAuthed) { @@ -10,6 +11,7 @@ async function byStartEndCRS(req: Request, res: Response, next: NextFunction) { try { let startCrs = req.params.startCrs; let endCrs = req.params.endCrs; + setCache(res, "public", 600) res.json(await findPisByOrigDest(startCrs, endCrs)); } catch (err: any) { console.error("Unknown Error", err.message); diff --git a/src/controllers/ref.controllers.js b/src/controllers/ref.controllers.js index dbe0748..32e8275 100644 --- a/src/controllers/ref.controllers.js +++ b/src/controllers/ref.controllers.js @@ -1,5 +1,7 @@ /* API V2 Exclusive Controller */ +import { setCache } from "../utils/cacheHeader.utils"; + const ldb = require("../services/ldb.services"); const find = require("../services/find.services"); @@ -7,6 +9,7 @@ async function getReasonCode(req, res, next) { try { const code = req.params.code; if (code === "all") { + setCache(res, "public", 604800) res.json(await ldb.getReasonCodeList()); next; } @@ -23,6 +26,7 @@ async function getLocationReference(req, res, next) { try { const searchType = req.params.searchType; const id = req.params.id; + setCache(res, "public", 604800) switch (searchType) { case "name": res.json(await find.name(id)); diff --git a/src/controllers/stats.controllers.js b/src/controllers/stats.controllers.js index 327b080..b24def4 100644 --- a/src/controllers/stats.controllers.js +++ b/src/controllers/stats.controllers.js @@ -1,8 +1,11 @@ +import { setCache } from "../utils/cacheHeader.utils"; + const stat = require("../services/stats.services"); async function versions(req, res, next) { // API v2 try { + setCache(res, "public", 60) res.json(await stat.getVersions()); } catch (err) { console.error("Controller Error", err); @@ -14,6 +17,7 @@ async function versions(req, res, next) { async function statistics(req, res, next) { // Api v2 try { + setCache(res, "public", 60) res.json(await stat.statistics()); } catch (err) { console.error("Controller Error", err); diff --git a/src/controllers/train.controllers.js b/src/controllers/train.controllers.js index fabc1d9..d2b8172 100644 --- a/src/controllers/train.controllers.js +++ b/src/controllers/train.controllers.js @@ -1,3 +1,4 @@ +import { setCache } from "../utils/cacheHeader.utils"; import { logger } from "../utils/logger.utils"; const train = require("../services/trainService.services"); @@ -32,9 +33,11 @@ async function get(req, res, next) { try { switch (searchType) { case "headcode": + setCache(res, "private", 1800) res.json(await train.findByHeadcode(id, date)); break; case "byTrainUid": + setCache(res, "private", 1800) res.json(await train.findByTrainUid(id, date)); break; default: diff --git a/src/middlewares/setHeaders.middlewares.ts b/src/middlewares/setHeaders.middlewares.ts deleted file mode 100644 index e0098f0..0000000 --- a/src/middlewares/setHeaders.middlewares.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { NextFunction, Request, Response } from "express"; - -export function setCacheHeaders(req: Request, res: Response, next: NextFunction) { - if (res.cacheType && res.cacheSecs) { - const cacheCtlHeader = `${res.cacheType}, max-age=${res.cacheSecs}`; - res.setHeader('Cache-Control', cacheCtlHeader) - } - - res.send(); -} \ No newline at end of file diff --git a/src/utils/cacheHeader.utils.ts b/src/utils/cacheHeader.utils.ts new file mode 100644 index 0000000..51e0d70 --- /dev/null +++ b/src/utils/cacheHeader.utils.ts @@ -0,0 +1,9 @@ +import type { Response } from "express" + +export function setCache(res: Response, type="private", time=120): void { + if (type === "no-store") { + res.setHeader('Cache-Control', 'no-store') + return + } + res.setHeader('Cache-Control', `${type}, max-age=${time}`) +} \ No newline at end of file