diff --git a/server/src/app.ts b/server/src/app.ts index a042e6d..39ffadd 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -4,6 +4,7 @@ import cookie from '@fastify/cookie' import fastifyStatic from '@fastify/static' import path from 'node:path' import { existsSync } from 'node:fs' +import { fileURLToPath } from 'node:url' import type { Config } from './config.js' import { SerialQueue } from './queue.js' import { SyncManager } from './sync.js' @@ -56,7 +57,8 @@ export async function buildApp(opts: AppOptions): Promise { decorateReply: false, }) - const webDist = opts.webDist ?? path.resolve('web/dist') + const moduleDir = path.dirname(fileURLToPath(import.meta.url)) + const webDist = opts.webDist ?? path.resolve(moduleDir, '../../web/dist') const hasWeb = existsSync(webDist) if (hasWeb) { await app.register(fastifyStatic, { root: webDist, prefix: '/' }) diff --git a/server/src/index.ts b/server/src/index.ts index 5ead07d..0d644ed 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -6,3 +6,13 @@ const config = loadConfig() const db = openDatabase(config.dbPath) const app = await buildApp({ db, config }) await app.listen({ port: config.port, host: '0.0.0.0' }) + +for (const signal of ['SIGINT', 'SIGTERM'] as const) { + process.on(signal, () => { + void (async () => { + await app.close() + db.close() + process.exit(0) + })() + }) +}