server.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var express = require('express');
  2. var path = require('path');
  3. var logger = require('morgan');
  4. var compression = require('compression');
  5. var methodOverride = require('method-override');
  6. var session = require('express-session');
  7. var flash = require('express-flash');
  8. var bodyParser = require('body-parser');
  9. var expressValidator = require('express-validator');
  10. var dotenv = require('dotenv');
  11. var nunjucks = require('nunjucks');
  12. var mongoose = require('mongoose');
  13. var passport = require('passport');
  14. var wildcardSubdomains = require('wildcard-subdomains');
  15. // Load environment variables from .env file
  16. dotenv.load();
  17. // Controllers
  18. var HomeController = require('./controllers/home');
  19. var userController = require('./controllers/user');
  20. var contactController = require('./controllers/contact');
  21. var campanhaController = require('./controllers/campanha');
  22. var uploadController = require('./controllers/upload');
  23. var sobreController = require('./controllers/sobre');
  24. var privacidadeController = require('./controllers/privacidade');
  25. var timeController = require('./controllers/time');
  26. var termosController = require('./controllers/termos');
  27. var voluntariadoController = require('./controllers/voluntariado');
  28. var faqController = require('./controllers/faq');
  29. // Passport OAuth strategies
  30. require('./config/passport');
  31. var app = express();
  32. mongoose.connect(process.env.DB_PATH);
  33. mongoose.connection.on('error', function() {
  34. console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
  35. process.exit(1);
  36. });
  37. // view engine setup
  38. nunjucks.configure('views', {
  39. autoescape: true,
  40. express: app
  41. });
  42. app.set('view engine', 'html');
  43. app.set('port', process.env.PORT || 3321);
  44. app.use(compression());
  45. app.use(logger('dev'));
  46. app.use(bodyParser.json());
  47. app.use(bodyParser.urlencoded({ extended: false }));
  48. app.use(expressValidator());
  49. app.use(methodOverride('_method'));
  50. app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
  51. app.use(flash());
  52. app.use(passport.initialize());
  53. app.use(passport.session());
  54. app.use(function(req, res, next) {
  55. res.locals.user = req.user;
  56. res.locals.baseurl = process.env.BASEURL;
  57. next();
  58. });
  59. app.use(express.static(path.join(__dirname, 'public')));
  60. //routers
  61. // Home
  62. app.get('/', HomeController.index);
  63. app.get('/novacampanha', userController.ensureAuthenticated, campanhaController.newCampanhaGet);
  64. app.post('/novacampanha', userController.ensureAuthenticated, campanhaController.newCampanhaPost);
  65. app.delete('/minhascampanhas', userController.ensureAuthenticated, campanhaController.campanhaDelete);
  66. //upload
  67. app.post('/upload', userController.ensureAuthenticated, uploadController.uploadImage);
  68. uploadDir = path.join(__dirname, '/public/media/');
  69. app.get('/minhascampanhas', campanhaController.minhascampanhasGet);
  70. app.get('/sobre', sobreController.index);
  71. app.get('/privacidade', privacidadeController.index);
  72. app.get('/time', timeController.index);
  73. app.get('/termos', termosController.index);
  74. app.get('/voluntariado', voluntariadoController.index);
  75. app.get('/faq', faqController.index);
  76. app.get('/contato', contactController.contactGet);
  77. app.post('/contato', contactController.contactPost);
  78. app.get('/account', userController.ensureAuthenticated, userController.accountGet);
  79. app.put('/account', userController.ensureAuthenticated, userController.accountPut);
  80. app.delete('/account', userController.ensureAuthenticated, userController.accountDelete);
  81. app.get('/signup', userController.signupGet);
  82. app.post('/signup', userController.signupPost);
  83. app.get('/login', userController.loginGet);
  84. app.post('/login', userController.loginPost);
  85. app.get('/forgot', userController.forgotGet);
  86. app.post('/forgot', userController.forgotPost);
  87. app.get('/reset/:token', userController.resetGet);
  88. app.post('/reset/:token', userController.resetPost);
  89. app.get('/logout', userController.logout);
  90. app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink);
  91. app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
  92. app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
  93. app.get('/auth/twitter', passport.authenticate('twitter'));
  94. app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect: '/', failureRedirect: '/login' }));
  95. app.get('/auth/github', passport.authenticate('github', { scope: [ 'user:email profile repo' ] }));
  96. app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }));
  97. app.get('/:urlname', campanhaController.campanhaGet);
  98. app.get('/c/:urlname', campanhaController.campanhaGet);
  99. app.get('/:urlname/exterior', campanhaController.exteriorGet);
  100. // Production error handler
  101. if (app.get('env') === 'production') {
  102. app.use(function(err, req, res, next) {
  103. console.error(err.stack);
  104. res.sendStatus(err.status || 500);
  105. });
  106. }
  107. app.listen(app.get('port'), function() {
  108. console.log('Express server listening on port ' + app.get('port'));
  109. });
  110. module.exports = app;