From dd819c45b58088f9a98384ab237268d865836fe0 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 24 Jun 2017 22:05:18 +0200 Subject: refactor: re-architect to be closer to ADR --- src/actions.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/actions.js (limited to 'src/actions.js') diff --git a/src/actions.js b/src/actions.js new file mode 100644 index 0000000..c6e04c0 --- /dev/null +++ b/src/actions.js @@ -0,0 +1,56 @@ +'use strict' + +const send = require('koa-send') +const streamify = require('stream-array') +const responders = require('./responders') + +function toArrayStream (iterator) { + return streamify(Array.from(iterator.entries())) +} + +function home (config, posts) { + const postsStream = toArrayStream(posts) + return async function (ctx, next) { + responders.home(ctx, config, postsStream) + } +} + +function post (config, posts) { + return async function (ctx, next) { + ctx.assert(posts.has(ctx.params.filename), 404, 'Post not found') + const post = posts.get(ctx.params.filename) + + responders.post(ctx, config, post) + } +} + +function taxonGenerator (config, term, items) { + return async function (ctx, next) { + const value = ctx.params.value + ctx.assert( + items.has(ctx.params.value), + 404, + `Could not find ${term} ${value}` + ) + + const taxonItems = toArrayStream(items.get(value)) + + responders.taxon(ctx, config, taxonItems) + } +} + +const prefix = /^\/static\// +async function serveFiles (ctx) { + if (prefix.test(ctx.path)) { + await send(ctx, ctx.path.replace(prefix, ''), { + root: './static' + }) + } +} + +module.exports = { + home, + post, + taxonGenerator, + serveFiles +} -- cgit 1.4.1