import { serve } from '@hono/node-server' import { serveStatic } from '@hono/node-server/serve-static' import { existsSync } from 'node:fs' import { join } from 'node:path' import { createApp } from './app.ts' import { loadConfig } from './config.ts' import { openDatabase } from './db/client.ts' const config = loadConfig() const database = await openDatabase(config.databaseUrl) const app = createApp({ db: database.db, config }) // In production the server also serves the built web app. if (existsSync(join(config.staticDir, 'index.html'))) { app.use( '/*', serveStatic({ root: config.staticDir, onFound: (path, c) => { if (path.includes('/assets/')) c.header('Cache-Control', 'public, max-age=31536000, immutable') }, }), ) app.get('*', serveStatic({ path: join(config.staticDir, 'index.html') })) // client-side routes } const server = serve({ fetch: app.fetch, port: config.port }, ({ port }) => { console.log(`Open SafetyLog EHS listening on port ${port}, public URL ${config.publicUrl} (database: ${config.databaseUrl.replace(/\/\/[^@]*@/, '//***@')})`) }) for (const signal of ['SIGINT', 'SIGTERM'] as const) { process.on(signal, () => { server.close(async () => { await database.close() process.exit(0) }) }) }