app.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * 应用程序入口。
  3. */
  4. "use strict";
  5. let express = require('express');
  6. let path = require('path');
  7. let favicon = require('serve-favicon');
  8. let logger = require('morgan');
  9. let cookieParser = require('cookie-parser');
  10. let bodyParser = require('body-parser');
  11. let log = require('./util/log');
  12. let redis = require('redis');
  13. // server configurations
  14. let APIv1 = require('./include/endpoints').APIv1;
  15. let PAGES = require('./include/endpoints').PAGES;
  16. let configFile = require('./include/commons').CONFIG_FILE;
  17. let config = require('./resources/config/' + configFile);
  18. // pages
  19. let index = require('./controllers/index');
  20. let socket = require('./controllers/socket');
  21. // endpoints
  22. let application = require('./endpoints/application.endpoint');
  23. let users = require('./endpoints/users.endpoint');
  24. let groups = require('./endpoints/groups.endpoint');
  25. let chats = require('./endpoints/chats.endpoint');
  26. let sessions = require('./endpoints/session.endpoint');
  27. let management = require('./endpoints/management.endpoint');
  28. // handlers
  29. let SocketHandler = require('./handlers/socket.handler');
  30. // schedule
  31. let PushJobLoader = require('./models/schedule/push.job.loader')
  32. // initialize express application
  33. let app = express();
  34. app.set('port', config.serverPort);
  35. // view engine
  36. app.set('views', path.join(__dirname, 'views'));
  37. app.set('view engine', 'jade');
  38. // logger, body parser, cookie parser and view path
  39. app.use(favicon(__dirname + '/public/favicon.ico', null));
  40. app.use(logger('dev'));
  41. app.use(bodyParser.json());
  42. app.use(bodyParser.urlencoded({extended: true}));
  43. app.use(cookieParser());
  44. app.use(express.static(path.join(__dirname, 'public')));
  45. // pages
  46. app.use(PAGES.Home.Index, index);
  47. app.use(PAGES.Socket.Index, socket);
  48. // setup rest endpoints
  49. app.use(APIv1.Application.Base, application);
  50. app.use(APIv1.Chats.Base, chats);
  51. app.use(APIv1.Users.Base, users);
  52. app.use(APIv1.Groups.Base, groups);
  53. app.use(APIv1.Management.Base, management);
  54. app.use(APIv1.Sessions.Base,sessions);
  55. // error handler, only handle the sync call exception
  56. app.use(function (err, req, res, next) {
  57. if (err) {
  58. res
  59. .status(err.httpStatus || 500)
  60. .send({message: err.message});
  61. }
  62. next(err);
  63. });
  64. // In development mode, error handler will print stacktrace, only handle the sync call exception
  65. if (app.get('env') === 'development') {
  66. app.use(function (err, req, res, next) {
  67. log.error(err);
  68. next(err);
  69. });
  70. }
  71. // only handle the sync call exception
  72. process.on('uncaughtException', function (err) {
  73. console.error(err);
  74. });
  75. // now enable http server and socket.io
  76. let server = require('http').createServer(app);
  77. let io = require('socket.io').listen(server);
  78. server.listen(config.serverPort);
  79. // event listener for HTTP server "error" event
  80. server.on('error', function (error) {
  81. if (error.syscall !== 'listen') throw error;
  82. let bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
  83. // handle specific listen errors with friendly messages
  84. switch (error.code) {
  85. case 'EACCES':
  86. console.error(bind + ' requires elevated privileges');
  87. process.exit(1);
  88. break;
  89. case 'EADDRINUSE':
  90. console.error(bind + ' is already in use');
  91. process.exit(1);
  92. break;
  93. default:
  94. throw error;
  95. }
  96. });
  97. // event listener for HTTP server "listening" event
  98. server.on('listening', function onListening() {
  99. let addr = server.address();
  100. let bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
  101. });
  102. let socketHandler = new SocketHandler(io);
  103. socketHandler.start();
  104. log.info('Starting IM server, version ' + config.version + ', running on port ' + server.address().port + ', ' + new Date().toLocaleString());
  105. log.info('Configuration profile: ' + configFile.split('.')[1]);