Skip to content
Snippets Groups Projects
Commit dc0fbe78 authored by aurieh's avatar aurieh
Browse files

Rewrite index, freshclam

parent 087b492a
No related branches found
No related tags found
No related merge requests found
// Required modules
const express = require('express');
const koa = require('koa');
// Check for required environment variables
for (let env of [
......@@ -13,39 +13,44 @@ for (let env of [
}
}
// Create Express app
const app = express();
app.disable('x-powered-by');
app.disable('etag');
// Create Koa app
const app = koa();
const route = require("koa-route");
/**
* Parse request body.
*/
app.use((req, res, next) => {
app.use(next => {
if (req.method === 'GET' || req.method === 'DELETE') {
return next();
yield next;
return;
}
if (!req.is('application/json')) {
return res.status(400).json({
if (!this.headers['application/json']) {
this.status = 400;
this.body = {
code: 400,
message: 'invalid content type, should be application/json'
});
};
yield next;
return;
}
let rawData = '';
req.on('data', chunk => {
this.req.on('data', chunk => {
rawData += chunk.toString();
});
req.on('end', () => {
this.req.on('end', () => {
try {
req.body = JSON.parse(rawData);
next();
this.req.body = JSON.parse(rawData);
yield next;
return;
} catch (err) {
res.status(400).json({
this.body = {
code: 400,
message: 'invalid body data, should be a valid JSON string'
});
};
this.status = 400;
}
});
});
......@@ -54,22 +59,22 @@ app.use((req, res, next) => {
* POST /scan
* Scan the file specified in the S3 event in the body.
*/
app.post('/scan', require('./routes/scan.js'));
app.use(route.post('/scan', require('./routes/scan.js')));
/**
* GET /freshclam
* Run freshclam to update the virus database.
*/
app.get('/freshclam', require('./routes/freshclam.js'));
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.
*/
app.get('/health', (req, res, next) => {
app.use(route.get('/health', (req, res, next) => {
res.status(200).end();
});
}));
// Listen on 8080
app.listen(8080);
......@@ -5,12 +5,16 @@ const freshClam = require('../lib/freshclam.js');
* > GET /freshclam
* Run freshclam to update the virus database.
*/
module.exports = function freshclam (req, res, next) {
module.exports = function* freshclam(next) {
freshClam().then(() => {
res.status(200).end();
this.body = null;
this.status = 200;
yield;
}).catch(err => {
console.error('failed to update virus definitions');
console.error(err);
res.status(500).end();
this.body = null;
this.status = 500;
yield;
});
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment