Refactor whitelist middleware to return a promise and update server.js to handle async initialization
| @@ -41,7 +41,7 @@ import { | ||
| 41 | 41 | |
| 42 | 42 | import getWebpackServeMiddleware from './src/middleware/webpack-serve.js'; |
| 43 | 43 | import basicAuthMiddleware from './src/middleware/basicAuth.js'; |
| 44 | 44 | import whitelistMiddlewaregetWhitelistMiddleware from './src/middleware/whitelist.js'; |
| 45 | 45 | import accessLoggerMiddleware, { getAccessLogPath, migrateAccessLog } from './src/middleware/accessLogWriter.js'; |
| 46 | 46 | import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js'; |
| 47 | 47 | import initRequestProxy from './src/request-proxy.js'; |
| @@ -125,7 +125,8 @@ if (cliArgs.listen && cliArgs.basicAuthMode) { | ||
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | if (cliArgs.whitelistMode) { |
| 128 | - app.use(whitelistMiddleware()); | |
| 128 | + const whitelistMiddleware = await getWhitelistMiddleware(); | |
| 129 | + app.use(whitelistMiddleware); | |
| 129 | 130 | } |
| 130 | 131 | |
| 131 | 132 | if (cliArgs.listen) { |
| @@ -22,8 +22,6 @@ if (fs.existsSync(whitelistPath)) { | ||
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | -await resolveHostnames(); | |
| 26 | - | |
| 27 | 25 | /** |
| 28 | 26 | * Get the client IP address from the request headers. |
| 29 | 27 | * @param {import('express').Request} req Express request object |
| @@ -110,9 +108,9 @@ async function resolveHostnames() { | ||
| 110 | 108 | |
| 111 | 109 | /** |
| 112 | 110 | * Returns a middleware function that checks if the client IP is in the whitelist. |
| 113 | 111 | * @returns {Promise<import('express').RequestHandler>} ThePromise that resolves to the middleware function |
| 114 | 112 | */ |
| 115 | 113 | export default async function whitelistMiddlewaregetWhitelistMiddleware() { |
| 116 | 114 | const forbiddenWebpage = Handlebars.compile( |
| 117 | 115 | safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '', |
| 118 | 116 | ); |
| @@ -121,6 +119,8 @@ export default function whitelistMiddleware() { | ||
| 121 | 119 | '/favicon.ico', |
| 122 | 120 | ]; |
| 123 | 121 | |
| 122 | + await resolveHostnames(); | |
| 123 | + | |
| 124 | 124 | return function (req, res, next) { |
| 125 | 125 | const clientIp = getIpFromRequest(req); |
| 126 | 126 | const forwardedIp = getForwardedIp(req); |