import path from 'node:path' import { fileURLToPath } from 'node:url' import { buildApp } from './app.js' import { FileConfigStore } from './config/file-config-store.js' import { ClientRegistry } from './clients/client-registry.js' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') const configPath = process.env.MULTI_SIMADMIN_CONFIG || path.join(root, 'config.json') const examplePath = path.join(root, 'config.example.json') const configStore = await FileConfigStore.open({ configPath, examplePath }) const clientRegistry = new ClientRegistry().reconcile(configStore.snapshot.instances) const app = await buildApp({ configStore, clientRegistry, logger: true }) let closing = false async function shutdown(signal) { if (closing) return closing = true app.log.info({ signal }, 'shutting down') await app.close() } for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => shutdown(signal).catch(error => { app.log.error(error); process.exitCode = 1 })) try { await app.listen({ ...configStore.snapshot.server }) app.log.info(`Loaded ${configStore.snapshot.instances.length} instance(s) from ${configPath}`) } catch (error) { app.log.error(error) await app.close().catch(() => {}) process.exitCode = 1 }