Server for my portfolio

index.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var fs = require('fs');
  2. var https = require('https');
  3. var express = require('express');
  4. var path = require('path');
  5. var forceSSL = require('express-force-ssl');
  6. var bodyParser = require('body-parser');
  7. var serveIndex = require('serve-index');
  8. var basic_auth = require('basic-auth');
  9. var FileStreamRotator = require('file-stream-rotator');
  10. var morgan = require('morgan');
  11. var route_manager = require("../ip-project-server/utils/route-manager.js");
  12. var scheduler = require("../ip-project-server/presenters/schedule-controller.js");
  13. var app = express();
  14. var logDirectory = 'log'
  15. /*
  16. Basic administration stuff
  17. */
  18. // Set up authentication and existing events.
  19. var log_passwd = fs.readFileSync('../ip-project-server/logpasswd', 'utf-8');
  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. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
  36. var accessLogStream = FileStreamRotator.getStream({
  37. date_format: 'YYYYMMDD',
  38. filename: logDirectory + '/access-%DATE%.log',
  39. frequency: 'daily',
  40. verbose: false
  41. })
  42. scheduler.register_existing_events();
  43. /*
  44. Set up middleware, included logging and headers.
  45. */
  46. // Always use SSL, comes first.
  47. app.use(forceSSL);
  48. app.use(bodyParser.json()); // for parsing application/json
  49. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  50. // Send CORS headers on api route.
  51. app.use('/soc-api/*', function(req, res, next) {
  52. res.header("Access-Control-Allow-Origin", "*");
  53. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  54. next();
  55. });
  56. // Logging comes next
  57. app.use(morgan('short', {stream: accessLogStream}))
  58. app.use(morgan('short'));
  59. /*
  60. Set up the individual routes for each part of my portfolio
  61. */
  62. // Check the /soc-api/ routes.
  63. app.use('/soc-api/v1', route_manager);
  64. // Serve the log files
  65. app.use('/log', auth, express.static('log'));
  66. app.use('/log', auth, serveIndex('log', {'icons': true}));
  67. // Static site fallback
  68. app.use('/', express.static('../www/'));
  69. // 404 Anything Else
  70. app.use(function (req,res,next) {
  71. res.status(404).sendFile(path.resolve('../www_res/404/index.html'));
  72. });
  73. /*
  74. Below here we set up the servers and choose which ports they will listen on.
  75. */
  76. var prkey = fs.readFileSync('key.pem');
  77. var certi = fs.readFileSync('cert.pem');
  78. var capem = fs.readFileSync('ca.pem');
  79. https.createServer({
  80. key: prkey,
  81. cert: certi,
  82. ca: capem
  83. }, app).listen(443, function() {
  84. console.log('Now accepting HTTPS connections on port 443.');
  85. });
  86. app.listen(80, function () {
  87. console.log('Now accepting HTTP connections on port 80.');
  88. });