-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
138 lines (125 loc) · 3.84 KB
/
server.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
const path = require('path');
const express = require('express');
const app = express();
const { query, validationResult, sanitizeQuery } = require('express-validator');
const processLeaderboard = require('./middleware/processLeaderboard');
const setTheme = require('./middleware/setTheme');
const resetLeaderboard = require('./middleware/resetLeaderboard');
const getLeaderboardSettings = require('./middleware/getLeaderboardSettings');
const getReferrals = require('./middleware/getReferrals');
const useReferral = require('./middleware/useReferral');
const requestIP = require('./middleware/requestIP');
const excludeFavicon = require('./middleware/exludeFavicon');
const buildJSON = require('./middleware/buildJSON');
require('dotenv').config();
function errorHandler(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
}
app.set('views', './views');
app.set('view engine', 'pug');
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use(excludeFavicon);
// Root Route
app.get('/', (req, res) => {
res.render('homepage');
//res.send('API is running.');
});
// Admin Routes
app.get(
'/admin/:channel',
getLeaderboardSettings,
getReferrals,
processLeaderboard,
(req, res) => {
res.render('admin');
}
);
app.get('/admin/:channel/reset', getLeaderboardSettings, resetLeaderboard);
app.post('/admin/:channel/set-theme/:theme', getLeaderboardSettings, setTheme);
app.get('/admin', (req, res) => {
res.send('Thou shalt not pass!');
});
app.get(
'/:channel',
getLeaderboardSettings,
getReferrals,
processLeaderboard,
(req, res) => {
res.render('index');
}
);
app.get(
'/api/v1/getReferrals',
[
query('channel')
.exists().withMessage('Please specify a channel')
.custom(channel => {
if (channel != undefined && channel.search(/[^a-z_]/) > -1) {
throw new Error('Channel contains invalid characters')
} else {
return true;
}
})
.trim()
.escape(),
query('referrer')
.optional()
.trim()
.escape(),
query('since')
.optional()
.isISO8601()
],
getReferrals,
buildJSON,
(req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
res.json(req.data);
}
);
app.get(
'/api/v1/getLeaderboard',
[
query('channel')
.exists().withMessage('Please specify a channel')
.custom(channel => {
if (channel != undefined && channel.search(/[^a-z_]/) > -1) {
throw new Error('Channel contains invalid characters')
} else {
return true;
}
})
.trim()
.escape(),
query('referrer')
.optional()
.trim()
.escape(),
query('since')
.optional()
.isISO8601()
],
getReferrals,
getLeaderboardSettings,
processLeaderboard,
buildJSON,
(req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
res.json(req.data);
}
);
// Referral Route
app.get('/:channel/:referrer', requestIP, useReferral);
app.get('*', function(req, res){
res.status(404).send('Not Found');
});
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));