-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.js
197 lines (173 loc) · 5.67 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
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const cookieParser = require('cookie-parser')();
const bodyParser = require('body-parser');
const http = require('http');
const flash = require('connect-flash');
const compress = require('compression');
const cors = require('cors');
const filesize = require('filesize');
const debug = require('debug')('RandomAPI:server');
const app = express();
const server = http.createServer(app);
const _ = require('lodash');
const logger = require('./utils').logger;
const syslog = require('./utils').syslog;
const settings = require('./utils').settings;
const dbStatus = require('./utils').dbStatus;
const User = require('./models/User');
const Tier = require('./models/Tier');
const Subscription = require('./models/Subscription');
// Redis Session Store
const session = require('express-session');
const redisStore = require('connect-redis')(session);
// Initialize generators
const GeneratorForker = require('./api/0.1/GeneratorForker');
let Generators = {};
Object.keys(settings.generators).forEach(generator => {
Generators[generator] = new Array(settings.generators[generator].count).fill().map((k, v) => {
return new GeneratorForker({
name: generator,
execTime: settings.generators[generator].execTime,
results: settings.generators[generator].results
});
});
});
// Store Generators in app
app.set("Generators", Generators);
// Cache removers
app.set("removeList", list => {
Object.keys(settings.generators).forEach(generator => {
Generators[generator].forEach(generator => {
generator.removeList(list);
});
});
});
app.set("removeAPI", api => {
Object.keys(settings.generators).forEach(generator => {
Generators[generator].forEach(generator => {
generator.removeAPI(api);
});
});
});
app.set("removeSnippet", snippet => {
Object.keys(settings.generators).forEach(generator => {
Generators[generator].forEach(generator => {
generator.removeSnippet(snippet);
});
});
});
/////////////////
// view engine setup
app.set('views', path.join(__dirname, '.viewsMin/pages'));
app.set('view engine', 'ejs');
app.set('port', settings.general.port);
app.set('baseURL', settings.general.baseURL);
app.set('basehref', settings.general.basehref);
// CORS and GZIP
app.use(cors());
app.use(compress());
// Session store
app.use(cookieParser);
let sessionSettings = {
key: settings.session.key,
store: new redisStore(),
secret: settings.session.secret,
cookie: {
path: '/'
},
resave: false,
saveUninitialized: false
};
app.use(session(sessionSettings));
// Flash messages
app.use(flash());
// Favicon
app.use(favicon(__dirname + '/public/img/favicon.png'));
//app.use(logger('dev'));
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ limit: '5mb', extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('*', (req, res, next) => {
if (!dbStatus()) {
return res.render('epicfail', {basehref: settings.general.basehref, messages: null, title: 'Uh oh, something bad has happened'});
}
// Skip if user is accessing api
if (req.params[0].slice(0, 5) === '/api/') next();
else {
let info = req.flash('info');
let warning = req.flash('warning');
let token = req.flash('token');
if (info.length) {
messages = ["info", info];
} else if (warning.length) {
messages = ["warning", warning];
} else if (token.length) {
messages = ["token", token];
} else {
messages = "";
}
// Default vars to send into views
app.set('defaultVars', {
session: req.session,
recaptcha: settings.recaptcha.siteKey,
publishableKey: settings.stripe.publishableKey,
basehref: settings.general.basehref,
title: null,
originalUrl: req.originalUrl,
filesize,
messages
});
// Attach user, tier, and subscription details
// to request object for logged in users
if (req.session.loggedin) {
User.getCond({username: req.session.user.username}).then(user => {
if (user === null) {
syslog(req.session);
delete req.session.loggedin;
res.redirect(settings.general.baseURL + '/logout');
return;
}
Tier.getCond({id: user.tierID}).then(tier => {
Subscription.getCond({uid: user.id}).then(subscription => {
req.session.user = user;
req.session.tier = tier;
req.session.subscription = subscription;
next();
});
});
});
} else {
next();
}
}
});
// Routes
app.use('/', require('./routes/index'));
app.use('/ajax', require('./routes/ajax'));
app.use('/code', require('./routes/code'));
app.use('/publish', require('./routes/publish'));
app.use('/charge', require('./routes/charge'));
app.use('/new', require('./routes/new'));
app.use('/view', require('./routes/view'));
app.use('/edit', require('./routes/edit'));
app.use('/delete', require('./routes/delete'));
app.use('/api', require('./routes/api'));
app.use('/settings', require('./routes/settings'));
app.use('/offline', require('./routes/offline'));
app.use('/statistics', require('./routes/statistics'));
if (settings.general.stats) {
app.use(`/${settings.general.statsPath}`, require('./routes/stats'));
}
// production error handler
// no stacktraces leaked to user
app.use((req, res, next) => res.sendStatus(404));
app.use((err, req, res, next) => {
syslog(err.stack);
res.sendStatus(404)
});
module.exports = {
server,
app
};