mirror of
https://github.com/JuniorDark/RustyHearts-API.git
synced 2026-05-07 14:11:44 -04:00
Version 1.3.0
- Implemented authentication and billing routes for Jpn region. - Refactored and changed the project structure from CommonJS to ES Modules
This commit is contained in:
parent
9584e58143
commit
c3d9e7afb5
76 changed files with 3847 additions and 1109 deletions
7
src/lib/closeConnection.js
Normal file
7
src/lib/closeConnection.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// This middleware sets the "Connection" header to "close" for all responses.
|
||||
// It ensures that the connection is closed after the response is sent, which can help with resource management and performance.
|
||||
// The 'next()' function is called to pass control to the next middleware or route handler in the stack.
|
||||
export const closeConnection = (req, res, next) => {
|
||||
res.set("Connection", "close");
|
||||
next();
|
||||
};
|
||||
43
src/lib/rateLimiter.js
Normal file
43
src/lib/rateLimiter.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import rateLimitConfig from "../config/rateLimitConfig.js";
|
||||
import { getClientIp } from "../utils/getClientIp.js";
|
||||
|
||||
async function rateLimiter(req, res, next) {
|
||||
try {
|
||||
const path = (req.baseUrl + req.path).replace(/\/+$/, "");
|
||||
const limiterKey = Array.from(rateLimitConfig.limiters.keys()).find((key) =>
|
||||
path.startsWith(key)
|
||||
);
|
||||
|
||||
if (!limiterKey) {
|
||||
console.warn(`[RateLimiter] No limiter found for path: "${path}"`);
|
||||
return next();
|
||||
}
|
||||
|
||||
const limiter = rateLimitConfig.getLimiter(limiterKey);
|
||||
|
||||
const clientIP = getClientIp(req);
|
||||
const rateLimitRes = await limiter.consume(clientIP);
|
||||
|
||||
res.set({
|
||||
"Retry-After": rateLimitRes.msBeforeNext / 1000,
|
||||
"X-RateLimit-Limit": limiter.points,
|
||||
"X-RateLimit-Remaining": rateLimitRes.remainingPoints,
|
||||
"X-RateLimit-Reset": new Date(Date.now() + rateLimitRes.msBeforeNext),
|
||||
});
|
||||
|
||||
next();
|
||||
} catch (rateLimitRes) {
|
||||
res.set({
|
||||
"Retry-After": rateLimitRes.msBeforeNext / 1000,
|
||||
});
|
||||
|
||||
return res.status(429).json({
|
||||
error: "Too many requests",
|
||||
message: `You've exceeded the rate limit. Please try again in ${Math.ceil(
|
||||
rateLimitRes.msBeforeNext / 1000
|
||||
)} seconds.`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default rateLimiter;
|
||||
Loading…
Add table
Add a link
Reference in a new issue