dailyhot-api
Version:
An Api on Today's Hot list
103 lines (102 loc) • 3.63 kB
JavaScript
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 = [];
const routersDirName = "routes";
// 排除路由
const excludeRoutes = [];
// 建立完整目录路径
const routersDirPath = path.join(__dirname, routersDirName);
// 递归查找函数
const findTsFiles = (dirPath, allFiles = [], basePath = "") => {
// 读取目录下的所有文件和文件夹
const items = fs.readdirSync(dirPath);
// 遍历每个文件或文件夹
items.forEach((item) => {
const fullPath = path.join(dirPath, item);
const relativePath = basePath ? path.posix.join(basePath, item) : item;
const stat = 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(`📂 The directory ${routersDirPath} does not exist or is not a directory`);
}
// 注册全部路由
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 generation failed" }, 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: "This interface is temporarily offline",
};
}
return { name: path, path: `/${path}` };
}),
}, 200));
export default app;