Server for my portfolio

index.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // Set up authentication and existing events.
  16. var log_passwd = fs.readFileSync('../ip-project-server/logpasswd', 'utf-8');
  17. var auth = function (req, res, next) {
  18. function unauthorized(res) {
  19. res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
  20. return res.sendStatus(401);
  21. };
  22. var user = basic_auth(req);
  23. if (!user || !user.name || !user.pass) {
  24. return unauthorized(res);
  25. };
  26. if (user.name === 'logs' && user.pass === log_passwd.trim()) {
  27. return next();
  28. } else {
  29. return unauthorized(res);
  30. };
  31. };
  32. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
  33. var accessLogStream = FileStreamRotator.getStream({
  34. date_format: 'YYYYMMDD',
  35. filename: logDirectory + '/access-%DATE%.log',
  36. frequency: 'daily',
  37. verbose: false
  38. })
  39. scheduler.register_existing_events();
  40. // Always use SSL, comes first.
  41. app.use(forceSSL);
  42. app.use(bodyParser.json()); // for parsing application/json
  43. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  44. // Send CORS headers on api route.
  45. app.use('/soc-api/*', function(req, res, next) {
  46. res.header("Access-Control-Allow-Origin", "*");
  47. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  48. next();
  49. });
  50. // Logging comes next
  51. app.use(morgan('short', {stream: accessLogStream}))
  52. app.use(morgan('short'));
  53. // Check the /soc-api/ routes.
  54. app.use('/soc-api/v1', route_manager);
  55. // Serve the log files
  56. app.use('/log', auth, express.static('log'));
  57. app.use('/log', auth, serveIndex('log', {'icons': true}));
  58. // Static site fallback
  59. app.use('/', express.static('../www/'));
  60. // 404 Anything Else
  61. app.use(function (req,res,next) {
  62. res.status(404).sendFile(path.resolve('../www_res/404/index.html'));
  63. });
  64. var prkey = fs.readFileSync('key.pem');
  65. var certi = fs.readFileSync('cert.pem');
  66. var capem = fs.readFileSync('ca.pem');
  67. https.createServer({
  68. key: prkey,
  69. cert: certi,
  70. ca: capem
  71. }, app).listen(443, function() {
  72. console.log('Now accepting HTTPS connections on port 443.');
  73. });
  74. app.listen(80, function () {
  75. console.log('Now accepting HTTP connections on port 80.');
  76. });