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