Application.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ///<reference path="node_modules/@types/node/index.d.ts"/>
  2. import express = require('express');
  3. import favicon = require('serve-favicon');
  4. import debug = require('debug');
  5. declare let __dirname;
  6. import logger = require('morgan');
  7. import cookieParser = require('cookie-parser');
  8. import session = require('express-session');
  9. import bodyParser = require('body-parser');
  10. import http = require("http");
  11. import {IndexController} from "./routes/IndexController";
  12. import {ConfiguratorDB} from "./modules/DbCrafty/ConfiguratorDB";
  13. import {SecurityController} from "./modules/SecurityCrafty/SecurityController";
  14. import {Security} from "./modules/SecurityCrafty/Security";
  15. import {MemberController} from "./routes/MemberController";
  16. import {GameController} from "./routes/GameController";
  17. import {SessionGame} from "./modules/Server0Game/SessionGame";
  18. export class Application {
  19. private static app;
  20. private static port;
  21. public static launch() {
  22. let app = Application.getApp();
  23. /**
  24. * Get port from environment and store in Express.
  25. */
  26. let port = Application.getPort();
  27. Application.app.set('port', port);
  28. /**
  29. * Create HTTP server.
  30. */
  31. let server = http.createServer(Application.getApp());
  32. /**
  33. * Listen on provided port, on all network interfaces.
  34. */
  35. server.listen(port);
  36. server.on('error', Application.onError);
  37. server.on('listening', Application.onListening);
  38. /**
  39. * View engine setup
  40. */
  41. app.set('views', __dirname + '/views');
  42. app.set('view engine', 'twig');
  43. // uncomment after placing your favicon in /public
  44. //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  45. app.use(logger('dev'));
  46. app.use(bodyParser.json());
  47. app.use(bodyParser.urlencoded({ extended: false }));
  48. app.use(cookieParser());
  49. app.use(express.static(__dirname + '/public'));
  50. //init session
  51. let sess = session({
  52. secret: "sdgdfg3h52dfg2d2gfdfg",
  53. resave: true,
  54. saveUninitialized: true
  55. });
  56. app.use(sess);
  57. //set global user if exist in session and control access
  58. app.use(function(req, res, next) {
  59. Security.controlAccess(req, res, next);
  60. });
  61. //configure DB
  62. ConfiguratorDB.loadModelConfig();
  63. //configure route
  64. Application.configureRoute(app);
  65. // load socket.io and listen
  66. let io = require("socket.io")(server);
  67. //add session value in each new socket
  68. io.use(function(socket, next) {
  69. sess(socket.handshake, {}, next);
  70. });
  71. SessionGame.init(io);
  72. // catch 404 and forward to error handler
  73. /*app.use(function(req, res, next) {
  74. let err = new Error('Not Found');
  75. err.status = 404;
  76. next(err);
  77. });*/
  78. /**
  79. * error handler
  80. */
  81. app.use(function(err, req, res, next) {
  82. // set locals, only providing error in development
  83. res.locals.message = err.message;
  84. res.locals.error = req.app.get('env') === 'development' ? err : {};
  85. // render the error page
  86. res.status(err.status || 500);
  87. res.render('error');
  88. });
  89. }
  90. private static createClassByName(name,...a) {
  91. let c = eval(name);
  92. return new c(...a);
  93. }
  94. /**
  95. * Configure route
  96. */
  97. private static configureRoute(app) {
  98. let router: express.Router;
  99. router = express.Router();
  100. //define route here
  101. IndexController.createRoute(router);
  102. SecurityController.createRoute(router);
  103. MemberController.createRoute(router);
  104. GameController.createRoute(router);
  105. app.use(router);
  106. }
  107. /**
  108. * Normalize a port into a number, string, or false.
  109. */
  110. private static normalizePort(val) {
  111. let port = parseInt(val, 10);
  112. if (isNaN(port)) {
  113. // named pipe
  114. return val;
  115. }
  116. if (port >= 0) {
  117. // port number
  118. return port;
  119. }
  120. return false;
  121. }
  122. /**
  123. * Event listener for HTTP server "listening" event.
  124. */
  125. private static onListening() {
  126. console.log("Server launch :)");
  127. /*let addr = server.address();
  128. let bind = typeof addr === 'string'
  129. ? 'pipe ' + addr
  130. : 'port ' + addr.port;
  131. //debug('Listening on ' + bind);*/
  132. }
  133. /**
  134. * Event listener for HTTP server "error" event.
  135. */
  136. private static onError(error) {
  137. let port = Application.getPort();
  138. if (error.syscall !== 'listen') {
  139. throw error;
  140. }
  141. let bind = typeof port === 'string'
  142. ? 'Pipe ' + port
  143. : 'Port ' + port;
  144. // handle specific listen errors with friendly messages
  145. switch (error.code) {
  146. case 'EACCES':
  147. console.error(bind + ' requires elevated privileges');
  148. //process.exit(1);
  149. break;
  150. case 'EADDRINUSE':
  151. console.error(bind + ' is already in use');
  152. //process.exit(1);
  153. break;
  154. default:
  155. throw error;
  156. }
  157. }
  158. public static getApp() {
  159. if (Application.app == null)
  160. Application.app = express();
  161. return Application.app;
  162. }
  163. private static getPort() {
  164. if (Application.port == null)
  165. Application.port = Application.normalizePort('3000');
  166. return Application.port;
  167. }
  168. }
  169. /**
  170. * Launching Application
  171. */
  172. Application.launch();