forked from osu-cs493-sp19/final-project-team-outta-here
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
112 lines (91 loc) · 2.6 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
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const api = require('./api');
const { connectToDB } = require('./lib/mongo')
const app = express();
const port = process.env.PORT || 8000;
//rate limiting
const redis = require('redis');
const redisPort = process.env.REDIS_PORT || 6379;
const redisHost = process.env.REDIS_HOST;
const redisClient = redis.createClient(redisPort, redisHost);
const rateLimitWindowMillis = 60000;
const rateLimitWindowMaxRequests = 5;
function getTokenBucket(ip) {
return new Promise((resolve, reject) => {
redisClient.hgetall(ip, (err, tokenBucket) => {
if (err) {
reject(err);
} else {
if (tokenBucket) {
tokenBucket.tokens = parseFloat(tokenBucket.tokens);
} else {
tokenBucket = {
tokens: rateLimitWindowMaxRequests,
last: Date.now()
};
}
resolve(tokenBucket);
}
});
});
}
function saveTokenBucket(ip, tokenBucket) {
return new Promise((resolve, reject) => {
redisClient.hmset(ip, tokenBucket, (err, result) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
async function rateLimit(req, res, next) {
try {
const tokenBucket = await getTokenBucket(req.ip);
const timestamp = Date.now();
const ellapsedMillis = timestamp - tokenBucket.last;
const refreshRate = rateLimitWindowMaxRequests / rateLimitWindowMillis;
tokenBucket.tokens += refreshRate * ellapsedMillis;
tokenBucket.tokens = Math.min(rateLimitWindowMaxRequests, tokenBucket.tokens);
tokenBucket.last = timestamp;
if (tokenBucket.tokens >= 1) {
tokenBucket.tokens -= 1;
saveTokenBucket(req.ip, tokenBucket);
next();
} else {
saveTokenBucket(req.ip, tokenBucket);
res.status(429).send({
error: "Too many requests per minute"
});
}
} catch (err) {
console.error(err);
next();
}
}
app.use(rateLimit);
/*
* Morgan is a popular logger.
*/
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(express.static('public'));
/*
* All routes for the API are written in modules in the api/ directory. The
* top-level router lives in api/index.js. That's what we include here, and
* it provides all of the routes.
*/
app.use('/', api);
app.use('*', function (req, res, next) {
res.status(404).json({
error: "Requested resource " + req.originalUrl + " does not exist"
});
});
connectToDB(() => {
app.listen(port, function() {
console.log("== Server is running on port", port);
});
});