UNPKG

npm-package-nodejs-utils-lda

Version:

Este projeto tem como fins de criar e abstrair módulos basicos e utilidades para o node js

45 lines (40 loc) 1.32 kB
const { fopen, fwrite, log } = require("./autoFileSysModule.cjs"); const dotenv = require("dotenv"); const { configExist } = require("./utils.cjs"); const logPath = "cache.txt"; // Carregar variáveis de ambiente do arquivo .env dotenv.config(); configExist(); checkConfigIntegrity(); function setCacheHeaders(req, res, next) { const configs = fopen("config.json"); if (req.headers["x-disable-cache"] === "true") { log( `🚫 Disabling cache for this request. ${req.method}:${req.path}`, logPath ); res.set("Cache-Control", "no-store"); return next(); } // Cache de 1 hora const cacheTime = 60 * configs.cacheDurationInMinutes; // 1 hora em segundos log( `✅ Adding ${cacheTime} minutes Cache header. ${req.method}:${req.path}`, logPath ); res.set("Cache-Control", `public, max-age=${cacheTime}`); res.set("Cache-time", cacheTime); next(); } function checkConfigIntegrity() { // obtem config.json const configs = fopen("config.json"); // verifica se blockedRoutes não existe if (!configs.cacheDurationInMinutes) { // caso não exista configura para uma rota padrão configs.cacheDurationInMinutes = 30; // salva novamente fwrite("config.json", configs); } } module.exports = setCacheHeaders;