import { initSentry, Sentry } from './sentry.js';
// Sentry deve ser inicializado ANTES de qualquer outra importacao que possa lancar
initSentry();
import { buildApp } from './app.js';
import { env } from './env.js';
import { logger } from './logger.js';
import { closeRedis } from './redis.js';
import { closeDb } from './db.js';
async function main() {
    const app = await buildApp();
    // Captura erros do Fastify e envia ao Sentry
    app.setErrorHandler((err, _req, reply) => {
        Sentry.captureException(err);
        reply.send(err);
    });
    try {
        await app.listen({ host: '0.0.0.0', port: env.CONTROL_PLANE_PORT });
        logger.info({ port: env.CONTROL_PLANE_PORT }, 'control-plane listening');
    }
    catch (err) {
        logger.error({ err }, 'failed to start');
        process.exit(1);
    }
    const shutdown = async (signal) => {
        logger.info({ signal }, 'shutting down');
        await app.close();
        await closeRedis();
        await closeDb();
        process.exit(0);
    };
    process.on('SIGINT', () => void shutdown('SIGINT'));
    process.on('SIGTERM', () => void shutdown('SIGTERM'));
}
main().catch((err) => {
    logger.error({ err }, 'fatal');
    process.exit(1);
});
//# sourceMappingURL=index.js.map