Newer
Older
// Check for required environment variables
for (let env of [
'AWS_ACCESSKEY', // Access key ID
'AWS_INFECTEDSNSARN', // Notification SNS ARN
'AWS_REGION', // AWS region
'AWS_SECRETKEY' // Secret key
]) {
if (!process.env.hasOwnProperty(env)) {
throw new Error(`missing required environment variable "${env}"`);
}
}
const route = require('koa-route');
// Error handler
app.use(function* (next) {
try {
yield next;
} catch (err) {
if (err.isBoom) {
this.status = err.output.statusCode || 500;
this.body = err.output.payload || '';
this.set('content-type', 'application/json; charset=utf-8');
for (const header in err.output.headers || {}) {
this.set(header, err.output.headers[header]);
}
} else {
this.status = err.status || 500;
this.body = err.message || '';
}
if (this.status > 499) this.app.emit('error', err, this);
}
});
app.use(function* (next) {
if (this.req.method === 'GET' || this.req.method === 'DELETE') {
if (!this.headers['application/json']) {
this.status = 400;
this.body = {
code: 400,
message: 'invalid content type, should be application/json'
code: 400,
message: 'invalid body data, should be a valid JSON string'
* POST /scan
* Scan the file specified in the S3 event in the body.
app.use(route.post('/scan', require('./routes/scan.js')));
* GET /freshclam
* Run freshclam to update the virus database.
app.use(route.get('/freshclam', require('./routes/freshclam.js')));
* GET /health
* Return a 200 OK response, so that Elastic Beanstalk can check if the server
* is still online.