mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
137 lines
3.6 KiB
TypeScript
137 lines
3.6 KiB
TypeScript
import { fileURLToPath } from "url";
|
|
import { config } from "./config.js";
|
|
import { Hono } from "hono";
|
|
import getRSS from "./utils/getRSS.js";
|
|
import path from "path";
|
|
import fs from "fs";
|
|
|
|
const app = new Hono();
|
|
|
|
// 模拟 __dirname
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
// 路由数据
|
|
let allRoutePath: Array<string> = [];
|
|
const routersDirName: string = "routes";
|
|
|
|
// 排除路由
|
|
const excludeRoutes: Array<string> = [];
|
|
|
|
// 建立完整目录路径
|
|
const routersDirPath = path.join(__dirname, routersDirName);
|
|
|
|
// 递归查找函数
|
|
const findTsFiles = (dirPath: string, allFiles: string[] = [], basePath: string = ""): string[] => {
|
|
// 读取目录下的所有文件和文件夹
|
|
const items: Array<string> = fs.readdirSync(dirPath);
|
|
// 遍历每个文件或文件夹
|
|
items.forEach((item) => {
|
|
const fullPath: string = path.join(dirPath, item);
|
|
const relativePath: string = basePath ? path.posix.join(basePath, item) : item;
|
|
const stat: fs.Stats = fs.statSync(fullPath);
|
|
if (stat.isDirectory()) {
|
|
// 如果是文件夹,递归查找
|
|
findTsFiles(fullPath, allFiles, relativePath);
|
|
} else if (
|
|
stat.isFile() &&
|
|
(item.endsWith(".ts") || item.endsWith(".js")) &&
|
|
!item.endsWith(".d.ts")
|
|
) {
|
|
// 符合条件
|
|
allFiles.push(relativePath.replace(/\.(ts|js)$/, ""));
|
|
}
|
|
});
|
|
return allFiles;
|
|
};
|
|
|
|
// 获取全部路由
|
|
if (fs.existsSync(routersDirPath) && fs.statSync(routersDirPath).isDirectory()) {
|
|
allRoutePath = findTsFiles(routersDirPath);
|
|
} else {
|
|
console.error(`目录 ${routersDirPath} 不存在或不是目录`);
|
|
}
|
|
|
|
// 注册全部路由
|
|
for (let index = 0; index < allRoutePath.length; index++) {
|
|
const router = allRoutePath[index];
|
|
// 是否处于排除名单
|
|
if (excludeRoutes.includes(router)) {
|
|
continue;
|
|
}
|
|
const listApp = app.basePath(`/${router}`);
|
|
// 返回榜单
|
|
listApp.get("/", async (c) => {
|
|
// 是否采用缓存
|
|
const noCache = c.req.query("cache") === "false";
|
|
// 限制显示条目
|
|
const limit = c.req.query("limit");
|
|
// 是否输出 RSS
|
|
const rssEnabled = c.req.query("rss") === "true";
|
|
// 获取路由路径
|
|
const { handleRoute } = await import(`./routes/${router}.js`);
|
|
const listData = await handleRoute(c, noCache);
|
|
// 是否限制条目
|
|
if (limit && listData?.data?.length > parseInt(limit)) {
|
|
listData.total = parseInt(limit);
|
|
listData.data = listData.data.slice(0, parseInt(limit));
|
|
}
|
|
// 是否输出 RSS
|
|
if (rssEnabled || config.RSS_MODE) {
|
|
const rss = getRSS(listData);
|
|
if (typeof rss === "string") {
|
|
c.header("Content-Type", "application/xml; charset=utf-8");
|
|
return c.body(rss);
|
|
} else {
|
|
return c.json(
|
|
{
|
|
code: 500,
|
|
message: "RSS 生成失败",
|
|
},
|
|
500,
|
|
);
|
|
}
|
|
}
|
|
return c.json({
|
|
code: 200,
|
|
...listData,
|
|
});
|
|
});
|
|
// 请求方式错误
|
|
listApp.all("*", (c) =>
|
|
c.json(
|
|
{
|
|
code: 405,
|
|
message: "Method Not Allowed",
|
|
},
|
|
405,
|
|
),
|
|
);
|
|
}
|
|
|
|
// 获取全部路由
|
|
app.get("/all", (c) =>
|
|
c.json(
|
|
{
|
|
code: 200,
|
|
count: allRoutePath.length,
|
|
routes: allRoutePath.map((path) => {
|
|
// 是否处于排除名单
|
|
if (excludeRoutes.includes(path)) {
|
|
return {
|
|
name: path,
|
|
path: undefined,
|
|
message: "该接口暂时下线",
|
|
};
|
|
}
|
|
return {
|
|
name: path,
|
|
path: `/${path}`,
|
|
};
|
|
}),
|
|
},
|
|
200,
|
|
),
|
|
);
|
|
|
|
export default app;
|