-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
220 lines (198 loc) · 6.43 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
/* eslint strict:0, no-console:0, no-var:0, vars-on-top:0, one-var:0, prefer-template:0 */
/* eslint block-scoped-var:0, no-param-reassign:0, arrow-body-style:0 */
// no-var: We aren't ES6 yet
/*
* You don't need to edit anything in this file. Go look at settings.js.example.
*/
'use strict';
// Load configuration
var options;
console.log('Attempting to load configuration...');
if (process.env.TWI_SETTINGS) {
console.log('Loading settings from Environment...');
try {
options = JSON.parse(process.env.TWI_SETTINGS);
} catch (e) {
console.log('Loading from Environment failed, attempting to load from file...');
}
}
if (!options) {
try {
options = require('./settings.js');
} catch (e) {
console.error('Twi has failed to boot.' +
'No configuration found in TWI_SETTINGS or ./settings.js.');
process.exit(e.code);
}
}
options.version = 'v0.0.5';
/**
* Module dependencies.
*/
var http = require('http'),
express = require('express'),
session = require('express-session'),
morgan = require('morgan'),
ImageProvider = require('./lib/mongodb/imageProvider').ImageProvider,
AliasProvider = require('./lib/mongodb/aliasProvider').AliasProvider,
TagProvider = require('./lib/mongodb/tagProvider').TagProvider,
UserProvider = require('./lib/mongodb/userProvider').UserProvider,
WikiProvider = require('./lib/mongodb/wikiProvider').WikiProvider,
CommentProvider = require('./lib/mongodb/commentProvider').CommentProvider,
ImplicationProvider = require('./lib/mongodb/implicationProvider').ImplicationProvider,
FileProvider = require('./lib/upload/' + options.upload.method + '.js').Storage,
mongojs = require('mongojs'),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
flash = require('flash'),
RedisStore = require('connect-redis')(session);
// App setup
var app = express();
app.twi = {};
app.twi.options = options;
// Providers
app.providers = {};
app.db = mongojs('mongodb://' + app.twi.options.database.user + ':' + app.twi.options.database.pass + '@' + app.twi.options.database.host + ':' + app.twi.options.database.port + '/' + app.twi.options.database.name);
app.providers.commentProvider = new CommentProvider(app.db.collection('comments'));
app.providers.fileProvider = new FileProvider(app.twi.options);
app.providers.imageProvider = new ImageProvider(app.db.collection('images'),
app.twi.options.resultsPerPage);
app.providers.aliasProvider = new AliasProvider(app.db.collection('aliases'));
app.providers.tagProvider = new TagProvider(app.db.collection('tags'));
app.providers.userProvider = new UserProvider(app.db.collection('users'));
app.providers.wikiProvider = new WikiProvider(app.db.collection('wiki'));
app.providers.implicationProvider = new ImplicationProvider(app.db.collection('implications'));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// app.use(express.bodyParser({
// uploadDir:
// ((app.twi.options.upload.method == 'direct') ? app.twi.options.upload.paths.temp : null)
// }));
var sessionInfo = {
resave: false,
saveUninitialized: false,
secret: app.twi.options.sessionKey
};
var conf = {
h: false, // Host
t: false, // Port
d: false, // Db
s: false // Pass
};
if (process.env.REDISTOGO_URL) {
var url = require('url'),
redisUrl = url.parse(process.env.REDISTOGO_URL),
redisAuth = redisUrl.auth.split(':');
conf.h = redisUrl.hostname;
conf.t = redisUrl.port;
conf.d = 0;
conf.s = redisAuth[1];
} else if (app.twi.options.redis.host) {
conf.h = app.twi.options.redis.host;
conf.t = app.twi.options.redis.port;
conf.d = app.twi.options.redis.db;
conf.s = app.twi.options.redis.pass;
}
if (conf.h) {
sessionInfo.store = new RedisStore({
host: conf.h,
port: conf.t,
db: conf.d,
pass: conf.s
});
} else {
sessionInfo.store = new RedisStore();
}
app.use(session(sessionInfo));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next) => {
// Template globals
res.locals.session = req.session;
res.locals.board = {
name: app.twi.options.name,
domain: app.twi.options.domain,
version: app.twi.options.version
};
res.locals.board.authenticated = false;
if (req.isAuthenticated()) {
res.locals.board.authenticated = true;
res.locals.board.user = req.user;
}
res.locals.board.flash = {
// error: req.flash('error'),
// info: req.flash('info')
};
for (var i = 0; i < res.locals.board.flash.length; i++) {
console.log('FlashMsg Error: ' + res.locals.board.flash[i]);
}
res.locals.board.uploadMethod = app.twi.options.upload.method;
next();
});
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev', {
skip: (req, res) => {
return res.statusCode < 400;
}
}));
passport.use(new LocalStrategy(
(username, password, done) => {
app.providers.userProvider.findOne({
username
}, (err, user) => {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, {
message: 'Unknown user'
});
}
if (user.t) {
return done(null, false, {
message: 'You must verify your account before you can log in.'
});
}
if (!app.providers.userProvider.validate(user, password)) {
return done(null, false, {
message: 'Invalid password'
});
}
return done(null, user);
});
}
));
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
app.providers.userProvider.findById(id, (err, user) => {
done(err, user);
});
});
// Include the routes
require('./lib/routes/index')(app);
// Except this one because passport is here and it would be silly and wasteful to ship it elsewhere
app.post('/login',
passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: 'Login failed.',
successRedirect: '/post',
successFlash: 'You have been logged in.'
})
);
// Cron
setInterval(() => {
app.providers.userProvider.reaper((error) => {
if (error) console.log('User Reaper Error (app.js:205): ' + error);
});
app.providers.tagProvider.reaper((error) => {
if (error) console.log('Tag Reaper Error (app.js:208): ' + error);
});
}, 3600000); // One hour
app.set('port', process.env.PORT || 3000);
var server = http.createServer(app).listen(app.get('port'), () => {
console.log('Twi booting on port %s in %s mode', app.get('port'), process.env.NODE_ENV);
});
module.exports = server;