Skip to content

Commit efeea17

Browse files
authored
Merge pull request #45 from t-ho/dev
Add rate limiting
2 parents 2f9c2a7 + 3a9d07a commit efeea17

14 files changed

Lines changed: 360 additions & 277 deletions

File tree

nginx-proxy/templates/service.conf.prod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Specify file cache expiration.
77
include h5bp/web_performance/cache_expiration.conf;
88

9+
limit_req_zone $binary_remote_addr zone=generallimit:10m rate=10r/s;
10+
911
upstream client-cluster {
1012
server client:3000;
1113
}
@@ -78,6 +80,8 @@ server {
7880
}
7981

8082
location /api {
83+
limit_req zone=generallimit burst=20 nodelay;
84+
limit_req_status 429;
8185
proxy_pass http://api-server-cluster;
8286
}
8387

package-lock.json

Lines changed: 49 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
},
2525
"homepage": "https://tdev.app/mern-stack",
2626
"devDependencies": {
27-
"chalk": "^4.1.0",
28-
"concurrently": "^6.0.0",
29-
"dotenv": "^8.2.0",
27+
"chalk": "^4.1.1",
28+
"concurrently": "^6.2.0",
29+
"dotenv": "^10.0.0",
3030
"figlet": "^1.5.0",
3131
"husky": "^4.3.8",
3232
"lodash": "^4.17.21",
33-
"ngrok": "^4.0.0",
34-
"prettier": "^2.2.1",
33+
"ngrok": "^4.0.1",
34+
"prettier": "^2.3.0",
3535
"pretty-quick": "^3.1.0",
3636
"tree-kill": "^1.2.2"
3737
},

server/config/config.default.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,6 @@ let defaultConfig = {
5858
uri: 'This will be overriden by environment variable MONGO_URI',
5959
testUri: 'mongodb://localhost:27017/mern_test',
6060
},
61-
sendgrid: {
62-
apiKey: 'This will be overriden by environment variable SENDGRID_API_KEY',
63-
},
64-
server: {
65-
host: 'This will be overriden by environment variable SERVER_HOST',
66-
port: 'This will be overriden by environment variable SERVER_PORT',
67-
publicUrl:
68-
'This will be overriden by environment variable SERVER_PUBLIC_URL',
69-
},
70-
paths: {
71-
root: fspath.normalize(`${__dirname}/..`),
72-
},
7361
oauth: {
7462
google: {
7563
clientId:
@@ -84,10 +72,30 @@ let defaultConfig = {
8472
'This will be overriden by environment variable FACEBOOK_APP_SECRET',
8573
},
8674
},
75+
paths: {
76+
root: fspath.normalize(`${__dirname}/..`),
77+
},
78+
rateLimit: {
79+
enabled: false,
80+
},
8781
seed: {
8882
logging: true,
8983
users: [],
9084
},
85+
sendgrid: {
86+
apiKey: 'This will be overriden by environment variable SENDGRID_API_KEY',
87+
},
88+
server: {
89+
host: 'This will be overriden by environment variable SERVER_HOST',
90+
port: 'This will be overriden by environment variable SERVER_PORT',
91+
publicUrl:
92+
'This will be overriden by environment variable SERVER_PUBLIC_URL',
93+
},
94+
trustProxy: {
95+
enabled: false,
96+
// see https://expressjs.com/en/guide/behind-proxies.html
97+
value: 0,
98+
},
9199
};
92100

93101
module.exports = defaultConfig;

server/config/config.prod.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ let prodConfig = {
3838
root: fspath.normalize(`${__dirname}/..`),
3939
},
4040
oauth: {},
41+
rateLimit: {
42+
enabled: true,
43+
},
4144
seed: {
4245
logging: true,
4346
users: [
@@ -59,6 +62,11 @@ let prodConfig = {
5962
},
6063
],
6164
},
65+
trustProxy: {
66+
enabled: true,
67+
// see https://expressjs.com/en/guide/behind-proxies.html
68+
value: 1,
69+
},
6270
};
6371

6472
prodConfig = _.merge({}, defaultConfig, prodConfig);

server/config/config.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ let testConfig = {
2121
testUri: `mongodb://localhost:27017/${defaultConfig.app.name}_test`,
2222
},
2323
oauth: {},
24+
rateLimit: {
25+
enabled: false,
26+
},
2427
seed: {
2528
logging: false,
2629
users: [
@@ -218,6 +221,11 @@ let testConfig = {
218221
},
219222
],
220223
},
224+
trustProxy: {
225+
enabled: false,
226+
// see https://expressjs.com/en/guide/behind-proxies.html
227+
value: 0,
228+
},
221229
};
222230

223231
testConfig = _.merge({}, defaultConfig, testConfig);

server/controllers/auth.controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ const sendEmailHelperAsync = (
585585
templatePath: `${config.paths.root}/templates/email.html`,
586586
dynamicTemplateData: {
587587
boxTitle: title,
588-
firstName: user.firstName,
589588
content,
590589
buttonText,
591590
url,

server/core/express.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const constants = require('./constants');
1414
// App Setup
1515
const app = express();
1616

17+
if (config.trustProxy.enabled) {
18+
app.set('trust proxy', config.trustProxy.value);
19+
}
20+
1721
// Logger
1822
if (config.morgan.enabled) {
1923
app.use(morgan(config.morgan.format, config.morgan.options));
@@ -62,7 +66,7 @@ app.use((req, res, next) => {
6266
// and send stacktrace to client
6367
if (config.env === constants.ENV_DEV) {
6468
app.use((err, req, res, next) => {
65-
console.log(err.stack);
69+
console.log('[DEV]', err.stack);
6670
res
6771
.status(err.status || 400)
6872
.json({ error: { message: err.message, details: err.stack } });

server/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const displayConfigurationStatus = () => {
4848
console.log(chalk.gray(`[*] Cors: ${config.cors.enabled}`));
4949
console.log(chalk.gray(`[*] Helmet: ${config.helmet.enabled}`));
5050
console.log(chalk.gray(`[*] Morgan: ${config.morgan.enabled}`));
51+
console.log(chalk.gray(`[*] RateLimit: ${config.rateLimit.enabled}`));
52+
console.log(chalk.gray(`[*] TrustProxy: ${config.trustProxy.enabled}`));
5153
};
5254

5355
displayConfigurationStatus();

0 commit comments

Comments
 (0)