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 // Required modules
const express = require('express'); const koa = require('koa');
// Check for required environment variables // Check for required environment variables
for (let env of [ for (let env of [
...@@ -13,39 +13,44 @@ for (let env of [ ...@@ -13,39 +13,44 @@ for (let env of [
} }
} }
// Create Express app // Create Koa app
const app = express(); const app = koa();
app.disable('x-powered-by'); const route = require("koa-route");
app.disable('etag');
/** /**
* Parse request body. * Parse request body.
*/ */
app.use((req, res, next) => { app.use(next => {
if (req.method === 'GET' || req.method === 'DELETE') { if (req.method === 'GET' || req.method === 'DELETE') {
return next(); yield next;
return;
} }
if (!req.is('application/json')) { if (!this.headers['application/json']) {
return res.status(400).json({ this.status = 400;
this.body = {
code: 400, code: 400,
message: 'invalid content type, should be application/json' message: 'invalid content type, should be application/json'
}); };
yield next;
return;
} }
let rawData = ''; let rawData = '';
req.on('data', chunk => { this.req.on('data', chunk => {
rawData += chunk.toString(); rawData += chunk.toString();
}); });
req.on('end', () => { this.req.on('end', () => {
try { try {
req.body = JSON.parse(rawData); this.req.body = JSON.parse(rawData);
next(); yield next;
return;
} catch (err) { } catch (err) {
res.status(400).json({ this.body = {
code: 400, code: 400,
message: 'invalid body data, should be a valid JSON string' message: 'invalid body data, should be a valid JSON string'
}); };
this.status = 400;
} }
}); });
}); });
...@@ -54,22 +59,22 @@ app.use((req, res, next) => { ...@@ -54,22 +59,22 @@ app.use((req, res, next) => {
* POST /scan * POST /scan
* Scan the file specified in the S3 event in the body. * 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 * GET /freshclam
* Run freshclam to update the virus database. * 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 * GET /health
* Return a 200 OK response, so that Elastic Beanstalk can check if the server * Return a 200 OK response, so that Elastic Beanstalk can check if the server
* is still online. * is still online.
*/ */
app.get('/health', (req, res, next) => { app.use(route.get('/health', (req, res, next) => {
res.status(200).end(); res.status(200).end();
}); }));
// Listen on 8080 // Listen on 8080
app.listen(8080); app.listen(8080);
...@@ -5,12 +5,16 @@ const freshClam = require('../lib/freshclam.js'); ...@@ -5,12 +5,16 @@ const freshClam = require('../lib/freshclam.js');
* > GET /freshclam * > GET /freshclam
* Run freshclam to update the virus database. * Run freshclam to update the virus database.
*/ */
module.exports = function freshclam (req, res, next) { module.exports = function* freshclam(next) {
freshClam().then(() => { freshClam().then(() => {
res.status(200).end(); this.body = null;
this.status = 200;
yield;
}).catch(err => { }).catch(err => {
console.error('failed to update virus definitions'); console.error('failed to update virus definitions');
console.error(err); 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