2024-05-20 23:43:53 +02:00
|
|
|
|
2024-05-06 22:51:46 +02:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import cors from 'cors';
|
2024-05-20 23:43:53 +02:00
|
|
|
import dotenv from 'dotenv';
|
|
|
|
import express from 'express';
|
2024-05-06 22:51:46 +02:00
|
|
|
import rateLimit from 'express-rate-limit';
|
|
|
|
import historyApiFallback from 'connect-history-api-fallback';
|
2023-07-28 22:47:07 +02:00
|
|
|
|
2024-05-06 22:51:46 +02:00
|
|
|
// Load environment variables from .env file
|
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
// Create the Express app
|
2023-07-28 22:47:07 +02:00
|
|
|
const app = express();
|
|
|
|
|
2024-05-06 22:51:46 +02:00
|
|
|
const __filename = new URL(import.meta.url).pathname;
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
2023-09-03 16:30:11 +02:00
|
|
|
const port = process.env.PORT || 3000; // The port to run the server on
|
2023-08-10 00:29:58 +02:00
|
|
|
const API_DIR = '/api'; // Name of the dir containing the lambda functions
|
|
|
|
const dirPath = path.join(__dirname, API_DIR); // Path to the lambda functions dir
|
2024-05-05 18:45:00 +02:00
|
|
|
const guiPath = path.join(__dirname, 'dist', 'client');
|
2023-09-03 17:23:07 +02:00
|
|
|
const placeholderFilePath = path.join(__dirname, 'public', 'placeholder.html');
|
2023-09-03 16:30:11 +02:00
|
|
|
const handlers = {}; // Will store list of API endpoints
|
|
|
|
process.env.WC_SERVER = 'true'; // Tells middleware to return in non-lambda mode
|
2023-08-10 00:29:58 +02:00
|
|
|
|
2023-09-03 15:11:15 +02:00
|
|
|
// Enable CORS
|
|
|
|
app.use(cors({
|
|
|
|
origin: process.env.API_CORS_ORIGIN || '*',
|
|
|
|
}));
|
|
|
|
|
2024-03-14 08:59:19 +01:00
|
|
|
// Define max requests within each time frame
|
|
|
|
const limits = [
|
|
|
|
{ timeFrame: 10 * 60, max: 100, messageTime: '10 minutes' },
|
|
|
|
{ timeFrame: 60 * 60, max: 250, messageTime: '1 hour' },
|
|
|
|
{ timeFrame: 12 * 60 * 60, max: 500, messageTime: '12 hours' },
|
|
|
|
];
|
|
|
|
|
|
|
|
// Construct a message to be returned if the user has been rate-limited
|
|
|
|
const makeLimiterResponseMsg = (retryAfter) => {
|
|
|
|
const why = 'This keeps the service running smoothly for everyone. '
|
|
|
|
+ 'You can get around these limits by running your own instance of Web Check.';
|
|
|
|
return `You've been rate-limited, please try again in ${retryAfter} seconds.\n${why}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create rate limiters for each time frame
|
|
|
|
const limiters = limits.map(limit => rateLimit({
|
|
|
|
windowMs: limit.timeFrame * 1000,
|
|
|
|
max: limit.max,
|
|
|
|
standardHeaders: true,
|
|
|
|
legacyHeaders: false,
|
|
|
|
message: { error: makeLimiterResponseMsg(limit.messageTime) }
|
|
|
|
}));
|
|
|
|
|
|
|
|
// If rate-limiting enabled, then apply the limiters to the /api endpoint
|
|
|
|
if (process.env.API_ENABLE_RATE_LIMIT === 'true') {
|
2024-05-20 23:43:53 +02:00
|
|
|
app.use(API_DIR, limiters);
|
2024-03-14 08:59:19 +01:00
|
|
|
}
|
|
|
|
|
2023-09-03 16:30:11 +02:00
|
|
|
// Read and register each API function as an Express routes
|
|
|
|
fs.readdirSync(dirPath, { withFileTypes: true })
|
2023-08-06 23:04:25 +02:00
|
|
|
.filter(dirent => dirent.isFile() && dirent.name.endsWith('.js'))
|
2024-05-06 22:51:46 +02:00
|
|
|
.forEach(async dirent => {
|
2023-09-03 16:30:11 +02:00
|
|
|
const routeName = dirent.name.split('.')[0];
|
|
|
|
const route = `${API_DIR}/${routeName}`;
|
2024-05-06 22:51:46 +02:00
|
|
|
// const handler = require(path.join(dirPath, dirent.name));
|
|
|
|
|
|
|
|
const handlerModule = await import(path.join(dirPath, dirent.name));
|
|
|
|
const handler = handlerModule.default || handlerModule;
|
2023-09-03 16:30:11 +02:00
|
|
|
handlers[route] = handler;
|
|
|
|
|
|
|
|
app.get(route, async (req, res) => {
|
|
|
|
try {
|
|
|
|
await handler(req, res);
|
|
|
|
} catch (err) {
|
|
|
|
res.status(500).json({ error: err.message });
|
|
|
|
}
|
|
|
|
});
|
2023-08-10 00:29:58 +02:00
|
|
|
});
|
|
|
|
|
2024-06-18 23:06:59 +02:00
|
|
|
const renderPlaceholderPage = async (res, msgId, logs) => {
|
|
|
|
const errorMessages = {
|
|
|
|
notCompiled: 'Looks like the GUI app has not yet been compiled.<br />'
|
|
|
|
+ 'Run <code>yarn build</code> to continue, then restart the server.',
|
|
|
|
notCompiledSsrHandler: 'Server-side rendering failed to initiate, as SSR handler not found.<br />'
|
|
|
|
+ 'This can be fixed by running <code>yarn build</code>, then restarting the server.<br />',
|
|
|
|
disabledGui: 'Web-Check API is up and running!<br />Access the endpoints at '
|
|
|
|
+ `<a href="${API_DIR}"><code>${API_DIR}</code></a>`,
|
|
|
|
};
|
|
|
|
const logOutput = logs ? `<div class="logs"><code>${logs}</code></div>` : '';
|
|
|
|
const errorMessage = (errorMessages[msgId] || 'An mystery error occurred.') + logOutput;
|
|
|
|
const placeholderContent = await fs.promises.readFile(placeholderFilePath, 'utf-8');
|
|
|
|
const htmlContent = placeholderContent.replace('<!-- CONTENT -->', errorMessage );
|
|
|
|
res.status(500).send(htmlContent);
|
|
|
|
};
|
|
|
|
|
2024-05-07 00:29:30 +02:00
|
|
|
// Create a single API endpoint to execute all lambda functions
|
2024-05-20 23:43:53 +02:00
|
|
|
app.get(API_DIR, async (req, res) => {
|
2024-05-07 00:29:30 +02:00
|
|
|
const results = {};
|
|
|
|
const { url } = req.query;
|
|
|
|
const maxExecutionTime = process.env.API_TIMEOUT_LIMIT || 20000;
|
|
|
|
|
2024-06-18 23:06:59 +02:00
|
|
|
const executeHandler = async (handler, req) => {
|
2024-05-07 00:29:30 +02:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2023-09-03 16:30:11 +02:00
|
|
|
try {
|
2024-05-07 00:29:30 +02:00
|
|
|
const mockRes = {
|
2024-06-18 23:06:59 +02:00
|
|
|
status: () => mockRes,
|
2024-05-07 00:29:30 +02:00
|
|
|
json: (body) => resolve({ body }),
|
|
|
|
};
|
|
|
|
await handler({ ...req, query: { url } }, mockRes);
|
2023-09-03 16:30:11 +02:00
|
|
|
} catch (err) {
|
2024-05-07 00:29:30 +02:00
|
|
|
reject(err);
|
2023-09-03 16:30:11 +02:00
|
|
|
}
|
|
|
|
});
|
2024-05-07 00:29:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
2023-07-28 22:47:07 +02:00
|
|
|
});
|
|
|
|
|
2024-05-07 00:29:30 +02:00
|
|
|
await Promise.all(handlerPromises);
|
|
|
|
res.json(results);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Skip the marketing homepage, for self-hosted users
|
|
|
|
app.use((req, res, next) => {
|
2024-05-20 23:43:53 +02:00
|
|
|
if (req.path === '/' && process.env.BOSS_SERVER !== 'true' && !process.env.DISABLE_GUI) {
|
2024-05-07 00:29:30 +02:00
|
|
|
req.url = '/check';
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2023-08-10 22:24:24 +02:00
|
|
|
// Serve up the GUI - if build dir exists, and GUI feature enabled
|
|
|
|
if (process.env.DISABLE_GUI && process.env.DISABLE_GUI !== 'false') {
|
2024-05-20 23:43:53 +02:00
|
|
|
app.get('/', async (req, res) => {
|
2024-06-18 23:06:59 +02:00
|
|
|
renderPlaceholderPage(res, 'disabledGui');
|
2023-08-10 22:24:24 +02:00
|
|
|
});
|
|
|
|
} else if (!fs.existsSync(guiPath)) {
|
2024-05-20 23:43:53 +02:00
|
|
|
app.get('/', async (req, res) => {
|
2024-06-18 23:06:59 +02:00
|
|
|
renderPlaceholderPage(res, 'notCompiled');
|
2024-05-06 22:51:46 +02:00
|
|
|
});
|
2023-08-10 22:24:24 +02:00
|
|
|
} else { // GUI enabled, and build files present, let's go!!
|
2024-05-06 22:51:46 +02:00
|
|
|
app.use(express.static('dist/client/'));
|
2024-06-18 23:06:59 +02:00
|
|
|
app.use(async (req, res, next) => {
|
|
|
|
const ssrHandlerPath = path.join(__dirname, 'dist', 'server', 'entry.mjs');
|
|
|
|
import(ssrHandlerPath).then(({ handler: ssrHandler }) => {
|
|
|
|
ssrHandler(req, res, next);
|
|
|
|
}).catch(async err => {
|
|
|
|
renderPlaceholderPage(res, 'notCompiledSsrHandler', err.message);
|
|
|
|
});
|
2024-05-06 22:51:46 +02:00
|
|
|
});
|
2023-08-10 22:24:24 +02:00
|
|
|
}
|
2023-08-10 00:29:58 +02:00
|
|
|
|
2024-05-06 22:51:46 +02:00
|
|
|
// Handle SPA routing
|
|
|
|
app.use(historyApiFallback({
|
|
|
|
rewrites: [
|
2024-05-20 23:43:53 +02:00
|
|
|
{ from: new RegExp(`^${API_DIR}/.*$`), to: (context) => context.parsedUrl.path },
|
|
|
|
{ from: /^.*$/, to: '/index.html' }
|
2024-05-06 22:51:46 +02:00
|
|
|
]
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Anything left unhandled (which isn't an API endpoint), return a 404
|
2023-09-03 17:23:07 +02:00
|
|
|
app.use((req, res, next) => {
|
2024-05-20 23:43:53 +02:00
|
|
|
if (!req.path.startsWith(`${API_DIR}/`)) {
|
2024-05-06 22:51:46 +02:00
|
|
|
res.status(404).sendFile(path.join(__dirname, 'public', 'error.html'));
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
2023-09-03 17:23:07 +02:00
|
|
|
});
|
|
|
|
|
2023-09-03 16:30:11 +02:00
|
|
|
// Print nice welcome message to user
|
|
|
|
const printMessage = () => {
|
|
|
|
console.log(
|
|
|
|
`\x1b[36m\n` +
|
|
|
|
' __ __ _ ___ _ _ \n' +
|
|
|
|
' \\ \\ / /__| |__ ___ / __| |_ ___ __| |__\n' +
|
|
|
|
' \\ \\/\\/ / -_) \'_ \\___| (__| \' \\/ -_) _| / /\n' +
|
|
|
|
' \\_/\\_/\\___|_.__/ \\___|_||_\\___\\__|_\\_\\\n' +
|
|
|
|
`\x1b[0m\n`,
|
|
|
|
`\x1b[1m\x1b[32m🚀 Web-Check is up and running at http://localhost:${port} \x1b[0m\n\n`,
|
|
|
|
`\x1b[2m\x1b[36m🛟 For documentation and support, visit the GitHub repo: ` +
|
|
|
|
`https://github.com/lissy93/web-check \n`,
|
|
|
|
`💖 Found Web-Check useful? Consider sponsoring us on GitHub ` +
|
|
|
|
`to help fund maintenance & development.\x1b[0m`
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create server
|
|
|
|
app.listen(port, () => {
|
|
|
|
printMessage();
|
2023-07-28 22:47:07 +02:00
|
|
|
});
|
|
|
|
|