diff options
author | Raphael Kabo <raphaelkabo@hey.com> | 2024-02-26 12:09:46 +0000 |
---|---|---|
committer | Raphael Kabo <raphaelkabo@hey.com> | 2024-02-26 12:09:46 +0000 |
commit | c93fd6e2d455ea4208f9e5ca6bfbd1c0e9fd1ad9 (patch) | |
tree | 8baed2c232b629a98b21006adf440f6f83d5da57 /src/lib | |
parent | 363cfbae076f4494ddd5fddcf03de622f6247051 (diff) |
refactor: allow Cypress to override config
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/config.ts | 20 | ||||
-rw-r--r-- | src/lib/middleware.ts | 29 |
2 files changed, 41 insertions, 8 deletions
diff --git a/src/lib/config.ts b/src/lib/config.ts index b4385ca..4bc43bd 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -1,6 +1,7 @@ import fs from "fs"; import toml from "toml"; import { exitWithError } from "./process.js"; +import { Response } from "express"; interface StaticPage { title: string; @@ -8,7 +9,7 @@ interface StaticPage { filename: string; } -interface GathioConfig { +export interface GathioConfig { general: { domain: string; port: string; @@ -68,8 +69,21 @@ const defaultConfig: GathioConfig = { }, }; -export const frontendConfig = (): FrontendConfig => { - const config = getConfig(); +export const frontendConfig = (res: Response): FrontendConfig => { + const config = res.locals.config; + if (!config) { + return { + domain: defaultConfig.general.domain, + siteName: defaultConfig.general.site_name, + isFederated: defaultConfig.general.is_federated, + emailLogoUrl: defaultConfig.general.email_logo_url, + showPublicEventList: defaultConfig.general.show_public_event_list, + showKofi: defaultConfig.general.show_kofi, + showInstanceInformation: false, + staticPages: [], + version: process.env.npm_package_version || "unknown", + }; + } return { domain: config.general.domain, siteName: config.general.site_name, diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index 0594e90..5073137 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -1,14 +1,14 @@ -import { Request, Response } from "express"; +import { NextFunction, Request, Response } from "express"; import MagicLink from "../models/MagicLink.js"; -import getConfig from "../lib/config.js"; - -const config = getConfig(); +import getConfig, { GathioConfig } from "../lib/config.js"; +import { deepMerge } from "../util/object.js"; export const checkMagicLink = async ( req: Request, res: Response, - next: any, + next: NextFunction, ) => { + const config = getConfig(); if (!config.general.creator_email_addresses?.length) { // No creator email addresses are configured, so skip the magic link check return next(); @@ -49,3 +49,22 @@ export const checkMagicLink = async ( } next(); }; + +// Route-specific middleware which injects the config into the request object +// It can also be used to modify the config based on the request, which +// we use for Cypress testing. +export const getConfigMiddleware = ( + req: Request, + res: Response, + next: NextFunction, +) => { + const config = getConfig(); + if (process.env.CYPRESS === "true" && req.cookies?.cypressConfigOverride) { + console.log("Overriding config with Cypress config"); + const override = JSON.parse(req.cookies.cypressConfigOverride); + res.locals.config = deepMerge<GathioConfig>(config, override); + return next(); + } + res.locals.config = config; + return next(); +}; |