fix: robust web/dist path resolution and graceful shutdown
This commit is contained in:
@@ -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<FastifyInstance> {
|
||||
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: '/' })
|
||||
|
||||
@@ -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)
|
||||
})()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user