-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (49 loc) · 1.33 KB
/
index.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
const {CronJob}=require('cron')
const express=require('express');
const app = express();
const PORT=process.env.PORT||3000;
app.use(express.json())
const LIMIT_SIZE=10;
const TOKENS=[];
const refillBucket=()=>{
if(TOKENS.length<LIMIT_SIZE){
TOKENS.push(Date.now());
}
};
app.get('/bucket',(req,res)=>{
res.json({
bucketLimit:LIMIT_SIZE,
currentBucketSize:TOKENS.length,
bucket:TOKENS
});
});
const limitingMiddleware =(req, res, next) =>{
if(TOKENS.length>0){
const token= TOKENS.shift();
console.log(`Token ${token} is used`);
res.set('X-Ratelimit-Remaining',TOKENS.length);
next();
}else{
res.status(429).set('X-RateLimit-Remaining', 0).set('Retry-After', 2).json({
success: false,
message: 'Too many requests'
});
}
}
app.use(limitingMiddleware);
app.get('/test',(req, res) => {
const ROCK_PAPER_SCISSORS = ['rock 🪨', 'paper 📃', 'scissors ✂️'];
const randomIndex = Math.floor(Math.random() * 3);
const randomChoice = ROCK_PAPER_SCISSORS[randomIndex];
res.json({
success: true,
message: `You got ${randomChoice}`
});
})
const job = new CronJob('*/2 * * * * *', () => {
refillBucket();
});
app.listen(PORT,()=>{
console.log(`Server started on port ${PORT}`);
job.start();
})