Server for my portfolio

index.js 2.7KB

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