Public API for a society manager application

index.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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', 'logpasswd'] }))
  52. if (production === "-p") {
  53. app.use('/log', auth, express.static('log'));
  54. app.use('/log', auth, serveIndex('log', {'icons': true}));
  55. }
  56. app.listen(3000, function () {
  57. console.log('Now accepting connections on port 3000.');
  58. });
  59. if (production === "-p") {
  60. var prkey = fs.readFileSync('key.pem');
  61. var certi = fs.readFileSync('cert.pem');
  62. app.listen(80, function() {
  63. console.log('Now accepting connections on port 80.');
  64. });
  65. https.createServer({
  66. key: prkey,
  67. cert: certi
  68. }, app).listen(443, function() {
  69. console.log("Now accepting HTTPS connections on port 443.");
  70. });
  71. }
  72. process.on('SIGINT', function() {
  73. console.log( "\nRecieved Ctrl-C, shutting down." );
  74. process.exit(0);
  75. })