forked from suetco/suet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
128 lines (114 loc) · 3.1 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
require('dotenv').config();
const express = require('express')
, Liquid = require('liquidjs')
, engine = Liquid()
, moment = require('moment')
, session = require('express-session')
, sessionStore = require('connect-mongo')(session)
, bodyParser = require('body-parser')
, helmet = require('helmet')
, path = require('path')
, flash = require('flash')
, compression = require('compression')
, favicon = require('serve-favicon')
, bugsnag = require('bugsnag')
// Libs
, acl = require('./lib/acl.js')
, dbo = require('./lib/db.js')
;
bugsnag.register(process.env.BS_KEY);
dbo.connect(err => {
// Config
const app = express();
app.listen(process.env.PORT || 3000);
//app.use(helmet());
app.use(bugsnag.requestHandler);
if (err)
bugsnag.notify(err);
// Index DB
require('./lib/indexes.js')(dbo);
// Filters
// Override 'remove'
engine.registerFilter('remove', (v, arg) => {
let arr = [],
arg_arr = arg.split(',')
;
// Remove
for (let _arg of arg_arr) {
for (let k in v) {
if (k == _arg)
delete v[k];
}
}
// Build http query
for (k in v)
arr.push([k, '=', v[k]].join(''));
return arr.join('&');
});
engine.registerFilter('sum', v => {
if (!v)
return 0;
return v.reduce((a, b) => {
return a + b;
}, 0);
});
engine.registerFilter('format', v => {
if (!v)
return 0;
return v.toLocaleString();
});
engine.registerFilter('literal_escape', v => {
if (!v)
return v;
v = v.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '').replace(/<script[^>]*>[\s\S]*/gi, '').replace(/`/g, '\\`')
return v;
});
engine.registerFilter('colour', v => {
var hash = 0;
if (v.length == 0) return hash;
for (let i = 0; i < v.length; i++) {
hash = v.charCodeAt(i) + ((hash << 5) - hash);
hash = hash & hash;
}
shortened = hash % 360;
return "hsl(" + shortened + ",90%,70%)";
})
// Middlewares
const sess = {
secret: process.env.SESSION_KEY,
maxAge: 3600000 * 24 * 365,
store: new sessionStore({
db: dbo.db(),
ttl: 3600000 * 24 * 365
}),
resave: false,
saveUninitialized: false,
unset: 'destroy'
}
app.use(session(sess));
app.engine('liquid', engine.express());
app.set('view engine', 'liquid');
app.set('views', __dirname + '/public_html');
app.use(favicon(path.join(__dirname, 'public_html', 'favicon.ico')));
app.use(flash());
app.use(compression());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(express.static(__dirname + '/public_html/static'));
app.use(acl());
// Routes
require('./routes/auth.js')(app);
require('./routes/domains.js')(app);
require('./routes/app.js')(app);
require('./routes/connections.js')(app);
require('./routes/static.js')(app);
// No matching route
app.use((err, req, res, next) => {
res.status(err.status || 500);
// todo: log err.message
console.log(err.message);
res.end();
//res.render('error');
});
});