<commit_before>import express from 'express';
import { logger } from '../services';

const router = new express.Router();
export default router;

function handleError(res) {
  return (error) => {
    // TODO implement more safe error handling
    logger.warn(error);
    res.status(error.code).send(error.message);
  };
}

const routes = [
  'home',
  'login',
  'login/callback',
  'token',
  'report/svg', // report/svg must place before repo/report.
  'repo',
  'repo/report',
];

routes.forEach(name => {
  const module = require(`./${name}`);
  const route = router.route(module.route);

  logger.debug(`[routes] route [${module.route}] from file [${name}] provides functions [${Object.keys(module)}].`);
  route.all((req, res, next) => {
    logger.debug(`[routes] request [${req.method}] path [${req.url}], hit route [${module.route}] from file [${name}].`);
    next();
  });

  if (module.post) {
    const middlewares = module.post.middlewares || [];
    route.post(
      ...middlewares,
      (req, res) =>
        module.post(req)
          .then(result =>
            // TODO leverage `res.format` to better content-negotiation
            req.accepts('html') === 'html'
            ? res.redirect(req.get('Referer'))
            : res.send(result))
          .catch(handleError(res)));
  }

  if (module.redirect) {
    route.get((req, res) =>
      module.redirect(req)
        .then(url => res.redirect(url)
        .catch(handleError(res))));
  } else if (module.view && module.model) {
    route.get((req, res) =>
      module.model(req)
        .then(model =>
          res.type(module.type || 'html').render(module.view, model))
        .catch(handleError(res)));
  } else {
    route.get((req, res) =>
      res.status(500).end());
  }
});<commit_msg>Refine the route index logic.<commit_after>import express from 'express';
import { logger } from '../services';

function handleError(res) {
  return (error) => {
    // TODO implement more safe error handling
    logger.warn(error);
    res.status(error.code).send(error.message);
  };
}

function register(router, route, name, method) {
  return (middlewares = [], fn) => {
    router[method](route, ...middlewares, (req, res) => {
      logger.debug(`[routes] request [${method}] URL [${req.url}], hit route [${route}] from file [${name}].`);
      fn(req, res).catch(handleError(res));
    });
  };
}

function createRouter() {
  const router = new express.Router();

  const routes = [
    'home',
    'login',
    'login/callback',
    'token',
    'report/svg', // report/svg must place before repo/report.
    'repo',
    'repo/report',
  ];

  routes.forEach(name => {
    const module = require(`./${name}`);
    const get = register(router, module.route, name, 'get');
    const post = register(router, module.route, name, 'post');

    logger.debug(`[routes] route [${module.route}] from file [${name}] provides functions [${Object.keys(module)}].`);

    if (module.post) {
      post(module.post.middlewares, (req, res) =>
        module.post(req).then(result =>
          // TODO leverage `res.format` to better content-negotiation
          req.accepts('html') === 'html'
          ? res.redirect(req.get('Referer'))
          : res.send(result)));
    }

    if (module.redirect) {
      get(module.redirect.middlewares, (req, res) =>
        module.redirect(req).then(url => res.redirect(url)));
    } else if (module.view && module.model) {
      get(module.model.middlewares, (req, res) =>
        module.model(req).then(model =>
          res.type(module.type || 'html').render(module.view, model)));
    }
  });

  return router;
}

export default createRouter();
{'fraction_non_alphanumeric': 0.18780754510661563, 'fraction_numerical': 0.0008201202843083652, 'mean_word_length': 2.9217577706323685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '71160', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1160, 'n_words': 251}