/
site.js
260 lines (224 loc) · 7.54 KB
/
site.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
(function() {
var env = require('node-env-file');
var express = require('express');
var jade = require('jade');
var moment = require('moment');
var mongo = require('mongodb');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cookieSession = require('cookie-session');
var methodOverride = require('method-override');
var morgan = require('morgan');
var logger = morgan('combined');
var MongoStore = require('connect-mongo')(session);
var extend = require('node.extend');
var path = require('path');
var passport = require('passport');
var TwitterStrategy = require('passport-twitter').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var GoogleStrategy = require('passport-google').Strategy;
var funflow = require('funflow');
function loadConf(name) {
return require('./conf/' + name);
}
function check(map, keys) {
keys.forEach(function(key) {
if (map[key] == undefined) {
throw new Error('.' + key + ' is not defined in ' + map.NODE_ENV);
}
});
}
function createApp(combinedConf, deps, options, done) {
var model = deps.model;
var port = combinedConf.PORT;
var hostAddress = combinedConf.GOOGLE_HOSTNAME;
var controller = deps.controller.withModel(model, hostAddress, options || {});
model.users = model.users || {};
passport.serializeUser(function(user, done) {
model.users[user.id] = user;
done(null, user.id);
});
passport.deserializeUser(function(obj, done) {
var res = model.users[obj];
done(null, res || null);
});
passport.use(new TwitterStrategy({
consumerKey: "FCvT4ed7oo1N8YvB1o5pQ",
consumerSecret: combinedConf.TWITTER_CONSUMER_SECRET,
callbackURL: "/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
done(null, profile);
}
));
passport.use(new FacebookStrategy({
clientID: "1404627676456080",
clientSecret: combinedConf.FACEBOOK_APP_SECRET,
callbackURL: "//collidingobjects.herokuapp.com/auth/facebook/callback",
},
function(accessToken, refreshToken, profile, done) {
return done(null, profile);
}
));
passport.use(new GoogleStrategy({
returnURL: hostAddress + '/auth/google/callback',
realm: hostAddress
},
function(identifier, profile, done) {
profile.id = identifier;
return done(null, profile);
}
));
var app = express();
app.set('port', port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
mongo.MongoClient.connect(combinedConf.MONGOLAB_URI, function(err, db) {
if (err) return done(err);
deps.db = db;
app.use(logger);
app.use(cookieParser(combinedConf.COOKIE_SECRET));
app.use(bodyParser.json());
app.use(cookieSession({ secret: combinedConf.COOKIE_SESSION_SECRET}));
app.use(methodOverride());
app.use(session({
secret: combinedConf.SESSION_SECRET,
store: new MongoStore({ db : db }),
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.get('/rss.xml', function(req, res) {
controller.rss(res);
});
app.get('/login', function(req, res) {
res.render('login', { headline: model.headline });
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
function authRoutes(provider) {
app.get('/auth/' + provider, passport.authenticate(provider),
function(req, res) {} // will never be called.
);
app.get('/auth/' + provider + '/callback',
passport.authenticate(provider,
{ failureRedirect: '/login', successRedirect: '/' })
);
}
authRoutes('twitter');
authRoutes('facebook');
authRoutes('google');
app.get('/', controller.posts);
app.get('/posts', controller.posts);
app.get('/edit', function(req, res) {
res.redirect('/edit.html');
});
app.get('/env.json', function(req, res) {
res.json({
});
});
function lookup(id, callback) {
var post = null;
model.posts.forEach(function(current) {
if (current.id == id) {
post = current;
}
});
if (!post) {
callback(null);
return;
}
if (post.body) {
callback(post);
return;
}
model.fetchBody(id, function(err, body) {
callback(extend({body: body}, post));
});
}
app.get('/posts/:id.json', function(req, res) {
lookup(req.params.id, function(post) {
res.json(post);
});
});
app.get('/posts/:id', function(req, res) {
lookup(req.params.id, function(post) {
if (post) {
controller.singlePost(post, req, res);
} else {
res.status(404).end();
}
});
});
app.get('/posts/:id/edit', function(req, res) {
res.sendFile(__dirname + '/public/edit.html');
});
app.use(express.static(__dirname + '/public'));
app.use(controller.error);
app.use(controller.pageNotFound);
done(null, app);
});
}
exports.createDriver = function(overridingConf, deps, options) {
try {
env(__dirname + '/conf/.env');
} catch(e) {
// Intentionally ignore.
}
var confName = process.env.NODE_ENV || 'development';
var combinedConf = extend(
loadConf('default'),
process.env,
loadConf(confName),
overridingConf,
{NODE_ENV: confName}
);
// Sanity checks on the configuration (hard to unit-test).
check(combinedConf, ['PORT', 'NODE_ENV', 'TWITTER_CONSUMER_SECRET', 'COOKIE_SESSION_SECRET']);
if (combinedConf.NODE_ENV === 'production' && combinedConf.TWITTER_CONSUMER_SECRET === combinedConf.FACEBOOK_APP_SECRET) {
throw new Error('Same consumer secret for two different providers in ' + combinedConf.NODE_ENV);
};
if (!combinedConf.PORT) {
throw new Error('No .PORT value is specified');
}
var flow = funflow.newFlow(
function create(initDone, combinedConf, deps, options, next) {
this.initDone = initDone;
createApp(combinedConf, deps, options, next);
},
function listen(app, next) { this.app = app; this.server = app.listen(app.get('port'), next) },
function serverIsUp(next) {
console.log(combinedConf.VERTICAL_SPACE + '> Express server [' + combinedConf.NODE_ENV
+ '] started at http://localhost:' + this.app.get('port') + combinedConf.VERTICAL_SPACE);
next();
},
funflow.comp(function yield(e, next) {
this.initDone(e);
this.resume = next;
}),
function closeServer(shutdownDone, next) {
this.shutdownDone = shutdownDone;
this.server.close(next);
},
function dbClose(next) { deps.db.close(next); },
funflow.comp(function(e, next) { this.shutdownDone(e); next(); })
);
var driver = {
start: function(initDone) {
var exec = flow(null, initDone, combinedConf, deps, options, function(err) {
if(err)
console.error(err.flowTrace);
});
driver.stop = function(shutdownDone) {
exec.context.shutdownDone = shutdownDone;
exec.context.resume(null, shutdownDone);
}
}
};
return driver;
};
})()