Skip to content

Commit 6a4a712

Browse files
committed
chore: add rate limiter
1 parent 123e926 commit 6a4a712

File tree

22 files changed

+154
-11
lines changed

22 files changed

+154
-11
lines changed

examples/cloudrun/knex/mysql2/index.cjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ const knex = require('knex');
1818

1919
const app = express();
2020

21+
// set up rate limiter: maximum of five requests per minute
22+
var RateLimit = require('express-rate-limit');
23+
var limiter = RateLimit({
24+
// 15 minutes
25+
windowMs: 15 * 60 * 1000,
26+
// max 100 requests per windowMs
27+
max: 100,
28+
});
29+
30+
// apply rate limiter to all requests
31+
app.use(limiter);
32+
2133
// Connector and connection pools are initialized as null to allow for lazy instantiation.
2234
// Lazy instantiation is a best practice for Cloud Run applications because it allows
2335
// the application to start faster and only initialize connections when they are needed.

examples/cloudrun/knex/mysql2/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"@google-cloud/cloud-sql-connector": "^1.8.4",
1212
"express": "^5.1.0",
1313
"knex": "^3.1.0",
14-
"mysql2": "^3.15.2"
14+
"mysql2": "^3.15.2",
15+
"express-rate-limit": "8.2.1"
1516
},
1617
"devDependencies": {
1718
}

examples/cloudrun/knex/pg/index.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ import knex from 'knex';
1818

1919
const app = express();
2020

21+
// set up rate limiter: maximum of five requests per minute
22+
var RateLimit = require('express-rate-limit');
23+
var limiter = RateLimit({
24+
// 15 minutes
25+
windowMs: 15 * 60 * 1000,
26+
// max 100 requests per windowMs
27+
max: 100,
28+
});
29+
30+
// apply rate limiter to all requests
31+
app.use(limiter);
32+
2133
// Connector and connection pools are initialized as null to allow for lazy instantiation.
2234
// Lazy instantiation is a best practice for Cloud Run applications because it allows
2335
// the application to start faster and only initialize connections when they are needed.

examples/cloudrun/knex/pg/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"@google-cloud/cloud-sql-connector": "^1.8.4",
1212
"express": "^5.1.0",
1313
"knex": "^3.1.0",
14-
"pg": "^8.16.3"
14+
"pg": "^8.16.3",
15+
"express-rate-limit": "8.2.1"
1516
},
1617
"devDependencies": {
1718
}

examples/cloudrun/knex/tedious/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ import knex, {Knex} from 'knex';
2222

2323
const app = express();
2424

25+
// set up rate limiter: maximum of five requests per minute
26+
var RateLimit = require('express-rate-limit');
27+
var limiter = RateLimit({
28+
// 15 minutes
29+
windowMs: 15 * 60 * 1000,
30+
// max 100 requests per windowMs
31+
max: 100,
32+
});
33+
34+
// apply rate limiter to all requests
35+
app.use(limiter);
36+
2537
// Connector and connection pools are initialized as null to allow for lazy instantiation.
2638
// Lazy instantiation is a best practice for Cloud Run applications because it allows
2739
// the application to start faster and only initialize connections when they are needed.

examples/cloudrun/knex/tedious/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"express": "^5.1.0",
1212
"@google-cloud/cloud-sql-connector": "^1.8.4",
1313
"tedious": "^18.6.1",
14-
"knex": "^3.1.0"
14+
"knex": "^3.1.0",
15+
"express-rate-limit": "8.2.1"
1516
},
1617
"devDependencies": {
1718
"@types/express": "^5.0.5",

examples/cloudrun/prisma/mysql/index.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ import {PrismaClient} from '@prisma/client';
1919

2020
const app = express();
2121

22+
// set up rate limiter: maximum of five requests per minute
23+
var RateLimit = require('express-rate-limit');
24+
var limiter = RateLimit({
25+
// 15 minutes
26+
windowMs: 15 * 60 * 1000,
27+
// max 100 requests per windowMs
28+
max: 100,
29+
});
30+
31+
// apply rate limiter to all requests
32+
app.use(limiter);
33+
2234
// Connector and connection pools are initialized as null to allow for lazy instantiation.
2335
// Lazy instantiation is a best practice for Cloud Run applications because it allows
2436
// the application to start faster and only initialize connections when they are needed.

examples/cloudrun/prisma/mysql/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"@google-cloud/cloud-sql-connector": "^1.8.4",
1212
"express": "^5.1.0",
1313
"prisma": "^5.22.0",
14-
"@prisma/client": "^5.22.0"
14+
"@prisma/client": "^5.22.0",
15+
"express-rate-limit": "8.2.1"
1516
},
1617
"devDependencies": {
1718
}

examples/cloudrun/prisma/postgresql/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ import {PrismaClient} from '@prisma/client';
2424

2525
const app = express();
2626

27+
// set up rate limiter: maximum of five requests per minute
28+
var RateLimit = require('express-rate-limit');
29+
var limiter = RateLimit({
30+
// 15 minutes
31+
windowMs: 15 * 60 * 1000,
32+
// max 100 requests per windowMs
33+
max: 100,
34+
});
35+
36+
// apply rate limiter to all requests
37+
app.use(limiter);
38+
2739
// Connector and connection pools are initialized as null to allow for lazy instantiation.
2840
// Lazy instantiation is a best practice for Cloud Run applications because it allows
2941
// the application to start faster and only initialize connections when they are needed.

examples/cloudrun/prisma/postgresql/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"@google-cloud/cloud-sql-connector": "^1.3.0",
1212
"express": "^5.1.0",
1313
"prisma": "^5.22.0",
14-
"@prisma/client": "^5.22.0"
14+
"@prisma/client": "^5.22.0",
15+
"express-rate-limit": "8.2.1"
1516
},
1617
"devDependencies": {
1718
"@types/express": "^5.0.5",

0 commit comments

Comments
 (0)