Skip to content

Commit

Permalink
Merge pull request #113 from pvdthings/cleanup
Browse files Browse the repository at this point in the history
api: cleanup and rate limiting
  • Loading branch information
dillonfagan authored Jan 28, 2025
2 parents 556b8cb + b6f35cc commit 12c3881
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 314 deletions.
5 changes: 4 additions & 1 deletion apps/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
## Environment variables
You'll need to set these environment variables in a `.env` file at the root of the project folder:
```js
NODE_ENV=development // or 'production'
NODE_ENV=[value] // 'development' or 'production' (default)

API_KEY=[value]

RATE_LIMIT_WINDOW_MINUTES=[value] // 15 (default)
RATE_LIMIT_FAILED_ATTEMPTS=[value] // 5 (default)

AIRTABLE_KEY=[value]
AIRTABLE_BASE_ID=[value]

Expand Down
7 changes: 0 additions & 7 deletions apps/api/auth/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions apps/api/auth/routes/check.js

This file was deleted.

27 changes: 27 additions & 0 deletions apps/api/middleware/cors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { isDevelopment } = require('../utils/environment');

const allowedOrigins = process.env.ACCESS_CONTROL_ALLOW_ORIGIN.split(',');

const corsOptions = Object.freeze({
allowedHeaders: [
'Origin',
'x-api-key',
'X-Requested-With',
'Content-Type',
'Accept',
'x-access-token'
],
credentials: true,
origin: (origin, callback) => {
if (allowedOrigins.includes(origin) || !origin || isDevelopment()) {
callback(null, true);
} else {
console.log('origin', origin);
callback(new Error('CORS Error'));
}
}
});

const cors = require('cors')(corsOptions);

module.exports = cors;
5 changes: 5 additions & 0 deletions apps/api/middleware/notFound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const notFound = (req, res, next) => {
res.status(404).send();
};

module.exports = notFound;
12 changes: 12 additions & 0 deletions apps/api/middleware/rateLimit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const rateLimit = require('express-rate-limit');

const minutes = process.env.RATE_LIMIT_WINDOW_MINUTES || 15;
const failedAttempts = process.env.RATE_LIMIT_FAILED_ATTEMPTS || 5;

const limit = rateLimit({
windowMs: minutes * 60 * 1000, // 15 minutes
limit: failedAttempts,
skipSuccessfulRequests: true,
});

module.exports = limit;
Loading

0 comments on commit 12c3881

Please sign in to comment.