-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (82 loc) · 2.59 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
require('./config');
var express = require('express');
var session = require('express-session')
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var nunjucks = require('nunjucks');
var passport = require('passport');
var flash = require('connect-flash');
var app = express();
// Database ====================================================================
require('./config/db');
// Mailgun ====================================================================
require('./config/mailgun');
// Authentication ==============================================================
require('./config/passport')(passport);
// configuration ===============================================================
nunjucks.configure('views', {
autoescape: true,
express: app,
});
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use('/public', express.static(process.cwd() + '/public'));
// required for passport
var pg = require('pg');
var pgSession = require('connect-pg-simple')(require('connect'));
app.use(session({
store: new pgSession({
pg: pg,
conString: process.env.SESSION_DATABASE_URL
}),
cookie: {
maxAge: 72 * 60 * 60 * 1000
},
secret: process.env.PASSPORT_SESSION_SECRET
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// routes ======================================================================
require('./routes/index.js')(app, passport);
var users = require('./routes/user');
var groups = require('./routes/group');
app.use('/user', users);
app.use('/group', groups);
app.get('*', function(req, res, next) {
var err = new Error();
err.status = 404;
next(err);
});
// error middleware
app.use(function(err, req, res, next){
if (err.status == 404){
res.status(404);
res.render('error.html', {
auth: req.isAuthenticated()
});
}
if (err.status == 500){
res.status(500);
res.render('error.html', {
auth: req.isAuthenticated()
});
}
});
app.listen(process.env.PORT || 8000);
// TODO: this is incredibly bad practice
process.on('uncaughtException', function(exception) {
console.log(exception);
});
// TODO: will set interval to poll gmail for data
var pollingFrequency = 10 * 60 * 1000;
setInterval(function() {
require('./utils/poll').pollAll();
}, pollingFrequency);
module.exports = app;