fix: server logic

This commit is contained in:
cha0s 2024-07-24 02:23:12 -05:00
parent 52f19b1d89
commit 80c1f78c6a

View File

@ -3,6 +3,9 @@ import compression from 'compression';
import express from 'express'; import express from 'express';
import morgan from 'morgan'; import morgan from 'morgan';
// patch pixi server context
import('./app/server/pixi-context.js');
const isProduction = process.env.NODE_ENV === 'production'; const isProduction = process.env.NODE_ENV === 'production';
const isInsecure = process.env.SILPHIUS_INSECURE_HTTP; const isInsecure = process.env.SILPHIUS_INSECURE_HTTP;
@ -32,6 +35,20 @@ else {
server = createServer(serverOptions, app); server = createServer(serverOptions, app);
} }
// immediately start listening and queueing up connections
let resolve, promise = new Promise((res) => {
resolve = res;
});
app.use(async (req, res, next) => {
await promise;
next();
});
const port = process.env.PORT || 3000;
server.listen(port, () =>
console.log(`Express server listening at http${isInsecure ? '' : 's'}://localhost:${port}`)
);
// possibly load dev server and build the request handler up front
const viteDevServer = isProduction const viteDevServer = isProduction
? undefined ? undefined
: await import('vite').then((vite) => : await import('vite').then((vite) =>
@ -39,26 +56,18 @@ const viteDevServer = isProduction
server: {middlewareMode: {server}}, server: {middlewareMode: {server}},
}) })
); );
const build = () => (
let websocketBuilt = false;
const remixHandler = createRequestHandler({
build: async () => {
// patch pixi server context
import('./app/server/pixi-context.js');
const ssr = await (
viteDevServer viteDevServer
? viteDevServer.ssrLoadModule('virtual:remix/server-build') ? viteDevServer.ssrLoadModule('virtual:remix/server-build')
: import('./build/server/index.js') : import('./build/server/index.js')
); );
if (!websocketBuilt) { const ssr = await build();
await ssr.entry.module.websocket(server, viteDevServer); await ssr.entry.module.websocket(server, viteDevServer);
websocketBuilt = true; const remixHandler = createRequestHandler({
} build: () => ssr,
return ssr;
},
}); });
// configure middleware
app.use(compression()); app.use(compression());
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header // http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
@ -85,7 +94,5 @@ app.use(morgan('tiny'));
// handle SSR requests // handle SSR requests
app.all('*', remixHandler); app.all('*', remixHandler);
const port = process.env.PORT || 3000; // finally let requests resolve
server.listen(port, () => resolve();
console.log(`Express server listening at http${isInsecure ? '' : 's'}://localhost:${port}`)
);