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 morgan from 'morgan';
// patch pixi server context
import('./app/server/pixi-context.js');
const isProduction = process.env.NODE_ENV === 'production';
const isInsecure = process.env.SILPHIUS_INSECURE_HTTP;
@ -32,6 +35,20 @@ else {
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
? undefined
: await import('vite').then((vite) =>
@ -39,26 +56,18 @@ const viteDevServer = isProduction
server: {middlewareMode: {server}},
})
);
let websocketBuilt = false;
const remixHandler = createRequestHandler({
build: async () => {
// patch pixi server context
import('./app/server/pixi-context.js');
const ssr = await (
const build = () => (
viteDevServer
? viteDevServer.ssrLoadModule('virtual:remix/server-build')
: import('./build/server/index.js')
);
if (!websocketBuilt) {
const ssr = await build();
await ssr.entry.module.websocket(server, viteDevServer);
websocketBuilt = true;
}
return ssr;
},
const remixHandler = createRequestHandler({
build: () => ssr,
});
// configure middleware
app.use(compression());
// 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
app.all('*', remixHandler);
const port = process.env.PORT || 3000;
server.listen(port, () =>
console.log(`Express server listening at http${isInsecure ? '' : 's'}://localhost:${port}`)
);
// finally let requests resolve
resolve();