mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-22 17:12:03 -07:00
35 lines
848 B
JavaScript
35 lines
848 B
JavaScript
import glob from 'glob'
|
|
import Router from 'koa-router'
|
|
|
|
exports = module.exports = function initModules (app) {
|
|
glob(`${__dirname}/*`, { ignore: '**/index.js' }, (err, matches) => {
|
|
if (err) { throw err }
|
|
|
|
matches.forEach((mod) => {
|
|
const router = require(`${mod}/router`)
|
|
|
|
const routes = router.default
|
|
const baseUrl = router.baseUrl
|
|
const instance = new Router({ prefix: baseUrl })
|
|
|
|
routes.forEach((config) => {
|
|
const {
|
|
method = '',
|
|
route = '',
|
|
handlers = []
|
|
} = config
|
|
|
|
const lastHandler = handlers.pop()
|
|
|
|
instance[method.toLowerCase()](route, ...handlers, async function(ctx) {
|
|
return await lastHandler(ctx)
|
|
})
|
|
|
|
app
|
|
.use(instance.routes())
|
|
.use(instance.allowedMethods())
|
|
})
|
|
})
|
|
})
|
|
}
|