From be533ddc740c77551c714836da08a651b81825b6 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 8 Jul 2017 22:30:36 +0200 Subject: refactor: assign responders to actions via front controller --- src/actions.js | 25 ++++++++++++++++--------- src/app.js | 17 +++++++++++++---- src/responders.js | 6 +++--- 3 files changed, 32 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/actions.js b/src/actions.js index e6d7f98..2ad0c77 100644 --- a/src/actions.js +++ b/src/actions.js @@ -3,19 +3,22 @@ const fs = require("fs"); const path = require("path"); const send = require("koa-send"); -const responders = require("./responders"); -function home(config, posts) { +function home(config, responder, posts) { const postsArray = Array.from(posts.values()); return async function(ctx, next) { - responders.home(ctx, config, postsArray); + responder(ctx, config, { posts: postsArray }); }; } -function posts(config, posts) { +function posts(config, responder, posts) { const postsArray = Array.from(posts.values()); return async function(ctx, next) { - responders.list(ctx, config, null, "Posts", postsArray); + responder(ctx, config, { + listType: null, + listName: "Posts", + posts: postsArray + }); }; } @@ -38,23 +41,27 @@ function highlightTheme(config) { }; } -function post(config, posts) { +function post(config, responder, 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); + responder(ctx, config, { post }); }; } -function taxonGenerator(config, term, items) { +function taxonGenerator(config, responder, term, items) { return async function(ctx, next) { const value = ctx.params.value; ctx.assert(items.has(ctx.params.value), 404, `${term} ${value} not found`); const taxonItems = items.get(value); - responders.list(ctx, config, term, value, taxonItems); + responder(ctx, config, { + listType: term, + listName: value, + posts: taxonItems + }); }; } diff --git a/src/app.js b/src/app.js index b6d8beb..912dda2 100644 --- a/src/app.js +++ b/src/app.js @@ -6,6 +6,7 @@ const app = new Koa(); const helmet = require("koa-helmet"); const actions = require("./actions.js"); +const responders = require("./responders.js"); const config = require("./modules/config.js"); @@ -19,9 +20,13 @@ module.exports = async function() { router.url("post", basename) ); - router.get("home", "/", actions.home(config, Posts.posts)); + router.get("home", "/", actions.home(config, responders.home, Posts.posts)); - router.get("posts", "/post", actions.posts(config, Posts.posts)); + router.get( + "posts", + "/post", + actions.posts(config, responders.list, Posts.posts) + ); router.get( "highlight-theme", @@ -29,13 +34,17 @@ module.exports = async function() { actions.highlightTheme(config) ); - router.get("post", "/post/:filename", actions.post(config, Posts.posts)); + router.get( + "post", + "/post/:filename", + actions.post(config, responders.post, Posts.posts) + ); for (let [term, items] of Posts.taxonomies) { router.get( `taxon-${term}`, `/${term}/:value`, - actions.taxonGenerator(config, term, items) + actions.taxonGenerator(config, responders.list, term, items) ); } diff --git a/src/responders.js b/src/responders.js index f85198f..8f084a3 100644 --- a/src/responders.js +++ b/src/responders.js @@ -93,7 +93,7 @@ module.exports = { postIndentLevel, indentForTemplate, - home(ctx, config, posts) { + home(ctx, config, { posts }) { ctx.type = "html"; ctx.body = layout( @@ -105,7 +105,7 @@ module.exports = { ); }, - list(ctx, config, listType, listName, posts) { + list(ctx, config, { listType, listName, posts }) { ctx.type = "html"; ctx.body = layout( @@ -117,7 +117,7 @@ module.exports = { ); }, - post(ctx, config, post) { + post(ctx, config, { post }) { ctx.type = "html"; ctx.body = layout( -- cgit 1.4.1