forked from Bastian/bStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
163 lines (149 loc) · 5.47 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
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const passport = require('passport');
const async = require('async');
const dataManager = require('./util/dataManager');
const auth = require('./util/auth');
const config = require('./util/config');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
auth(passport);
app.use(session({
store: new RedisStore({
client: require('./util/databaseManager').getRedisCluster()
}),
resave: false,
saveUninitialized: false,
secret: config.sessionSecret
}));
app.use(passport.initialize());
app.use(passport.session());
// Middleware to include locals
app.use(function (req, res, next) {
res.locals.user = req.user === undefined ? null : req.user;
res.locals.loggedIn = req.user !== undefined;
if (req.method === 'POST') {
return next();
}
if (req.path.startsWith('/api')) {
// Check if we can cache the request
dataManager.getCachedPage(req.originalUrl, function (err, cache) {
if (cache === null) {
return next();
}
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(cache);
res.end();
});
return;
}
try {
async.parallel([
function (callback) {
dataManager.getAllSoftware(['name', 'url', 'globalPlugin'], callback);
},
function (callback) {
if (req.user !== undefined) {
dataManager.getPluginsOfUser(req.user.username, ['name', 'software'], callback);
} else {
callback(null, []);
}
}
], function(err, results) {
if (err) {
return next(err);
}
res.locals.allSoftware = results[0];
res.locals.myPlugins = results[1];
// Replace the software id with a proper object
for (let i = 0; i < results[1].length; i++) {
for (let j = 0; j < results[0].length; j++) {
if (results[1][i].software === results[0][j].id) {
results[1][i].software = results[0][j];
}
}
}
next();
});
} catch (err) {
console.log(err);
}
});
// Middleware to include the custom color local
app.use(function (req, res, next) {
let customColor1 = req.cookies["custom-color1"];
customColor1 = customColor1 === undefined ? 'teal' : customColor1;
res.locals.customColor1 = customColor1;
next();
});
app.use('/', require('./routes/index'));
app.use('/login', require('./routes/login'));
app.use('/logout', require('./routes/logout'));
app.use('/register', require('./routes/register'));
app.use('/submitData', require('./routes/submitData'));
app.use('/global', require('./routes/global'));
app.use('/plugin', require('./routes/plugin'));
app.use('/author', require('./routes/author'));
app.use('/editPlugin', require('./routes/editPlugin'));
app.use('/plugin-list', require('./routes/pluginList'));
app.use('/add-plugin', require('./routes/addPlugin'));
app.use('/getting-started', require('./routes/gettingStarted'));
app.use('/help/custom-charts', require('./routes/customCharts'));
app.use('/imprint', require('./routes/imprint'));
app.use('/privacy-policy', require('./routes/privacyPolicy'));
app.use('/credits', require('./routes/credits'));
app.use('/faq', require('./routes/faq'));
app.use('/help/rest-api', require('./routes/restApi'));
app.use('/admin', require('./routes/admin'));
app.use('/what-is-my-plugin-id', require('./routes/whatIsMyPluginId'));
// Api v1
app.use('/api/v1/plugins', require('./routes/api/v1/plugin'));
app.use('/api/v1/datatable', require('./routes/api/v1/datatable'));
app.use('/api/v1/software', require('./routes/api/v1/software'));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
if (req.path.startsWith('/api')) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.writeHead(404, {'Content-Type': 'application/json'});
res.write(JSON.stringify({error: 'Invalid URL'}));
res.end();
} else {
next(err);
}
});
// error handler
app.use(function(err, req, res, next) {
res.status(err.status || 500);
if (err.status === undefined) {
err.status = 500;
}
if (err.message === undefined) {
err.message = 'Unknown';
}
console.log(JSON.stringify(err));
res.render('error', {
message: err.message,
error: err
});
});
app.locals.dataManager = dataManager;
module.exports = app;