Skip to content

Commit

Permalink
Memoize processes on token (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
magopian authored and ccomb committed May 19, 2024
1 parent 6f3666d commit fdf57d7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dotenv": "^16.4.5",
"elm": "^0.19.1-6",
"express": "^4.19.2",
"fast-memoize": "^2.5.2",
"helmet": "^7.1.0",
"highcharts": "^11.4.0",
"js-yaml": "^4.1.0",
Expand Down
32 changes: 21 additions & 11 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const helmet = require("helmet");
const Sentry = require("@sentry/node");
const { Elm } = require("./server-app");
const lib = require("./lib");
const memoize = require("fast-memoize");

const app = express(); // web app
const api = express(); // api app
const host = "0.0.0.0";
const express_port = 8001;
const django_port = 8002;
const max_memoize_age = 1000 * 60 * 24; // 24 hours memoization

// Env vars
const { SENTRY_DSN, MATOMO_HOST, MATOMO_SITE_ID, MATOMO_TOKEN } = process.env;
Expand Down Expand Up @@ -108,32 +110,40 @@ api.get(/^\/products$/, (_, res) => res.redirect("textile/products"));
const cleanRedirect = (url) => (url.startsWith("/") ? url : "");
api.get(/^\/simulator(.*)$/, ({ url }, res) => res.redirect(`/api/textile${cleanRedirect(url)}`));

// Note: Text/JSON request body parser (JSON is decoded in Elm)
api.all(/(.*)/, bodyParser.json(), async (req, res) => {
const getProcesses = async (token) => {
let headers = {};
if (req.headers.token) {
headers["token"] = req.headers.token;
if (token) {
headers["token"] = token;
}
let processes;
if (process.env.NODE_ENV == "test") {
headers["fakeDetails"] = "true";
}
const processesUrl = `http://127.0.0.1:${django_port}/processes/processes.json`;
const processesRes = await fetch(processesUrl, { headers: headers });
const processes = await processesRes.json();
return { processes: processes, status: processesRes.status };
};

const memoizedGetProcesses = memoize(getProcesses, { maxAge: max_memoize_age });

// Note: Text/JSON request body parser (JSON is decoded in Elm)
api.all(/(.*)/, bodyParser.json(), async (req, res) => {
let result;
try {
const processesUrl = `http://127.0.0.1:${django_port}/processes/processes.json`;
const processesRes = await fetch(processesUrl, { headers: headers });
processes = await processesRes.json();
if (processesRes.status != 200) {
return res.status(processesRes.status).send(processes);
result = await memoizedGetProcesses(req.headers.token);
if (result.status != 200) {
return res.status(result.status).send(result.processes);
}
} catch (err) {
console.error(err.message);
return res.status(500).send("Error while retrieving the processes");
}

elmApp.ports.input.send({
method: req.method,
url: req.url,
body: req.body,
processes: processes,
processes: result.processes,
jsResponseHandler: ({ status, body }) => {
apiTracker.track(status, req);
res.status(status).send(body);
Expand Down

0 comments on commit fdf57d7

Please sign in to comment.