2013-11-13 12:32:22 -05:00
|
|
|
var express = require('express'),
|
2013-11-13 18:58:03 -05:00
|
|
|
http = require('http'),
|
|
|
|
path = require('path'),
|
2013-11-13 12:32:22 -05:00
|
|
|
fs = require('fs'),
|
2013-11-18 14:37:01 -05:00
|
|
|
flash = require('connect-flash'),
|
2013-11-13 18:58:03 -05:00
|
|
|
mongoose = require('mongoose'),
|
2013-11-15 11:13:21 -05:00
|
|
|
passport = require('passport');
|
2013-11-13 15:49:58 -05:00
|
|
|
|
2013-11-19 17:09:05 -05:00
|
|
|
// TODO: Add node-opencv!!
|
2013-11-20 02:38:03 -05:00
|
|
|
// TODO: "Lego-like" modules, e.g. swap one login view for another
|
|
|
|
// TODO: Let users plug any components of the website
|
2013-11-19 16:20:18 -05:00
|
|
|
// App Configuration (API Keys, Database URI)
|
2013-11-18 19:43:45 -05:00
|
|
|
var config = require('./config/config.json');
|
2013-11-18 17:37:50 -05:00
|
|
|
var passportConf = require('./config/passport');
|
2013-11-14 02:29:55 -05:00
|
|
|
|
2013-11-19 16:20:18 -05:00
|
|
|
|
2013-11-15 11:13:21 -05:00
|
|
|
// Load controllers
|
|
|
|
var home = require('./controllers/home'),
|
2013-11-19 14:33:11 -05:00
|
|
|
user = require('./controllers/user'),
|
|
|
|
api = require('./controllers/api'),
|
|
|
|
contact = require('./controllers/contact');
|
2013-11-14 02:29:55 -05:00
|
|
|
|
|
|
|
// Connect to database
|
2013-11-15 11:13:21 -05:00
|
|
|
var db = mongoose.connect(config.db);
|
2013-11-13 12:32:22 -05:00
|
|
|
|
2013-11-19 16:20:18 -05:00
|
|
|
|
|
|
|
|
2013-11-15 11:13:21 -05:00
|
|
|
// Initialize express application
|
|
|
|
var app = express();
|
2013-11-13 12:32:22 -05:00
|
|
|
|
2013-11-13 15:49:58 -05:00
|
|
|
app.set('port', process.env.PORT || 3000);
|
2013-11-13 22:19:37 -05:00
|
|
|
app.set('views', __dirname + '/views');
|
2013-11-16 12:55:40 -05:00
|
|
|
app.set('view engine', 'jade');
|
2013-11-13 16:05:26 -05:00
|
|
|
app.use(express.logger('dev'));
|
2013-11-14 02:29:55 -05:00
|
|
|
app.use(express.errorHandler());
|
|
|
|
app.use(express.favicon());
|
2013-11-13 18:58:03 -05:00
|
|
|
app.use(express.cookieParser());
|
|
|
|
app.use(express.bodyParser());
|
2013-11-13 12:49:37 -05:00
|
|
|
app.use(express.methodOverride());
|
2013-11-19 14:33:11 -05:00
|
|
|
app.use(express.session({ secret: 'Bob-vs-Alice' }));
|
2013-11-13 18:58:03 -05:00
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
2013-11-18 14:37:01 -05:00
|
|
|
app.use(flash());
|
2013-11-13 22:19:37 -05:00
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
2013-11-13 12:49:37 -05:00
|
|
|
app.use(app.router);
|
|
|
|
|
2013-11-14 02:37:37 -05:00
|
|
|
|
2013-11-20 02:38:03 -05:00
|
|
|
app.get('/', home.index);
|
2013-11-16 13:24:06 -05:00
|
|
|
app.get('/login', user.getLogin);
|
2013-11-18 17:37:50 -05:00
|
|
|
app.post('/login', user.postLogin);
|
2013-11-16 13:24:06 -05:00
|
|
|
app.get('/logout', user.logout);
|
2013-11-17 15:15:05 -05:00
|
|
|
app.get('/signup', user.getSignup);
|
2013-11-16 13:24:06 -05:00
|
|
|
app.post('/signup', user.postSignup);
|
2013-11-18 17:37:50 -05:00
|
|
|
app.get('/account', passportConf.ensureAuthenticated, user.account);
|
2013-11-20 02:38:03 -05:00
|
|
|
app.get('/admin', passportConf.ensureAuthenticated, passportConf.ensureAdmin(), user.getAdmin);
|
2013-11-14 02:37:37 -05:00
|
|
|
app.get('/partials/:name', home.partials);
|
2013-11-19 14:33:11 -05:00
|
|
|
app.get('/api', api.apiBrowser);
|
2013-11-19 17:12:36 -05:00
|
|
|
app.get('/api/foursquare', passportConf.ensureAuthenticated, api.foursquare);
|
2013-11-19 14:33:11 -05:00
|
|
|
app.get('/contact', contact.getContact);
|
|
|
|
app.post('/contact', contact.postContact);
|
|
|
|
|
2013-11-23 14:43:51 -05:00
|
|
|
app.get('/auth/facebook', passport.authenticate('facebook', {
|
|
|
|
scope: [
|
|
|
|
'email'
|
|
|
|
]
|
|
|
|
}), function(req, res) {
|
|
|
|
// The request will be redirected to Facebook for authentication, so this
|
|
|
|
// function will not be called.
|
|
|
|
});
|
|
|
|
|
2013-11-19 17:12:36 -05:00
|
|
|
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
|
2013-11-21 14:20:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
app.get('/auth/google', passport.authenticate('google', {
|
|
|
|
scope: [
|
|
|
|
'https://www.googleapis.com/auth/userinfo.profile',
|
|
|
|
'https://www.googleapis.com/auth/userinfo.email'
|
|
|
|
]
|
|
|
|
}), function (req, res) {
|
|
|
|
// The request will be redirected to Google for authentication, so this
|
|
|
|
// function will not be called.
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/auth/google/callback', passport.authenticate('google', {
|
2013-11-22 10:23:54 -05:00
|
|
|
successRedirect: '/',
|
2013-11-21 14:20:38 -05:00
|
|
|
failureRedirect: '/login'
|
|
|
|
}), function (req, res) {
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-11-19 17:09:05 -05:00
|
|
|
app.get('/auth/foursquare', api.foursquareAuth);
|
|
|
|
app.get('/auth/foursquare/callback', api.foursquareCallback);
|
2013-11-14 02:29:55 -05:00
|
|
|
app.get('*', home.index);
|
2013-11-13 16:05:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
app.listen(app.get('port'), function() {
|
|
|
|
console.log('Express server listening on port ' + app.get('port'));
|
|
|
|
});
|