|
| 1 | +import colors from 'colors'; |
| 2 | +import mongoose from 'mongoose'; |
| 3 | +import { Server } from 'socket.io'; |
| 4 | +import app from './app'; |
| 5 | +import config from './config'; |
| 6 | +import { seedSuperAdmin } from './DB/seedAdmin'; |
| 7 | +import { socketHelper } from './helpers/socketHelper'; |
| 8 | +import { errorLogger, logger } from './shared/logger'; |
| 9 | + |
| 10 | +//uncaught exception |
| 11 | +import process from 'process'; |
| 12 | + |
| 13 | +process.on('uncaughtException', error => { |
| 14 | + errorLogger.error('UnhandleException Detected', error); |
| 15 | + process.exit(1); |
| 16 | +}); |
| 17 | + |
| 18 | +let server: any; |
| 19 | +async function main() { |
| 20 | + try { |
| 21 | + mongoose.connect(config.database_url as string); |
| 22 | + logger.info(colors.green('🚀 Database connected successfully')); |
| 23 | + |
| 24 | + //Seed Super Admin after database connection is successful |
| 25 | + await seedSuperAdmin(); |
| 26 | + |
| 27 | + const port = |
| 28 | + typeof config.port_dev === 'number' ? config.port_dev : Number(config.port_dev); |
| 29 | + |
| 30 | + server = app.listen(port, config.ip_address as string, () => { |
| 31 | + logger.info( |
| 32 | + colors.yellow(`♻️ Application listening on port:${port}`) |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + //socket |
| 37 | + const io = new Server(server, { |
| 38 | + pingTimeout: 60000, |
| 39 | + cors: { |
| 40 | + origin: '*', |
| 41 | + }, |
| 42 | + }); |
| 43 | + socketHelper.socket(io); |
| 44 | + //@ts-ignore |
| 45 | + global.io = io; |
| 46 | + } catch (error) { |
| 47 | + console.error(error); |
| 48 | + errorLogger.error(colors.red('🤢 Failed to connect Database')); |
| 49 | + } |
| 50 | + |
| 51 | + //handle unhandleRejection |
| 52 | + process.on('unhandledRejection', error => { |
| 53 | + if (server) { |
| 54 | + server.close(() => { |
| 55 | + errorLogger.error('UnhandleRejection Detected', error); |
| 56 | + process.exit(1); |
| 57 | + }); |
| 58 | + } else { |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +main(); |
| 65 | + |
| 66 | +//SIGTERM |
| 67 | +process.on('SIGTERM', () => { |
| 68 | + logger.info('SIGTERM IS RECEIVE'); |
| 69 | + if (server) { |
| 70 | + server.close(); |
| 71 | + } |
| 72 | +}); |
0 commit comments