Server for my portfolio

index.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 basic_auth = require('basic-auth');
  8. var FileStreamRotator = require('file-stream-rotator');
  9. var morgan = require('morgan');
  10. var logDirectory = 'log'
  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. // Set up authentication and existing events.
  15. var log_passwd = fs.readFileSync('../ip-project-server/logpasswd', 'utf-8');
  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. scheduler.register_existing_events();
  32. // Always use SSL, comes first.
  33. app.use(forceSSL);
  34. app.use(bodyParser.json()); // for parsing application/json
  35. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  36. // Send CORS headers on api route.
  37. app.use('/soc-api/*', function(req, res, next) {
  38. res.header("Access-Control-Allow-Origin", "*");
  39. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  40. next();
  41. });
  42. // Logging comes next
  43. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
  44. var accessLogStream = FileStreamRotator.getStream({
  45. date_format: 'YYYYMMDD',
  46. filename: logDirectory + '/access-%DATE%.log',
  47. frequency: 'daily',
  48. verbose: false
  49. })
  50. app.use(morgan('short', {stream: accessLogStream}))
  51. app.use(morgan('short'));
  52. // Check the /soc-api/ routes.
  53. app.use('/soc-api/v1/', route_manager);
  54. // Serve the log files
  55. app.use('/log', auth, express.static('log'));
  56. app.use('/log', auth, serveIndex('log', {'icons': true}));
  57. // Static site fallback
  58. app.use('/', express.static('../www/'));
  59. // 404 Anything Else
  60. app.use(function (req,res,next) {
  61. res.status(404).sendFile(path.resolve('../www_res/404/index.html'));
  62. });
  63. var prkey = fs.readFileSync('key.pem');
  64. var certi = fs.readFileSync('cert.pem');
  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. app.listen(80, function () {
  72. console.log('Now accepting HTTP connections on port 80.');
  73. });