forked from asbjornu/drywall
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
108 lines (92 loc) · 3.16 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
'use strict';
//dependencies
var _ = require('underscore'),
config = require('./config'),
env = process.env.NODE_ENV || 'development',
dbConfig = config.db[env],
logging = require('./util/logging')(config),
express = require('express'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
session = require('express-session'),
http = require('http'),
path = require('path'),
passport = require('passport'),
models = require('./models/index'),
helmet = require('helmet'),
csrf = require('csurf'),
SequelizeStore = require('connect-session-sequelize')(session.Store),
sequelizeStore = new SequelizeStore({ db: models.sequelize });
exports.setup = function(app) {
//keep reference to config
app.config = config;
//setup the web server
app.server = http.createServer(app);
app.db = _.extend(models, {
store: sequelizeStore
});
//settings
app.disable('x-powered-by');
app.set('port', config.port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// middleware
app.use(require('morgan')('dev'));
app.use(require('compression')());
app.use(require('serve-static')(path.join(__dirname, 'public')));
app.use(require('method-override')());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser(config.cryptoKey));
app.use(session({
resave: true,
saveUninitialized: true,
secret: config.cryptoKey,
store: sequelizeStore,
name: 'piratpartiet.sid',
cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 }, // 30 days
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(csrf({ cookie: { signed: true } }));
helmet(app);
//response locals
app.use(function(req, res, next) {
res.cookie('_csrfToken', req.csrfToken());
res.locals = res.locals ? res.locals : {};
res.locals.user = {};
res.locals.user.defaultReturnUrl = req.user && req.user.defaultReturnUrl();
res.locals.user.username = req.user && req.user.username;
next();
});
// https://github.com/ethanl/connect-browser-logger
// app.use(require('browser-logger')());
//global locals
app.locals.projectName = app.config.projectName;
app.locals.copyrightYear = new Date().getFullYear();
app.locals.copyrightName = app.config.companyName;
app.locals.cacheBreaker = 'br34k-01';
//setup passport
require('./passport')(app, passport);
//setup routes
require('./routes')(app, passport);
//custom (friendly) error handler
// app.use(require('./views/http/index').http500);
//setup utilities
app.utility = _.extend(logging, {
sendmail: require('./util/sendmail'),
slugify: require('./util/slugify'),
workflow: require('./util/workflow')
});
// TODO: Figure out a more elegant way to do this. @asbjornu
dbConfig.logging = dbConfig.logging ? app.utility.debug : false;
sequelizeStore.sync(dbConfig);
return app;
};
// create express app
exports.app = this.setup(express());
// listen up
exports.app.server.listen(exports.app.config.port, function() {
//and... we're live
exports.app.utility.debug('Server is running on port ' + config.port);
});