From 34ca09fc2039b696e87845463de9c7d7dbd59144 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Mon, 20 May 2024 22:43:53 +0100 Subject: [PATCH] Fix catch-all when GUI is disabled --- server.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/server.js b/server.js index af5c085..3c23c05 100644 --- a/server.js +++ b/server.js @@ -1,10 +1,11 @@ -import express from 'express'; + import fs from 'fs'; import path from 'path'; import cors from 'cors'; +import dotenv from 'dotenv'; +import express from 'express'; import rateLimit from 'express-rate-limit'; import historyApiFallback from 'connect-history-api-fallback'; -import dotenv from 'dotenv'; import { handler as ssrHandler } from './dist/server/entry.mjs'; @@ -55,7 +56,7 @@ const limiters = limits.map(limit => rateLimit({ // If rate-limiting enabled, then apply the limiters to the /api endpoint if (process.env.API_ENABLE_RATE_LIMIT === 'true') { - app.use('/api', limiters); + app.use(API_DIR, limiters); } // Read and register each API function as an Express routes @@ -80,7 +81,7 @@ fs.readdirSync(dirPath, { withFileTypes: true }) }); // Create a single API endpoint to execute all lambda functions -app.get('/api', async (req, res) => { +app.get(API_DIR, async (req, res) => { const results = {}; const { url } = req.query; const maxExecutionTime = process.env.API_TIMEOUT_LIMIT || 20000; @@ -129,7 +130,7 @@ app.get('/api', async (req, res) => { // Skip the marketing homepage, for self-hosted users app.use((req, res, next) => { - if (req.path === '/' && process.env.BOSS_SERVER !== 'true') { + if (req.path === '/' && process.env.BOSS_SERVER !== 'true' && !process.env.DISABLE_GUI) { req.url = '/check'; } next(); @@ -137,17 +138,17 @@ app.use((req, res, next) => { // Serve up the GUI - if build dir exists, and GUI feature enabled if (process.env.DISABLE_GUI && process.env.DISABLE_GUI !== 'false') { - app.get('*', async (req, res) => { + app.get('/', async (req, res) => { const placeholderContent = await fs.promises.readFile(placeholderFilePath, 'utf-8'); const htmlContent = placeholderContent.replace( '', 'Web-Check API is up and running!
Access the endpoints at ' - +'/api' + +`${API_DIR}` ); res.status(500).send(htmlContent); }); } else if (!fs.existsSync(guiPath)) { - app.get('*', async (req, res) => { + app.get('/', async (req, res) => { const placeholderContent = await fs.promises.readFile(placeholderFilePath, 'utf-8'); const htmlContent = placeholderContent.replace( '', @@ -159,24 +160,21 @@ if (process.env.DISABLE_GUI && process.env.DISABLE_GUI !== 'false') { } else { // GUI enabled, and build files present, let's go!! app.use(express.static('dist/client/')); app.use((req, res, next) => { - const locals = { - title: 'New title', - }; - ssrHandler(req, res, next, locals); + ssrHandler(req, res, next); }); } // Handle SPA routing app.use(historyApiFallback({ rewrites: [ - { from: /^\/api\/.*$/, to: (context) => context.parsedUrl.path }, - { from: /^.*$/, to: '/index.html' } + { from: new RegExp(`^${API_DIR}/.*$`), to: (context) => context.parsedUrl.path }, + { from: /^.*$/, to: '/index.html' } ] })); // Anything left unhandled (which isn't an API endpoint), return a 404 app.use((req, res, next) => { - if (!req.path.startsWith('/api/')) { + if (!req.path.startsWith(`${API_DIR}/`)) { res.status(404).sendFile(path.join(__dirname, 'public', 'error.html')); } else { next();