server.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 markdown = require('nunjucks-markdown');
  13. var marked = require('marked');
  14. var mongoose = require('mongoose');
  15. var passport = require('passport');
  16. // Load environment variables from .env file
  17. dotenv.load();
  18. // Controllers
  19. var HomeController = require('./controllers/home');
  20. var userController = require('./controllers/user');
  21. var contactController = require('./controllers/contact');
  22. var watchController = require('./controllers/watch');
  23. var uploadController = require('./controllers/upload');
  24. var pagesController = require('./controllers/pages');
  25. var listController = require('./controllers/list');
  26. var profileController = require('./controllers/profile');
  27. // Passport OAuth strategies
  28. require('./config/passport');
  29. var app = express();
  30. mongoose.connect(process.env.DB_PATH);
  31. mongoose.connection.on('error', function() {
  32. console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
  33. process.exit(1);
  34. });
  35. // view engine setup
  36. var njks = nunjucks.configure('views', {
  37. autoescape: true,
  38. express: app,
  39. marked: true
  40. });
  41. markdown.register(njks, marked);
  42. app.set('view engine', 'html');
  43. app.set('port', process.env.PORT || 3999);
  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. next();
  57. });
  58. app.use(express.static(path.join(__dirname, 'public')));
  59. // routers
  60. // Home
  61. app.get('/', HomeController.index);
  62. // Pages
  63. app.get('/sobre', pagesController.sobreController);
  64. app.get('/apps', pagesController.appsController);
  65. app.get('/privacy', pagesController.privacyController);
  66. app.get('/tos', pagesController.tosController);
  67. app.get('/dmca', pagesController.dmcaController);
  68. app.get('/faq', pagesController.faqController);
  69. app.get('/press', pagesController.pressController);
  70. app.get('/uploader', userController.ensureAuthenticated, pagesController.uploaderController);
  71. // Watch
  72. app.get('/assistir/:permalink', watchController.watchGet);
  73. app.get('/t/:tags', watchController.tagsGet);
  74. app.get('/novo', userController.ensureAuthenticated, watchController.newWatchGet);
  75. app.post('/novo', userController.ensureAuthenticated, watchController.newWatchPost);
  76. // Watch edit
  77. app.get('/edit/:_id', userController.ensureAuthenticated, watchController.watchEdit);
  78. app.post('/edit/:_id', userController.ensureAuthenticated, watchController.watchPut);
  79. //upload
  80. app.post('/upload', userController.ensureAuthenticated, uploadController.uploadImage);
  81. uploadDir = path.join(__dirname, '/public/media/');
  82. // Contato
  83. app.get('/contato', contactController.contactGet);
  84. app.post('/contato', contactController.contactPost);
  85. //list
  86. app.get('/list', listController.index);
  87. //ProfileNavigation
  88. app.get('/u/:username', profileController.profileGet);
  89. // Account
  90. app.get('/account', userController.ensureAuthenticated, userController.accountGet);
  91. app.put('/account', userController.ensureAuthenticated, userController.accountPut);
  92. app.delete('/account', userController.ensureAuthenticated, userController.accountDelete);
  93. app.get('/signup', userController.signupGet);
  94. app.post('/signup', userController.signupPost);
  95. app.get('/login', userController.loginGet);
  96. app.post('/login', userController.loginPost);
  97. app.get('/forgot', userController.forgotGet);
  98. app.post('/forgot', userController.forgotPost);
  99. app.get('/reset/:token', userController.resetGet);
  100. app.post('/reset/:token', userController.resetPost);
  101. app.get('/logout', userController.logout);
  102. app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink);
  103. app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
  104. app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
  105. app.get('/auth/twitter', passport.authenticate('twitter'));
  106. app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect: '/', failureRedirect: '/login' }));
  107. app.get('/auth/github', passport.authenticate('github', { scope: [ 'user:email profile repo' ] }));
  108. app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }));
  109. // Production error handler
  110. if (app.get('env') === 'production') {
  111. app.use(function(err, req, res, next) {
  112. console.error(err.stack);
  113. res.sendStatus(err.status || 500);
  114. });
  115. }
  116. app.listen(app.get('port'), function() {
  117. console.log('Express server listening on port ' + app.get('port'));
  118. });
  119. module.exports = app;