forked from zone-eu/zmta-webadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
181 lines (146 loc) · 4.66 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
'use strict';
let config = require('config');
let log = require('npmlog');
let express = require('express');
let bodyParser = require('body-parser');
let path = require('path');
let favicon = require('serve-favicon');
let logger = require('morgan');
let cookieParser = require('cookie-parser');
let session = require('express-session');
let RedisStore = require('connect-redis')(session);
let flash = require('connect-flash');
let hbs = require('hbs');
let compression = require('compression');
let auth = require('basic-auth');
const humanize = require('humanize');
let routes = require('./routes/index');
let app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// Handle proxies. Needed to resolve client IP
if (config.proxy) {
app.set('trust proxy', config.proxy);
}
// Do not expose software used
app.disable('x-powered-by');
/**
* We need this helper to make sure that we consume flash messages only
* when we are able to actually display these. Otherwise we might end up
* in a situation where we consume a flash messages but then comes a redirect
* and the message is never displayed
*/
hbs.registerHelper('flash_messages', function () { // eslint-disable-line prefer-arrow-callback
if (typeof this.flash !== 'function') { // eslint-disable-line no-invalid-this
return '';
}
let messages = this.flash(); // eslint-disable-line no-invalid-this
let response = [];
// group messages by type
Object.keys(messages).forEach(key => {
let el = '<div class="alert alert-' + key + ' alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>';
if (key === 'danger') {
el += '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> ';
}
let rows = [];
messages[key].forEach(message => {
rows.push(hbs.handlebars.escapeExpression(message));
});
if (rows.length > 1) {
el += '<p>' + rows.join('</p>\n<p>') + '</p>';
} else {
el += rows.join('');
}
el += '</div>';
response.push(el);
});
return new hbs.handlebars.SafeString(
response.join('\n')
);
});
hbs.registerHelper('num', function (options) { // eslint-disable-line prefer-arrow-callback
return new hbs.handlebars.SafeString(
humanize.numberFormat(options.fn(this), 0, ',', ' ') // eslint-disable-line no-invalid-this
);
});
hbs.registerHelper('dec', function (options) { // eslint-disable-line prefer-arrow-callback
return new hbs.handlebars.SafeString(
humanize.numberFormat(options.fn(this), 3, ',', ' ') // eslint-disable-line no-invalid-this
);
});
app.use(compression());
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger(config.httplog, {
stream: {
write: message => {
message = (message || '').toString();
if (message && process.NODE_ENV !== 'production') {
log.info('HTTP', message.replace('\n', '').trim());
}
}
}
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
store: new RedisStore(config.redis),
secret: config.secret,
saveUninitialized: false,
resave: false
}));
app.use(flash());
app.use(bodyParser.urlencoded({
extended: true,
limit: config.maxPostSize
}));
app.use(bodyParser.text({
limit: config.maxPostSize
}));
app.use(bodyParser.json({
limit: config.maxPostSize
}));
// make sure flash messages are available
app.use((req, res, next) => {
res.locals.flash = req.flash.bind(req);
let menu = [
/*{
title: 'Home',
url: '/',
selected: true
}*/
];
res.setSelectedMenu = key => {
menu.forEach(item => {
item.selected = (item.key === key);
});
};
res.locals.menu = menu;
next();
});
// setup HTTP auth
app.use((req, res, next) => {
if (!config.auth) {
return next();
}
let credentials = auth(req);
if (!credentials || credentials.name !== config.user || credentials.pass !== config.pass) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="example"');
res.end('Access denied');
} else {
next();
}
});
app.use('/', routes);
app.use((err, req, res, next) => {
if (!err) {
return next();
}
res.status(err.statusCode || 500);
res.render('error', {
message: err.message,
error: err
});
});
module.exports = app;