randydev commited on
Commit
89e56d1
·
verified ·
1 Parent(s): c40eb97

Update middleware/midware.js

Browse files
Files changed (1) hide show
  1. middleware/midware.js +9 -9
middleware/midware.js CHANGED
@@ -1,9 +1,8 @@
1
  import { Database } from '../database/database.js';
2
- import { rateLimit } from 'express-rate-limit';
3
- import rateLimitMongo from "rate-limit-mongo";
4
- import * as config from '../config.js';
5
 
6
- const MongoStore = rateLimitMongo;
7
 
8
  const authenticateApiKey = async (req, res, next) => {
9
  const apiKey = req.headers['x-api-key'];
@@ -25,18 +24,19 @@ const authenticateApiKey = async (req, res, next) => {
25
  };
26
 
27
 
28
- const apiLimiter = rateLimit({
29
  /*
30
  store: new MongoStore({
31
  uri: dbUrls,
32
  collectionName: "rateLimits",
33
  }),
34
  */
35
- windowMs: 2 * 60 * 1000,
36
- max: 3,
 
37
  keyGenerator: (req) => req.headers["x-api-key"],
38
- standardHeaders: true,
39
- legacyHeaders: false,
40
  message: (req, res) => {
41
  const retryAfterMs = res.getHeaders()["retry-after"] * 1000 || 2 * 60 * 1000;
42
  const remainingSeconds = Math.ceil(retryAfterMs / 1000);
 
1
  import { Database } from '../database/database.js';
2
+ import { MongoStorage } from "@canmertinyo/rate-limiter-mongo";
3
+ import { rateLimiter } from "@canmertinyo/rate-limiter-core";
 
4
 
5
+ import * as config from '../config.js';
6
 
7
  const authenticateApiKey = async (req, res, next) => {
8
  const apiKey = req.headers['x-api-key'];
 
24
  };
25
 
26
 
27
+ const apiLimiter = rateLimiter({
28
  /*
29
  store: new MongoStore({
30
  uri: dbUrls,
31
  collectionName: "rateLimits",
32
  }),
33
  */
34
+ ms: 2 * 60 * 1000,
35
+ maxRequest: 3,
36
+ storage: new MongoStorage(config.dbUrl),
37
  keyGenerator: (req) => req.headers["x-api-key"],
38
+ // standardHeaders: true,
39
+ // legacyHeaders: false,
40
  message: (req, res) => {
41
  const retryAfterMs = res.getHeaders()["retry-after"] * 1000 || 2 * 60 * 1000;
42
  const remainingSeconds = Math.ceil(retryAfterMs / 1000);