Public API for a society manager application

index.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // ensure log directory exists
  36. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
  37. // create a rotating write stream
  38. var accessLogStream = FileStreamRotator.getStream({
  39. date_format: 'YYYYMMDD',
  40. filename: logDirectory + '/access-%DATE%.log',
  41. frequency: 'daily',
  42. verbose: false
  43. })
  44. // setup the logger
  45. app.use(morgan('short', {stream: accessLogStream}))
  46. app.use(morgan('short'));
  47. app.use(function(req, res, next) {
  48. res.header("Access-Control-Allow-Origin", "*");
  49. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  50. next();
  51. });
  52. app.use(bodyParser.json()); // for parsing application/json
  53. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  54. app.use('/', route_manager);
  55. app.use('/source', require('magic-window')('/source', { ignore: ['config', 'redis', 'cert.pem', 'key.pem', 'dump.rdb', 'logpasswd'] }))
  56. /* istanbul ignore if */
  57. if (production === "-p") {
  58. app.use('/log', auth, express.static('log'));
  59. app.use('/log', auth, serveIndex('log', {'icons': true}));
  60. }
  61. app.listen(3000, function () {
  62. console.log('Now accepting connections on port 3000.');
  63. });
  64. /* istanbul ignore if */
  65. if (production === "-p") {
  66. var prkey = fs.readFileSync('key.pem');
  67. var certi = fs.readFileSync('cert.pem');
  68. app.listen(80, function() {
  69. console.log('Now accepting connections on port 80.');
  70. });
  71. https.createServer({
  72. key: prkey,
  73. cert: certi
  74. }, app).listen(443, function() {
  75. console.log("Now accepting HTTPS connections on port 443.");
  76. });
  77. }
  78. process.on('SIGINT', function() {
  79. console.log( "\nRecieved Ctrl-C, shutting down." );
  80. process.exit(0);
  81. })