diff --git a/astro.config.mjs b/astro.config.mjs index 94fcadb..b3bf619 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -31,6 +31,9 @@ const site = unwrapEnvVar('SITE_URL', 'https://web-check.xyz'); // The base URL of the site (if serving from a subdirectory) const base = unwrapEnvVar('BASE_URL', '/'); +// Should run the app in boss-mode (requires extra configuration) +const isBossServer = unwrapEnvVar('BOSS_SERVER', false); + // Initialize Astro integrations const integrations = [react(), partytown(), sitemap()]; @@ -62,17 +65,13 @@ console.log( `to help fund maintenance & development.\x1b[0m\n`, ); -const buildOptions = { - output: 'dist', - format: 'esm', -}; +const redirects = {}; - -const redirects = { - '/': '/test', - // '/results/[...target]': '/check/[...target]', // The old path was /results (redirect to maintain compatibility) +// Skip the marketing homepage for self-hosted users +if (isBossServer && isBossServer === 'true') { + redirects['/'] = '/check'; } // Export Astro configuration -export default defineConfig({ output, base, integrations, site, adapter, redirects, buildOptions }); +export default defineConfig({ output, base, integrations, site, adapter, redirects }); diff --git a/package.json b/package.json index 45c7d5e..86e2bf7 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "web-check", + "type": "module", "version": "0.0.1", "homepage": ".", "scripts": { diff --git a/server.js b/server.js index 490fe71..af5c085 100644 --- a/server.js +++ b/server.js @@ -79,54 +79,62 @@ fs.readdirSync(dirPath, { withFileTypes: true }) }); }); - // Create a single API endpoint to execute all lambda functions - app.get('/api', async (req, res) => { - const results = {}; - const { url } = req.query; - const maxExecutionTime = process.env.API_TIMEOUT_LIMIT || 20000; - - const executeHandler = async (handler, req, res) => { - return new Promise(async (resolve, reject) => { - try { - const mockRes = { - status: (statusCode) => mockRes, - json: (body) => resolve({ body }), - }; - await handler({ ...req, query: { url } }, mockRes); - } catch (err) { - reject(err); - } - }); - }; - - const timeout = (ms, jobName = null) => { - return new Promise((_, reject) => { - setTimeout(() => { - reject(new Error( - `Timed out after ${ms/1000} seconds${jobName ? `, when executing ${jobName}` : ''}` - )); - }, ms); - }); - }; - - const handlerPromises = Object.entries(handlers).map(async ([route, handler]) => { - const routeName = route.replace(`${API_DIR}/`, ''); - +// Create a single API endpoint to execute all lambda functions +app.get('/api', async (req, res) => { + const results = {}; + const { url } = req.query; + const maxExecutionTime = process.env.API_TIMEOUT_LIMIT || 20000; + + const executeHandler = async (handler, req, res) => { + return new Promise(async (resolve, reject) => { try { - const result = await Promise.race([ - executeHandler(handler, req, res), - timeout(maxExecutionTime, routeName) - ]); - results[routeName] = result.body; + const mockRes = { + status: (statusCode) => mockRes, + json: (body) => resolve({ body }), + }; + await handler({ ...req, query: { url } }, mockRes); } catch (err) { - results[routeName] = { error: err.message }; + reject(err); } }); - - await Promise.all(handlerPromises); - res.json(results); + }; + + const timeout = (ms, jobName = null) => { + return new Promise((_, reject) => { + setTimeout(() => { + reject(new Error( + `Timed out after ${ms/1000} seconds${jobName ? `, when executing ${jobName}` : ''}` + )); + }, ms); + }); + }; + + const handlerPromises = Object.entries(handlers).map(async ([route, handler]) => { + const routeName = route.replace(`${API_DIR}/`, ''); + + try { + const result = await Promise.race([ + executeHandler(handler, req, res), + timeout(maxExecutionTime, routeName) + ]); + results[routeName] = result.body; + } catch (err) { + results[routeName] = { error: err.message }; + } }); + await Promise.all(handlerPromises); + res.json(results); +}); + +// Skip the marketing homepage, for self-hosted users +app.use((req, res, next) => { + if (req.path === '/' && process.env.BOSS_SERVER !== 'true') { + req.url = '/check'; + } + 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) => {