index.ts41 lines, 1.2 KB
| 1 | import { createApp } from '@/app'; |
| 2 | import { createServer } from 'http'; |
| 3 | import { env } from '@/config/env'; |
| 4 | import { logger } from '@/utils/logger'; |
| 5 | import { closeMongoClient, getMongoClient } from '@/clients/mongo.client'; |
| 6 | import { closeRedis, connectRedis } from '@/clients/redis.client'; |
| 7 | import { startConsumers } from '@/messaging/rabbitmq.consumer'; |
| 8 | |
| 9 | const main = async () => { |
| 10 | try { |
| 11 | await Promise.all([getMongoClient(), connectRedis(), startConsumers()]); |
| 12 | |
| 13 | const app = createApp(); |
| 14 | const server = createServer(app); |
| 15 | |
| 16 | const port = env.CHAT_SERVICE_PORT; |
| 17 | |
| 18 | server.listen(port, () => { |
| 19 | logger.info({ port }, 'Chat service is running'); |
| 20 | }); |
| 21 | |
| 22 | const shutdown = () => { |
| 23 | logger.info('Shutting down chat service...'); |
| 24 | Promise.all([closeRedis(), closeMongoClient()]) |
| 25 | .catch((error: unknown) => { |
| 26 | logger.error({ error }, 'Error during shutdown tasks'); |
| 27 | }) |
| 28 | .finally(() => { |
| 29 | server.close(() => process.exit(0)); |
| 30 | }); |
| 31 | }; |
| 32 | |
| 33 | process.on('SIGINT', shutdown); |
| 34 | process.on('SIGTERM', shutdown); |
| 35 | } catch (error) { |
| 36 | logger.error({ error }, 'Failed to start chat service'); |
| 37 | process.exit(1); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | void main(); |