Public API for a society manager application

index.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var express = require('express');
  2. var serveIndex = require('serve-index');
  3. var https = require('https');
  4. var app = express();
  5. var route_manager = require("./utils/route-manager.js");
  6. var scheduler = require("./presenters/schedule-controller.js");
  7. var bodyParser = require('body-parser');
  8. var basic_auth = require('basic-auth');
  9. var FileStreamRotator = require('file-stream-rotator');
  10. var morgan = require('morgan');
  11. var fs = require('fs');
  12. var logDirectory = 'log'
  13. var production = process.argv[2];
  14. /* istanbul ignore if */
  15. if (production === "-p") {
  16. var log_passwd = fs.readFileSync('logpasswd', 'utf-8');
  17. }
  18. scheduler.register_existing_events();
  19. /* istanbul ignore next */
  20. var auth = function (req, res, next) {
  21. function unauthorized(res) {
  22. res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
  23. return res.sendStatus(401);
  24. };
  25. var user = basic_auth(req);
  26. if (!user || !user.name || !user.pass) {
  27. return unauthorized(res);
  28. };
  29. if (user.name === 'logs' && user.pass === log_passwd.trim()) {
  30. return next();
  31. } else {
  32. return unauthorized(res);
  33. };
  34. };
  35. /*istanbul ignore if*/
  36. if (production === "-p") {
  37. // ensure log directory exists
  38. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
  39. // create a rotating write stream
  40. var accessLogStream = FileStreamRotator.getStream({
  41. date_format: 'YYYYMMDD',
  42. filename: logDirectory + '/access-%DATE%.log',
  43. frequency: 'daily',
  44. verbose: false
  45. })
  46. // setup the logger
  47. app.use(morgan('short', {stream: accessLogStream}))
  48. app.use(morgan('short'));
  49. }
  50. app.use(function(req, res, next) {
  51. res.header("Access-Control-Allow-Origin", "*");
  52. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  53. next();
  54. });
  55. app.use(bodyParser.json()); // for parsing application/json
  56. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  57. app.use('/', route_manager);
  58. app.use('/source', require('magic-window')('/source', { ignore: ['config', 'redis', 'cert.pem', 'key.pem', 'dump.rdb', 'logpasswd'] }))
  59. /* istanbul ignore if */
  60. if (production === "-p") {
  61. app.use('/log', auth, express.static('log'));
  62. app.use('/log', auth, serveIndex('log', {'icons': true}));
  63. }
  64. var server = app.listen(3000, function () {
  65. console.log('Now accepting connections on port 3000.');
  66. });
  67. /* istanbul ignore if */
  68. if (production === "-p") {
  69. var prkey = fs.readFileSync('key.pem');
  70. var certi = fs.readFileSync('cert.pem');
  71. app.listen(80, function() {
  72. console.log('Now accepting connections on port 80.');
  73. });
  74. https.createServer({
  75. key: prkey,
  76. cert: certi
  77. }, app).listen(443, function() {
  78. console.log("Now accepting HTTPS connections on port 443.");
  79. });
  80. }
  81. /* istanbul ignore next */
  82. process.on('SIGINT', function() {
  83. console.log( "\nRecieved Ctrl-C, shutting down." );
  84. process.exit(0);
  85. });
  86. module.exports = {
  87. close: function () {
  88. server.close();
  89. }
  90. };