Skip to content

Commit b00fd2c

Browse files
Add ability to make the API readonly
1 parent 67aa420 commit b00fd2c

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Dev: [![CircleCI](https://circleci.com/gh/topcoder-platform/challenge-api/tree/d
3434
Configuration for the application is at `config/default.js`.
3535
The following parameters can be set in config files or in env variables:
3636

37+
- READONLY: sets the API in read-only mode. POST/PUT/PATCH/DELETE operations will return 403 Forbidden
3738
- LOG_LEVEL: the log level, default is 'debug'
3839
- PORT: the server port, default is 3000
3940
- AUTH_SECRET: The authorization secret used during token verification.

app.js

+9
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@ const fileUpload = require('express-fileupload')
1616
const YAML = require('yamljs')
1717
const swaggerUi = require('swagger-ui-express')
1818
const challengeAPISwaggerDoc = YAML.load('./docs/swagger.yaml')
19+
const { ForbiddenError } = require('./src/common/errors')
1920

2021
// setup express app
2122
const app = express()
2223

24+
// Disable POST, PUT, PATCH, DELETE operations if READONLY is set to true
25+
app.use((req, res, next) => {
26+
if (config.READONLY && ['POST', 'PUT', 'PATCH', 'DELETE'].includes(req.method)) {
27+
throw new ForbiddenError('Action is temporarely not allowed!')
28+
}
29+
next()
30+
})
31+
2332
// serve challenge V5 API swagger definition
2433
app.use('/v5/challenges/docs', swaggerUi.serve, swaggerUi.setup(challengeAPISwaggerDoc))
2534

config/default.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
module.exports = {
6+
READONLY: process.env.READONLY || false,
67
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
78
PORT: process.env.PORT || 3000,
89
API_VERSION: process.env.API_VERSION || 'v5',

0 commit comments

Comments
 (0)