Skip to content
Snippets Groups Projects
index.js 2.2 KiB
Newer Older
// Required modules
aurieh's avatar
aurieh committed
const koa = require('koa');

// Check for required environment variables
for (let env of [
Dean's avatar
Dean committed
  '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}"`);
  }
}

aurieh's avatar
aurieh committed
// Create Koa app
const app = koa();
aurieh's avatar
aurieh committed
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);
  }
});
Dean's avatar
Dean committed
 * Parse request body.
aurieh's avatar
aurieh committed
app.use(function* (next) {
  if (this.req.method === 'GET' || this.req.method === 'DELETE') {
aurieh's avatar
aurieh committed
    yield next;
    return;
Dean's avatar
Dean committed
  }
aurieh's avatar
aurieh committed
  if (!this.headers['application/json']) {
    this.status = 400;
    this.body = {
Dean's avatar
Dean committed
      code: 400,
      message: 'invalid content type, should be application/json'
aurieh's avatar
aurieh committed
    };
    yield next;
    return;
Dean's avatar
Dean committed
  }
Dean's avatar
Dean committed
  let rawData = '';
Dean's avatar
Dean committed

aurieh's avatar
aurieh committed
  this.req.on('data', chunk => {
Dean's avatar
Dean committed
    rawData += chunk.toString();
aurieh's avatar
aurieh committed
  this.req.on('end', () => {
Dean's avatar
Dean committed
    try {
aurieh's avatar
aurieh committed
      this.req.body = JSON.parse(rawData);
aurieh's avatar
aurieh committed
      next();
aurieh's avatar
aurieh committed
      return;
Dean's avatar
Dean committed
    } catch (err) {
aurieh's avatar
aurieh committed
      this.body = {
Dean's avatar
Dean committed
        code: 400,
        message: 'invalid body data, should be a valid JSON string'
aurieh's avatar
aurieh committed
      };
      this.status = 400;
Dean's avatar
Dean committed
    }
Dean's avatar
Dean committed
});
Dean's avatar
Dean committed
 * POST /scan
 * Scan the file specified in the S3 event in the body.
aurieh's avatar
aurieh committed
app.use(route.post('/scan', require('./routes/scan.js')));
Dean's avatar
Dean committed
 * GET /freshclam
 * Run freshclam to update the virus database.
aurieh's avatar
aurieh committed
app.use(route.get('/freshclam', require('./routes/freshclam.js')));
Dean's avatar
Dean committed
 * GET /health
 * Return a 200 OK response, so that Elastic Beanstalk can check if the server
 * is still online.
aurieh's avatar
aurieh committed
app.use(route.get('/health', () => {
  this.status = 200;
aurieh's avatar
aurieh committed
}));
Dean's avatar
Dean committed
// Listen on 8080
app.listen(8080);