-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
223 lines (200 loc) · 7.04 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Module dependencies.
*/
// This comment added to test automating dev server deployment upon github commits
var config = require('./config')
, express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, jadeBrowser = require('jade-browser')
, mongoose = require('mongoose')
, connectAssets = require('connect-assets')
, lessMiddleware = require('less-middleware')
, Tweet = require('./models/tweet')
, passport = require('passport')
, LocalStrategy = require('passport-local').Strategy
, TwitterStrategy = require('passport-twitter').Strategy
, FacebookStrategy = require('passport-facebook').Strategy
, MongoStore = require('connect-mongo')(express)
, stylus = require('stylus')
, sessionStore = new MongoStore({ url: config.mongodb })
, twitterStream = require('./twitter/server')
;
/*
// set up passport authentication
if(config.enableGuestLogin) {
passport.use('guest', new LocalStrategy(
{
usernameField: 'name',
},
// doesn't actually use password, just records name
function(name, password, done) {
process.nextTick(function() {
User.authGuest(name, done);
});
}
));
}
if(config.enableEmailLogin) {
passport.use('email', new LocalStrategy(
{
usernameField: 'email'
},
function(email, password, done) {
process.nextTick(function() {
User.authEmail(email, password, done);
});
}
));
}
if(config.twitter) {
passport.use(new TwitterStrategy(
config.twitter,
function(token, tokenSecret, profile, done) {
process.nextTick(function() {
User.authTwitter(token, tokenSecret, profile, done);
});
}
));
}
if(config.facebook) {
passport.use(new FacebookStrategy(
config.facebook,
function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
User.authFacebook(accessToken, refreshToken, profile, done);
});
}
));
}
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
*/
// connect the database
mongoose.connect(config.mongodb);
// create app, server, and socket.io
var app = express(),
server = http.createServer(app);
twitterStream.listen(server);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.locals.pretty = true;
// export jade templates to reuse on client side
// This includes a kind of terrible cache-buster hack
// It generates a new cache-busting query string for the script tag every time the server starts
// This should probably only happen every time there's a change to the templates.js file
var jadeTemplatesPath = '/js/templates.js';
app.use(jadeBrowser(jadeTemplatesPath, ['*.jade', '*/*.jade'], { root: __dirname + '/views', minify: true }));
var jadeTemplatesCacheBuster = (new Date()).getTime();
var jadeTemplatesSrc = jadeTemplatesPath + '?' + jadeTemplatesCacheBuster;
global.jadeTemplates = function() { return '<script src="' + jadeTemplatesSrc + '" type="text/javascript"></script>'; }
// use the connect assets middleware for Snockets sugar
app.use(connectAssets());
app.use(express.favicon());
app.use(express.logger(config.loggerFormat));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(config.sessionSecret));
app.use(express.session({ store: sessionStore }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
if(config.useErrorHandler) app.use(express.errorHandler());
});
// API routes
// Do not call res.send(), res.json(), etc. in API route functions
// Instead, within each API route, set res.jsonData to the JSON data, then call next()
// This will allow us to write UI route functions that piggyback on the API functions
//
// Example API function for /api/me:
/*
exports.show = function(req, res, next) {
res.jsonData = req.user;
next();
};
*/
var sendJson = function(req, res) { res.json(res.jsonData); }
/*
app.get('/api/me', routes.api.me.show);
app.get('/api/users/:id', routes.api.users.show);
*/
// this catch-all route will send JSON for every API route that falls through to this point in the chain
// WARNING: Sometimes they don't fall through to this point in the chain! Example:
//
// app.get('/api/users/someNonStandardService', routes.api.users.someNonStandardService);
// app.get('/api/users/:id', routes.api.users.show)
//
// In this case the next() of `someNonStandardService` is the `show` route, but we want to send json
// So explicitly tell the `someNonStandardService` that its `next` is the `sendJson` function:
//
// app.get('/api/users/someNonStandardService', routes.api.users.someNonStandardService, sendJson);
//
app.all('/api/*', sendJson);
// UI routes
// Within each UI route function, call the corresponding API function.
// Grab the API response data from res.jsonData and render as needed.
//
// Example UI function for /me:
/*
var me = require('../api/me');
exports.show = function(req, res) {
me.show(req, res, function() {
res.render('me/index', { title: 'Profile', user: res.jsonData });
});
};
*/
app.get('/superdash/events', routes.api.superdash.events);
//app.get('/superdash/official', routes.api.superdash.official);
app.get('/superdash/instagram', routes.api.superdash.instagram);
app.get('/superdash/heatmap', routes.api.superdash.heatmap);
app.get('/superdash/psa', routes.api.superdash.psa);
app.get('/superdash/changes', routes.api.superdash.changes);
app.get('/superdash/accountfeed', routes.api.superdash.accountfeed);
app.get('/superdash/instagram/callback', routes.api.superdash.instagram.subscribe_callback);
app.post('/superdash/instagram/callback', routes.api.superdash.instagram.callback);
// home
app.get('/', routes.ui.home);
// left-hand dashboard
app.get('/left', routes.ui.left);
// right-hand dashboard
app.get('/right', routes.ui.right);
/*
// currently logged-in user
app.get('/me', routes.ui.me.show);
app.put('/me', routes.ui.me.update);
// user profiles
app.get('/users/:id', routes.ui.users.show);
// authentication
if(config.enableGuestLogin) {
app.post('/auth/guest', routes.ui.auth.guest);
}
if(config.enableEmailLogin) {
app.post('/auth/registerEmail', routes.ui.auth.registerEmail);
app.post('/auth/email', routes.ui.auth.email);
}
if(config.twitter) {
app.get('/auth/twitter', routes.ui.auth.twitter);
app.get('/auth/twitter/callback', routes.ui.auth.twitterCallback);
}
if(config.facebook) {
app.get('/auth/facebook', routes.ui.auth.facebook);
app.get('/auth/facebook/callback', routes.ui.auth.facebookCallback);
}
app.get('/auth/success', routes.ui.auth.success);
app.get('/auth/finish', routes.ui.auth.finish);
app.get('/auth/failure', routes.ui.auth.failure)
app.get('/auth/logout', routes.ui.auth.logout);
*/
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});