From 14bc5a1dce86f33064f934400f6bcb09a06df025 Mon Sep 17 00:00:00 2001 From: imsyy Date: Fri, 7 Jun 2024 16:03:22 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=94=AF=E6=8C=81=20Vercel?= =?UTF-8?q?=20=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 9 +++++--- README.md | 16 +++++++-------- package.json | 7 ++++--- pnpm-lock.yaml | 12 +++++++++++ public/ico/powered-by-vercel.svg | 6 ------ src/index.ts | 4 +++- src/registry.ts | 35 ++++++++++++++++++++++++++++---- src/utils/getToken/weread.ts | 3 ++- src/views/Home.tsx | 14 +++++++++---- 9 files changed, 76 insertions(+), 30 deletions(-) delete mode 100644 public/ico/powered-by-vercel.svg diff --git a/Dockerfile b/Dockerfile index 71300e1..399739b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,14 @@ FROM node:20-alpine AS base +ENV NODE_ENV=docker + # 安装 Puppeteer 所需的依赖库 -RUN apk add --no-cache chromium nss freetype harfbuzz ca-certificates libc6-compat +RUN apk add libc6-compat +# RUN apk add chromium nss freetype harfbuzz ca-certificates # 配置 Chromium -ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true -ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser +# ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true +# ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser FROM base AS builder diff --git a/README.md b/README.md index bae4b8f..5f0a579 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,8 @@ | 稀土掘金 | 热榜 | juejin | 🟢 | | 腾讯新闻 | 热点榜 | qq-news | 🟢 | | 网易新闻 | 热点榜 | netease-news | 🟢 | -| 吾爱破解 | 榜单 | 52pojie | 🟢 | -| 全球主机交流 | 榜单 | hostloc | 🟢 | +| 吾爱破解 | 榜单 | 52pojie | ❌ | +| 全球主机交流 | 榜单 | hostloc | ❌ | | 虎嗅 | 24小时 | huxiu | 🟢 | | 爱范儿 | 快讯 | ifanr | 🟢 | | 英雄联盟 | 更新公告 | lol | 🟢 | @@ -148,6 +148,12 @@ npm run start 成功启动后程序会在控制台输出可访问的地址 +### Vercel 部署 + +本项目支持通过 `Vercel` 进行一键部署,点击下方按钮或前往 [项目仓库](https://github.com/imsyy/DailyHotApi-Vercel) 进行手动部署 + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/imsyys-projects/clone?repository-url=https%3A%2F%2Fgithub.com%2Fimsyy%2FDailyHotApi-Vercel) + ### Railway 部署 本项目支持使用 [Railway](https://railway.app/) 一键部署,请先将本项目 fork 到您的仓库中,即可使用一键部署。 @@ -156,12 +162,6 @@ npm run start 本项目支持使用 [Zeabur](https://zeabur.com/) 一键部署,请先将本项目 fork 到您的仓库中,即可使用一键部署。 -### Vercel 部署 - -> 🚧 Vercel 部署支持正在修复中 - -若您目前仅能通过 `Vercel` 进行部署,那么请暂时不要使用最新版本 - ## ⚠️ 须知 - 本项目为了避免频繁请求官方数据,默认对数据做了缓存处理,默认为 `60` 分钟,如需更改,请自行修改配置 diff --git a/package.json b/package.json index 764f576..9688b93 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dailyhot-api", - "version": "2.0.0-rc.5", + "version": "2.0.0", "description": "An Api on Today's Hot list", "keywords": [ "API", @@ -31,8 +31,8 @@ "scripts": { "format": "prettier --write .", "lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts,.vue --fix", - "dev": "tsx watch --no-cache src/index.ts", - "dev:cache": "tsx watch src/index.ts", + "dev": "cross-env NODE_ENV=development tsx watch --no-cache src/index.ts", + "dev:cache": "cross-env NODE_ENV=development tsx watch src/index.ts", "build": "tsc --project tsconfig.json", "start": "tsx src/index.ts" }, @@ -56,6 +56,7 @@ "@types/node": "^20.14.1", "@typescript-eslint/eslint-plugin": "^7.12.0", "@typescript-eslint/parser": "^7.12.0", + "cross-env": "^7.0.3", "eslint": "^8.57.0", "prettier": "^3.3.0", "tsx": "^3.14.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 982b4bc..02a4849 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -57,6 +57,9 @@ importers: '@typescript-eslint/parser': specifier: ^7.12.0 version: 7.12.0(eslint@8.57.0)(typescript@5.4.5) + cross-env: + specifier: ^7.0.3 + version: 7.0.3 eslint: specifier: ^8.57.0 version: 8.57.0 @@ -518,6 +521,11 @@ packages: typescript: optional: true + cross-env@7.0.3: + resolution: {integrity: sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==} + engines: {node: '>=10.14', npm: '>=6', yarn: '>=1'} + hasBin: true + cross-spawn@7.0.3: resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==} engines: {node: '>= 8'} @@ -1808,6 +1816,10 @@ snapshots: optionalDependencies: typescript: 5.4.5 + cross-env@7.0.3: + dependencies: + cross-spawn: 7.0.3 + cross-spawn@7.0.3: dependencies: path-key: 3.1.1 diff --git a/public/ico/powered-by-vercel.svg b/public/ico/powered-by-vercel.svg deleted file mode 100644 index 8778286..0000000 --- a/public/ico/powered-by-vercel.svg +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/src/index.ts b/src/index.ts index e58a9e9..e7e44da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,8 @@ const serveHotApi = (port: number = config.PORT) => { } }; -serveHotApi(); +if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "docker") { + serveHotApi(config.PORT); +} export default serveHotApi; diff --git a/src/registry.ts b/src/registry.ts index ee87f29..996396d 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -14,6 +14,9 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); let allRoutePath: Array = []; const routersDirName: string = "routes"; +// 排除路由 +const excludeRoutes: Array = ["52pojie", "hostloc"]; + // 建立完整目录路径 const routersDirPath = path.join(__dirname, routersDirName); @@ -47,6 +50,10 @@ if (fs.existsSync(routersDirPath) && fs.statSync(routersDirPath).isDirectory()) // 注册全部路由 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) => { @@ -85,6 +92,16 @@ for (let index = 0; index < allRoutePath.length; index++) { ...listData, }); }); + // 请求方式错误 + listApp.all("*", (c) => + c.json( + { + code: 405, + message: "Method Not Allowed", + }, + 405, + ), + ); } // 获取全部路由 @@ -93,10 +110,20 @@ app.get("/all", (c) => { code: 200, count: allRoutePath.length, - routes: allRoutePath.map((path) => ({ - name: path, - path: `/${path}`, - })), + routes: allRoutePath.map((path) => { + // 是否处于排除名单 + if (excludeRoutes.includes(path)) { + return { + name: path, + path: null, + message: "该接口暂时下线", + }; + } + return { + name: path, + path: `/${path}`, + }; + }), }, 200, ), diff --git a/src/utils/getToken/weread.ts b/src/utils/getToken/weread.ts index 54c5de8..7a8a28d 100644 --- a/src/utils/getToken/weread.ts +++ b/src/utils/getToken/weread.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import crypto from "crypto"; /** @@ -13,7 +14,7 @@ const getWereadID = (bookId: string) => { // 取哈希结果的前三个字符作为初始值 let strSub = str.substring(0, 3); // 判断书籍 ID 的类型并进行转换 - let fa; + let fa: (string | any[])[]; if (/^\d*$/.test(bookId)) { // 如果书籍 ID 只包含数字,则将其拆分成长度为 9 的子字符串,并转换为十六进制表示 const chunks = []; diff --git a/src/views/Home.tsx b/src/views/Home.tsx index 9c89f46..2ce62d9 100644 --- a/src/views/Home.tsx +++ b/src/views/Home.tsx @@ -7,14 +7,17 @@ const Home: FC = () => {
- logo + logo

DailyHot API

服务已正常运行
-
{html` `}