'use strict';

let assert = require('assert');
let util = require('util');
let path = require('path');
let glob = require('glob');
let router = require('koa-router')();

/**
 * Consts
 * @private
 */

const cwd = process.cwd();
const DEFAULT_PATH = {
  controllers: './controllers',
  filters: './filters'
};
const METHODS = require('methods');

/**
 * Method exports
 * @public
 */

exports.route = route;

process.on('unhandledRejection', (reason) => console.error(`unhandled rejection: ${reason}`));

/**
 * Create routers
 * @param  {Object}  a koa instance
 * @param  {Object}  object that specify the path of the route folder
 * @public
 */

function route (app, path = DEFAULT_PATH) {
  assert(util.isObject(path), `path should be an object but a ${typeof path}`);
  let controllersPath = path.join(cwd, path.controllers);
  let filtersPath = path.join(cwd, path.filters);

  new Promise((resolve, reject) => {
    glob(controllersPath + '/**/*.js', (err, files) => {
      if (err) reject(err);
      resolve(files);
    });
  })
  .then((files) => {
    files.forEach((path) => {
      let exported = require(`${cwd}/${path}`);
      let url = path.split('.')[0];
      METHODS.forEach((method) => {
        if (typeof exported[method] === 'function') router[method](url, exported[method]);
      });
    });

    app.use(router.routes());
    app.use(router.allowedMethods());
  })
  .catch((err) => {
    let msg = err.message;
    console.error(`uncaught error: ${msg}`);
  });
};

@@ -8,16 +8,48 @@
 rict';%0A%0A
+let %7B join %7D = require('path');%0A
 let asse
@@ -104,36 +104,8 @@
 ');%0A
-let path = require('path');%0A
 let 
@@ -785,37 +785,32 @@
 ntrollersPath = 
-path.
 join(cwd, path.c
@@ -842,21 +842,16 @@
 sPath = 
-path.
 join(cwd
@@ -874,52 +874,21 @@
 %0A%0A  
-new Promise((resolve, reject) =%3E %7B%0A    glob(
+glob.sync(%60$%7B
 cont
@@ -902,20 +902,17 @@
 Path
- + '
+%7D
 /**/*.js
 ', (
@@ -911,147 +911,148 @@
 *.js
-', (err, files) =%3E %7B%0A      if (err) reject(err);%0A      resolve(files);%0A    %7D);%0A  %7D)%0A  .then((files) =%3E %7B%0A    files.forEach((path) =%3E %7B%0A    
+%60).forEach((path) =%3E _addRouters(path));%0A%0A  app.use(router.routes());%0A  app.use(router.allowedMethods());%0A%7D;%0A%0Afunction _addRouters (path) %7B%0A
   le
@@ -1091,20 +1091,16 @@
 ath%7D%60);%0A
-    
   let ur
@@ -1125,18 +1125,212 @@
 )%5B0%5D;%0A  
-  
+let middlewares = %5B%5D;%0A  let outterFilters = %5B%5D;%0A%0A  if (util.isArray(exported.filters)) %7B%0A    exported.filters.forEach((filter) =%3E outFilters.push(require(%60$%7BfiltersPath%7D/$%7Bpath%7D%60)%5Bfilter%5D));%0A  %7D%0A%0A
   METHOD
@@ -1353,20 +1353,16 @@
 d) =%3E %7B%0A
-    
     if (
@@ -1405,230 +1405,363 @@
 n') 
-router%5Bmethod%5D(url, exported%5Bmethod%5D);%0A      %7D);%0A    %7D);%0A%0A    app.use(router.routes());%0A    app.use(router.allowedMethods());%0A  %7D)%0A  .catch((err) =%3E %7B%0A    let msg = err.message;%0A    console.error(%60uncaught error: $%7Bmsg%7D%60);
+%7B%0A      let innerFilters = %5B%5D;%0A      if (util.isArray(exported%5Bmethod%5D.filters)) %7B%0A        exported%5Bmethod%5D.filters.forEach(() =%3E innerFilters.push(require(%60$%7BfiltersPath%7D/$%7Bpath%7D%60)%5Bfilter%5D));%0A      %7D%0A      outterFilters.unshift(url);%0A      innerFilters.push(exported%5Bmethod%5D);%0A      router%5Bmethod%5D.apply(router, outterFilters.concat(innerFilters));%0A    %7D
 %0A  %7D

add filters