mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
feat: 新增获取全部接口信息
This commit is contained in:
@@ -4,6 +4,14 @@ const Router = require("koa-router");
|
||||
|
||||
const router = new Router();
|
||||
|
||||
// 全部路由数据
|
||||
const allRouterInfo = {
|
||||
name: "全部接口",
|
||||
subtitle: "除了特殊接口外的全部接口列表",
|
||||
total: 0,
|
||||
data: [],
|
||||
};
|
||||
|
||||
// 根目录
|
||||
router.get("/", async (ctx) => {
|
||||
await ctx.render("index");
|
||||
@@ -17,10 +25,43 @@ fs.readdirSync(__dirname)
|
||||
const routerModule = require(routerPath);
|
||||
// 自动注册路由
|
||||
if (routerModule instanceof Router) {
|
||||
// 写入路由数据
|
||||
if (routerModule?.info) {
|
||||
allRouterInfo.total++;
|
||||
allRouterInfo.data.push({
|
||||
...routerModule.info,
|
||||
stack: routerModule.stack,
|
||||
});
|
||||
}
|
||||
// 引用路由
|
||||
router.use(routerModule.routes());
|
||||
}
|
||||
});
|
||||
|
||||
// 全部接口路由
|
||||
router.get("/all", async (ctx) => {
|
||||
console.log("获取全部接口路由");
|
||||
if (allRouterInfo.total > 0) {
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
message: "获取成功",
|
||||
...allRouterInfo,
|
||||
};
|
||||
} else if (allRouterInfo.total === 0) {
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
message: "暂无接口,请添加",
|
||||
...allRouterInfo,
|
||||
};
|
||||
} else {
|
||||
ctx.body = {
|
||||
code: 500,
|
||||
message: "获取失败",
|
||||
...allRouterInfo,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// 404 路由
|
||||
router.use(async (ctx) => {
|
||||
await ctx.render("404");
|
||||
|
||||
Reference in New Issue
Block a user