Ship Novya PWA with backend for deployable production use.
Fix production TypeScript builds and add DEPLOY.md so the app can be hosted from this repo. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import { serve, type ServerType } from '@hono/node-server';
|
||||
import { createApp } from './app.js';
|
||||
import { env } from './config/index.js';
|
||||
import { connectPrisma, disconnectPrisma } from './lib/prisma.js';
|
||||
import { flushLogger, logger } from './lib/logger.js';
|
||||
|
||||
const SHUTDOWN_SIGNALS: NodeJS.Signals[] = ['SIGINT', 'SIGTERM'];
|
||||
|
||||
let server: ServerType | undefined;
|
||||
let isShuttingDown = false;
|
||||
|
||||
async function startServer() {
|
||||
const app = createApp();
|
||||
|
||||
try {
|
||||
await connectPrisma();
|
||||
logger.info('database connection established');
|
||||
const { seedCourseSessions } = await import('./modules/sessions/sessions.service.js');
|
||||
await seedCourseSessions();
|
||||
logger.info('course sessions seeded');
|
||||
} catch (err) {
|
||||
logger.warn({ err }, 'database connection failed at startup; /ready will report unavailable');
|
||||
}
|
||||
|
||||
server = serve(
|
||||
{
|
||||
fetch: app.fetch,
|
||||
hostname: env.HOST,
|
||||
port: env.PORT,
|
||||
},
|
||||
(info) => {
|
||||
logger.info(
|
||||
{
|
||||
host: info.address,
|
||||
port: info.port,
|
||||
env: env.NODE_ENV,
|
||||
trustProxy: env.TRUST_PROXY,
|
||||
},
|
||||
'server started',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
server.on('error', (err: NodeJS.ErrnoException) => {
|
||||
if (err.code === 'EADDRINUSE') {
|
||||
logger.fatal(
|
||||
{ port: env.PORT, err },
|
||||
`Port ${env.PORT} is already in use. Stop the other process or run "npm run dev" from the project root (predev frees the port automatically).`,
|
||||
);
|
||||
} else {
|
||||
logger.fatal({ err }, 'failed to start HTTP server');
|
||||
}
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
registerShutdownHandlers();
|
||||
}
|
||||
|
||||
function registerShutdownHandlers() {
|
||||
for (const signal of SHUTDOWN_SIGNALS) {
|
||||
process.on(signal, () => {
|
||||
void shutdown(signal);
|
||||
});
|
||||
}
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
logger.fatal({ err }, 'uncaught exception');
|
||||
void shutdown('uncaughtException', 1);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason) => {
|
||||
logger.fatal({ err: reason }, 'unhandled rejection');
|
||||
void shutdown('unhandledRejection', 1);
|
||||
});
|
||||
}
|
||||
|
||||
async function shutdown(reason: string, exitCode = 0) {
|
||||
if (isShuttingDown) {
|
||||
return;
|
||||
}
|
||||
|
||||
isShuttingDown = true;
|
||||
logger.info({ reason }, 'shutdown initiated');
|
||||
|
||||
const forceExitTimer = setTimeout(() => {
|
||||
logger.error('forced shutdown after timeout');
|
||||
process.exit(1);
|
||||
}, 10_000);
|
||||
|
||||
forceExitTimer.unref();
|
||||
|
||||
try {
|
||||
if (server) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server!.close((err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
logger.info('http server closed');
|
||||
}
|
||||
|
||||
await disconnectPrisma();
|
||||
logger.info('database connection closed');
|
||||
|
||||
await flushLogger();
|
||||
logger.info('logger flushed');
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'error during shutdown');
|
||||
exitCode = 1;
|
||||
} finally {
|
||||
clearTimeout(forceExitTimer);
|
||||
process.exit(exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
void startServer();
|
||||
Reference in New Issue
Block a user