Public API for a society manager application

index.js 2.5KB

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