From efc3b46c2b38ae00c142e56ffd184d20fc2fd399 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 17 Nov 2023 15:23:51 -0500 Subject: [PATCH] fix(striker-ui-api): wait for access module setup before starting express app --- striker-ui-api/src/index.ts | 61 +++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/striker-ui-api/src/index.ts b/striker-ui-api/src/index.ts index ca53409a..d11b0662 100644 --- a/striker-ui-api/src/index.ts +++ b/striker-ui-api/src/index.ts @@ -2,28 +2,43 @@ import { getgid, getuid, setgid, setuid } from 'process'; import { PGID, PUID, PORT, ECODE_DROP_PRIVILEGES } from './lib/consts'; -import app from './app'; -import { proxyServerVncUpgrade } from './middlewares'; +import { access } from './lib/accessModule'; import { stderr, stdout } from './lib/shell'; -(async () => { - stdout(`Starting main process with ownership ${getuid()}:${getgid()}`); - - const server = (await app).listen(PORT, () => { - try { - // Group must be set before user to avoid permission error. - setgid(PGID); - setuid(PUID); - - stdout(`Main process ownership changed to ${getuid()}:${getgid()}.`); - } catch (error) { - stderr(`Failed to change main process ownership; CAUSE: ${error}`); - - process.exit(ECODE_DROP_PRIVILEGES); - } - - stdout(`Listening on localhost:${PORT}.`); - }); - - server.on('upgrade', proxyServerVncUpgrade); -})(); +/** + * Wait until the anvil-access-module daemon finishes its setup before doing + * anything else. + * + * Notes: + * * The webpackMode directive tells webpack to include the dynamic module into + * the main bundle. Webpack defaults to put such modules in separate files to + * reduce the amount of loading. + */ +access.once('active', async () => { + const { default: app } = await import(/* webpackMode: "eager" */ './app'); + const { proxyServerVncUpgrade } = await import( + /* webpackMode: "eager" */ './middlewares' + ); + + (async () => { + stdout(`Starting main process with ownership ${getuid()}:${getgid()}`); + + const server = (await app).listen(PORT, () => { + try { + // Group must be set before user to avoid permission error. + setgid(PGID); + setuid(PUID); + + stdout(`Main process ownership changed to ${getuid()}:${getgid()}.`); + } catch (error) { + stderr(`Failed to change main process ownership; CAUSE: ${error}`); + + process.exit(ECODE_DROP_PRIVILEGES); + } + + stdout(`Listening on localhost:${PORT}.`); + }); + + server.on('upgrade', proxyServerVncUpgrade); + })(); +});