Server for my portfolio

index.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 basic_auth = require('basic-auth');
  10. var FileStreamRotator = require('file-stream-rotator');
  11. var morgan = require('morgan');
  12. var fs = require('fs');
  13. var logDirectory = 'log'
  14. var app = express();
  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. scheduler.register_existing_events();
  33. // Always use SSL, comes first.
  34. app.use(forceSSL);
  35. app.use(bodyParser.json()); // for parsing application/json
  36. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  37. // Send CORS headers on api route.
  38. app.use('/soc-api/*', function(req, res, next) {
  39. res.header("Access-Control-Allow-Origin", "*");
  40. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  41. next();
  42. });
  43. // Logging comes next
  44. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
  45. var accessLogStream = FileStreamRotator.getStream({
  46. date_format: 'YYYYMMDD',
  47. filename: logDirectory + '/access-%DATE%.log',
  48. frequency: 'daily',
  49. verbose: false
  50. })
  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. https.createServer({
  67. key: prkey,
  68. cert: certi
  69. }, app).listen(443, function() {
  70. console.log('Now accepting HTTPS connections on port 443.');
  71. });
  72. app.listen(80, function () {
  73. console.log('Now accepting HTTP connections on port 80.');
  74. });