Server for my portfolio

index.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. io.on('connection', function(socket){
  36. console.log('a user connected');
  37. nicklist[socket.id] = getRandomInt(0,99999);
  38. socket.on('chat message', function(msg){
  39. console.log(msg.nick + '#' + nicklist[socket.id] + ': ' + msg.msg);
  40. msg.nick = msg.nick + '#' + nicklist[socket.id];
  41. io.emit('chat message', msg);
  42. });
  43. socket.on('disconnect', function(){
  44. console.log('user disconnected');
  45. delete nicklist[socket.id];
  46. });
  47. });
  48. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
  49. var accessLogStream = FileStreamRotator.getStream({
  50. date_format: 'YYYYMMDD',
  51. filename: logDirectory + '/access-%DATE%.log',
  52. frequency: 'daily',
  53. verbose: false
  54. })
  55. scheduler.register_existing_events();
  56. /*
  57. Set up middleware, included logging and headers.
  58. */
  59. // Always use SSL, comes first.
  60. app.use(forceSSL);
  61. app.use(bodyParser.json()); // for parsing application/json
  62. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  63. // Send CORS headers on api route.
  64. app.use('/soc-api/*', function(req, res, next) {
  65. res.header("Access-Control-Allow-Origin", "*");
  66. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  67. next();
  68. });
  69. // Logging comes next
  70. app.use(morgan('short', {stream: accessLogStream}))
  71. app.use(morgan('short'));
  72. /*
  73. Set up the individual routes for each part of my portfolio
  74. */
  75. // Check the /soc-api/ routes.
  76. app.use('/soc-api/v1', route_manager);
  77. // Serve the log files
  78. app.use('/log', auth, express.static('log'));
  79. app.use('/log', auth, serveIndex('log', {'icons': true}));
  80. // Static site fallback
  81. app.use('/', express.static('../www/'));
  82. // 404 Anything Else
  83. app.use(function (req,res,next) {
  84. res.status(404).sendFile(path.resolve('../www_res/404/index.html'));
  85. });
  86. /*
  87. Below here we set up the servers and choose which ports they will listen on.
  88. */
  89. var prkey = fs.readFileSync('key.pem');
  90. var certi = fs.readFileSync('cert.pem');
  91. var capem = fs.readFileSync('ca.pem');
  92. https.createServer({
  93. key: prkey,
  94. cert: certi,
  95. ca: capem
  96. }, app).listen(443, function() {
  97. console.log('Now accepting HTTPS connections on port 443.');
  98. });
  99. app.listen(80, function () {
  100. console.log('Now accepting HTTP connections on port 80.');
  101. });