mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 04:39:58 +00:00
api/core: extract rate limit response to function
This commit is contained in:
parent
44f7e4f76c
commit
034f7ebe4a
1 changed files with 11 additions and 9 deletions
|
@ -57,6 +57,16 @@ export const runAPI = (express, app, __dirname) => {
|
||||||
git,
|
git,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const handleRateExceeded = (_, res) => {
|
||||||
|
const { status, body } = createResponse("error", {
|
||||||
|
code: "error.api.rate_exceeded",
|
||||||
|
context: {
|
||||||
|
limit: env.rateLimitWindow
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return res.status(status).json(body);
|
||||||
|
};
|
||||||
|
|
||||||
const apiLimiter = rateLimit({
|
const apiLimiter = rateLimit({
|
||||||
windowMs: env.rateLimitWindow * 1000,
|
windowMs: env.rateLimitWindow * 1000,
|
||||||
max: env.rateLimitMax,
|
max: env.rateLimitMax,
|
||||||
|
@ -68,15 +78,7 @@ export const runAPI = (express, app, __dirname) => {
|
||||||
}
|
}
|
||||||
return generateHmac(getIP(req), ipSalt);
|
return generateHmac(getIP(req), ipSalt);
|
||||||
},
|
},
|
||||||
handler: (req, res) => {
|
handler: handleRateExceeded
|
||||||
const { status, body } = createResponse("error", {
|
|
||||||
code: "error.api.rate_exceeded",
|
|
||||||
context: {
|
|
||||||
limit: env.rateLimitWindow
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return res.status(status).json(body);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const apiLimiterStream = rateLimit({
|
const apiLimiterStream = rateLimit({
|
||||||
|
|
Loading…
Reference in a new issue