Files
RSSHub/lib/middleware/load-on-demand.js
NeverBehave 0792f7ba25 feat(core): first attempt to init script standard (#8224)
- lazy load
- rate limit per path
- init .debug.json support
- docs
- maintainer
- radar
2021-09-22 05:41:00 -07:00

37 lines
1.1 KiB
JavaScript

const mount = require('koa-mount');
const Router = require('@koa/router');
const routes = require('../v2router');
const loadedRoutes = new Set();
module.exports = function (app) {
return async function (ctx, next) {
const p = ctx.request.path.split('/').filter(Boolean);
let modName = null;
let mounted = false;
if (p.length > 0) {
modName = p[0];
if (!loadedRoutes.has(modName)) {
const mod = routes[modName];
// Mount module
if (mod) {
mounted = true;
loadedRoutes.add(modName);
const router = new Router();
mod(router);
app.use(mount(`/${modName}`, router.routes())).use(router.allowedMethods());
}
} else {
mounted = true;
}
}
await next();
// We should only add it when koa router matched
if (mounted && ctx._matchedRoute) {
ctx._matchedRoute = `/${modName}${ctx._matchedRoute}`;
}
};
};