about summary refs log tree commit diff stats
path: root/src/actions.js
blob: 11acff5c0314e50c04da199eb1f4314740e78626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";

const send = require("koa-send");
const responders = require("./responders");

function home(config, posts) {
  const postsArray = Array.from(posts.values());
  return async function(ctx, next) {
    responders.home(ctx, config, postsArray);
  };
}

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, `${term} ${value} not found`);

    const taxonItems = 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
};