app.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. // nodejs modules
  3. const path = require('path'),
  4. fs = require('fs'),
  5. os = require('os');
  6. process.env.NODE_CONFIG_DIR = process.cwd() + '/backend/config';
  7. // npm modules
  8. const express = require('express'),
  9. session = require('express-session'),
  10. mongoose = require('mongoose'),
  11. mongoStore = require('connect-mongo')(session),
  12. bodyParser = require('body-parser'),
  13. config = require('config'),
  14. request = require('request'),
  15. passport = require('passport'),
  16. LocalStrategy = require('passport-local').Strategy,
  17. passportSocketIo = require("passport.socketio");
  18. // custom modules
  19. const global = require('./logic/global'),
  20. coreHandler = require('./logic/coreHandler'),
  21. socketHandler = require('./logic/socketHandler'),
  22. expressHandler = require('./logic/expressHandler');
  23. // database
  24. const MongoDB = mongoose.connect('mongodb://localhost:27017/musare').connection;
  25. MongoDB.on('error', function(err) {
  26. console.log('Database error: ' + err.message);
  27. });
  28. MongoDB.once('open', function() {
  29. console.log('Connected to database');
  30. });
  31. global.db = {
  32. user: require('./schemas/user')(mongoose),
  33. station: require('./schemas/station')(mongoose)
  34. };
  35. // setup express and socket.io
  36. const app = express(MongoDB);
  37. const server = app.listen(80);
  38. global.io = require('socket.io')(server);
  39. app.use(passport.initialize());
  40. app.use(passport.session());
  41. app.use(session({
  42. secret: config.get('secret'),
  43. store: new mongoStore({ mongooseConnection: MongoDB }),
  44. resave: true,
  45. saveUninitialized: true
  46. }));
  47. global.io.use(passportSocketIo.authorize({
  48. secret: config.get('secret'),
  49. store: new mongoStore({ mongooseConnection: MongoDB })
  50. }));
  51. passport.serializeUser(function(user, done) {
  52. done(null, user);
  53. });
  54. passport.deserializeUser(function(user, done) {
  55. done(null, user);
  56. });
  57. passport.use('local-signup', new LocalStrategy(function(username, password, cb) {
  58. process.nextTick(function() {
  59. db.user.findOne({'username' : username }, function(err, user) {
  60. if (err) return cb(err);
  61. if (user) return cb(null, false);
  62. else {
  63. var newUser = new db.user({
  64. username: username
  65. });
  66. newUser.save(function(err) {
  67. if (err) throw err;
  68. return cb(null, newUser);
  69. });
  70. }
  71. });
  72. });
  73. }));
  74. passport.use('local-login', new LocalStrategy(function(username, password, cb) {
  75. process.nextTick(function() {
  76. db.user.findOne({'username' : username }, function(err, user) {
  77. if (err) return cb(err);
  78. if (!user) return cb(null, false);
  79. if (!user.services.token.password == password) return done(null, false);
  80. return done(null, user);
  81. });
  82. });
  83. }));
  84. app.use(bodyParser.json());
  85. app.use(bodyParser.urlencoded({
  86. extended: true
  87. }));
  88. app.use(express.static(__dirname + '/../frontend/build/'));
  89. socketHandler(coreHandler, global.io);
  90. expressHandler(coreHandler, app);