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/app.js | 54 +++++++++++------------------------------------------- 1 file changed, 11 insertions(+), 43 deletions(-) (limited to 'src/app.js') diff --git a/src/app.js b/src/app.js index 84722fc..46ebd2e 100644 --- a/src/app.js +++ b/src/app.js @@ -3,10 +3,7 @@ const Koa = require('koa') const app = new Koa() -const streamify = require('stream-array') - -const send = require('koa-send') -const responders = require('./responders.js') +const actions = require('./actions.js') const config = require('./modules/config.js') @@ -15,51 +12,22 @@ const router = new Router() app.context.getURL = router.url.bind(router) -const Posts = require('./modules/posts.js') -const posts = Posts.getFolder(config.posts.folder) - -function toArrayStream (iterator) { - return streamify(Array.from(iterator.entries())) -} - -const postsStream = toArrayStream(posts) -router.get('home', '/', async function (ctx, next) { - responders.home(ctx, config, postsStream) -}) - -router.get('post', '/post/:filename', async function (ctx, next) { - ctx.assert(posts.has(ctx.params.filename), 404, 'Post not found') - const post = posts.get(ctx.params.filename) - post.body = Posts.render(post) - - responders.post(ctx, config, post) -}) +const Posts = require('./domain/posts.js')(config.posts) -const taxonomies = Posts.taxonomise(config.taxonomies, posts) -for (let [term, items] of taxonomies) { - router.get(`taxon-${term}`, `/${term}/:value`, async function (ctx, next) { - const value = ctx.params.value - ctx.assert( - items.has(ctx.params.value), - 404, - `Could not find ${term} ${value}` - ) +router.get('home', '/', actions.home(config, Posts.posts)) - const taxonItems = toArrayStream(items.get(value)) +router.get('post', '/post/:filename', actions.post(config, Posts.posts)) - responders.taxon(ctx, config, taxonItems) - }) +for (let [term, items] of Posts.taxonomies) { + router.get( + `taxon-${term}`, + `/${term}/:value`, + actions.taxonGenerator(config, term, items) + ) } app.use(router.routes()).use(router.allowedMethods()) -const prefix = /^\/static\// -app.use(async function (ctx) { - if (prefix.test(ctx.path)) { - await send(ctx, ctx.path.replace(prefix, ''), { - root: './static' - }) - } -}) +app.use(actions.serveFiles) module.exports = app -- cgit 1.4.1