Skip to content

Commit

Permalink
Merge pull request #762 from weather-gov/mgwalker/api-proxy-thing-again
Browse files Browse the repository at this point in the history
Proxy bundles, take 2
  • Loading branch information
greg-does-weather authored Feb 14, 2024
2 parents c433887 + 9aa1215 commit 3fcdf9a
Show file tree
Hide file tree
Showing 41 changed files with 117 additions and 431 deletions.
2 changes: 1 addition & 1 deletion tests/a11y/cypress/e2e/axe-accessibility-tests.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const viewports = [

describe("accessibility tests", () => {
before(() => {
cy.request("http://localhost:8081/local");
cy.request("http://localhost:8081/play/testing");
});

pages.forEach(({ name, url }) => {
Expand Down
33 changes: 5 additions & 28 deletions tests/api/config.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
let bundling = false;
let bundleID = false;
let localService = true;
let recording = false;
let play = "testing";

export default {
get bundling() {
return bundling;
},
set bundling(v) {
bundling = v;
if (v === false) {
console.log(`CONFIG: bundle ${bundleID} finished`);
bundleID = false;
}
},

get bundleID() {
return bundleID;
get play() {
return play;
},
set bundleID(v) {
if (bundling && !bundleID) {
bundleID = v;
console.log(`CONFIG: starting bundle ${bundleID}`);
}
},

get localService() {
return localService;
},
set localService(v) {
localService = v;
},

get recording() {
return recording;
},
toggleRecording() {
recording = !recording;
set play(v) {
play = v;
},
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
103 changes: 83 additions & 20 deletions tests/api/main.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,103 @@
import fs from "node:fs/promises";
import express from "express";
import path from "node:path";
import proxyToApi from "./proxy.js";
import config from "./config.js";
import serveLocally from "./local.js";
import serveBundle from "./serve.js";

const app = express();
const port = process.env.PORT ?? 8081;

app.get("*", (req, res) => {
if (req.path === "/no-local") {
config.localService = false;
res.write("Locally-served files are now disabled");
console.log("Locally-served files are now disabled");
const fsExists = async (filePath) =>
fs
.access(filePath, fs.constants.F_OK)
.then(() => true)
.catch(() => false);

const ui = async (error = false) => {
const lines = ["<html>"];

if (error) {
lines.push(`<h2>${error}</h2>`);
}

if (config.bundling) {
lines.push("Currently recording new bundles");
lines.push(`<br><a href="/stop">Stop recording</a>`);
lines.push("<br><br>");
} else if (config.play) {
lines.push(`Currently playing bundle <strong>${config.play}</strong>`);
lines.push(`<br><a href="/stop">Stop playing</a>`);
lines.push("<br><br>");
} else {
lines.push(`Not playing a bundle. Sending requests through.`);
lines.push("<br><br>");
}

const contents = await fs
.readdir("./data")
.then((files) => files.filter((file) => !file.startsWith(".")));

const dirs = await Promise.all(
contents.map(async (file) => {
const stat = await fs.stat(path.join("./data", file));
return stat.isDirectory();
}),
);

const bundles = contents.filter((_, i) => dirs[i]);

lines.push("Available bundles:");
lines.push(
`<ul><li>${bundles.map((p) => `<a href="/play/${p}">${p}</a>`).join("</li><li>")}</li></ul>`,
);

if (!config.bundling) {
lines.push(`<a href="/bundle">record bundles</a>`);
}

lines.push("</html>");
return lines.join("");
};

app.get("*", async (req, res) => {
if (req.path === "/") {
res.write(await ui());
res.end();
return;
}

if (req.path === "/local") {
config.localService = true;
res.write("Locally-served files are now enabled");
console.log("Locally-served files are now enabled");
if (/^\/stop\/?$/i.test(req.path)) {
config.play = false;
config.bundling = false;
res.write(await ui());
res.end();
return;
}

if (req.path === "/record") {
config.toggleRecording();
res.write(`Recording is now ${config.recording ? "en" : "dis"}abled`);
console.log(`Recording is now ${config.recording ? "en" : "dis"}abled`);
if (/^\/play\/.+$/.test(req.path)) {
const bundle = req.path.split("/").pop();
const exists = await fsExists(path.join("./data", bundle));
if (exists) {
config.play = bundle;
config.bundling = false;
res.write(await ui());
res.end();
return;
}

res.write(await ui(`I don't have a bundle ${bundle}`));
res.end();
return;
}

if (req.path === "/bundle") {
if (/^\/bundle\/?$/.test(req.path)) {
config.play = false;
config.bundling = true;
res.write("The next sequence of requests will be recorded and bundled");
console.log("The next sequence of requests will be recorded and bundled");
// res.write("The next sequence of requests will be recorded and bundled");
// console.log("The next sequence of requests will be recorded and bundled");
// res.end();
res.write(await ui());
res.end();
return;
}
Expand All @@ -44,10 +107,10 @@ app.get("*", (req, res) => {
.join("&");
console.log(`REQUEST: ${req.path}${query.length > 0 ? `?${query}` : ""}`);

if (!config.localService) {
proxyToApi(req, res);
if (config.play) {
serveBundle(req, res);
} else {
serveLocally(req, res);
proxyToApi(req, res);
}
});

Expand Down
Loading

0 comments on commit 3fcdf9a

Please sign in to comment.