server.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // Load environment variables from .env file
  15. dotenv.load();
  16. // Controllers
  17. var HomeController = require('./controllers/home');
  18. var userController = require('./controllers/user');
  19. var contactController = require('./controllers/contact');
  20. var watchController = require('./controllers/watch');
  21. var uploadController = require('./controllers/upload');
  22. var pagesController = require('./controllers/pages');
  23. // Passport OAuth strategies
  24. require('./config/passport');
  25. var app = express();
  26. mongoose.connect(process.env.DB_PATH);
  27. mongoose.connection.on('error', function() {
  28. console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
  29. process.exit(1);
  30. });
  31. // view engine setup
  32. nunjucks.configure('views', {
  33. autoescape: true,
  34. express: app
  35. });
  36. app.set('view engine', 'html');
  37. app.set('port', process.env.PORT || 3999);
  38. app.use(compression());
  39. app.use(logger('dev'));
  40. app.use(bodyParser.json());
  41. app.use(bodyParser.urlencoded({ extended: false }));
  42. app.use(expressValidator());
  43. app.use(methodOverride('_method'));
  44. app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
  45. app.use(flash());
  46. app.use(passport.initialize());
  47. app.use(passport.session());
  48. app.use(function(req, res, next) {
  49. res.locals.user = req.user;
  50. next();
  51. });
  52. app.use(express.static(path.join(__dirname, 'public')));
  53. // routers
  54. // Home
  55. app.get('/', HomeController.index);
  56. // Pages
  57. app.get('/sobre', pagesController.sobreController);
  58. // Watch
  59. app.get('/assistir/:permalink', watchController.watchGet);
  60. app.get('/t/:tags', watchController.tagsGet);
  61. app.get('/novo', userController.ensureAuthenticated, watchController.newWatchGet);
  62. app.post('/novo', userController.ensureAuthenticated, watchController.newWatchPost);
  63. //upload
  64. app.post('/upload', userController.ensureAuthenticated, uploadController.uploadImage);
  65. uploadDir = path.join(__dirname, '/public/media/');
  66. // Contato
  67. app.get('/contato', contactController.contactGet);
  68. app.post('/contato', contactController.contactPost);
  69. // Account
  70. app.get('/account', userController.ensureAuthenticated, userController.accountGet);
  71. app.put('/account', userController.ensureAuthenticated, userController.accountPut);
  72. app.delete('/account', userController.ensureAuthenticated, userController.accountDelete);
  73. app.get('/signup', userController.signupGet);
  74. app.post('/signup', userController.signupPost);
  75. app.get('/login', userController.loginGet);
  76. app.post('/login', userController.loginPost);
  77. app.get('/forgot', userController.forgotGet);
  78. app.post('/forgot', userController.forgotPost);
  79. app.get('/reset/:token', userController.resetGet);
  80. app.post('/reset/:token', userController.resetPost);
  81. app.get('/logout', userController.logout);
  82. app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink);
  83. app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
  84. app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
  85. app.get('/auth/twitter', passport.authenticate('twitter'));
  86. app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect: '/', failureRedirect: '/login' }));
  87. app.get('/auth/github', passport.authenticate('github', { scope: [ 'user:email profile repo' ] }));
  88. app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }));
  89. // Production error handler
  90. if (app.get('env') === 'production') {
  91. app.use(function(err, req, res, next) {
  92. console.error(err.stack);
  93. res.sendStatus(err.status || 500);
  94. });
  95. }
  96. app.listen(app.get('port'), function() {
  97. console.log('Express server listening on port ' + app.get('port'));
  98. });
  99. module.exports = app;