From dc0fbe7886b30bbd78f0d555376024012de73740 Mon Sep 17 00:00:00 2001 From: aurieh <auriteh@gmail.com> Date: Tue, 27 Dec 2016 19:14:34 +0200 Subject: [PATCH] Rewrite index, freshclam --- index.js | 45 +++++++++++++++++++++++++-------------------- routes/freshclam.js | 12 ++++++++---- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index c2f4465..5294bf7 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ // 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); diff --git a/routes/freshclam.js b/routes/freshclam.js index 9575657..a0b952a 100644 --- a/routes/freshclam.js +++ b/routes/freshclam.js @@ -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; }); -}; +} -- GitLab