impr(dev): add configurable server response slowdown

This commit is contained in:
Miodec 2024-03-14 23:04:01 +01:00
parent da7e59a28a
commit 1686287f93
3 changed files with 27 additions and 5 deletions

View file

@ -25,6 +25,8 @@ import {
static as expressStatic,
} from "express";
import { isDevEnvironment } from "../../utils/misc";
import { getLiveConfiguration } from "../../init/configuration";
import Logger from "../../utils/logger";
const pathOverride = process.env["API_PATH_OVERRIDE"];
const BASE_ROUTE = pathOverride !== undefined ? `/${pathOverride}` : "";
@ -57,11 +59,14 @@ function addApiRoutes(app: Application): void {
});
app.use("/configure", expressStatic(join(__dirname, "../../../private")));
//simulate delay for all requests
// app.use(async (req, res, next) => {
// await new Promise((resolve) => setTimeout(resolve, 1000));
// next();
// });
app.use(async (req, res, next) => {
const slowdown = (await getLiveConfiguration()).dev.responseSlowdownMs;
if (slowdown > 0) {
Logger.info(`Simulating ${slowdown}ms delay for ${req.path}`);
await new Promise((resolve) => setTimeout(resolve, slowdown));
}
next();
});
}
// Cannot be added to the route map because it needs to be added before the maintenance handler

View file

@ -5,6 +5,9 @@
*/
export const BASE_CONFIGURATION: SharedTypes.Configuration = {
maintenance: false,
dev: {
responseSlowdownMs: 0,
},
results: {
savingEnabled: false,
objectHashCheckEnabled: false,
@ -151,6 +154,17 @@ export const CONFIGURATION_FORM_SCHEMA: ObjectSchema<SharedTypes.Configuration>
type: "boolean",
label: "In Maintenance",
},
dev: {
type: "object",
label: "Development",
fields: {
responseSlowdownMs: {
type: "number",
label: "Response Slowdown (miliseconds)",
min: 0,
},
},
},
results: {
type: "object",
label: "Results",

View file

@ -14,6 +14,9 @@ declare namespace SharedTypes {
interface Configuration {
maintenance: boolean;
dev: {
responseSlowdownMs: number;
};
quotes: {
reporting: {
enabled: boolean;