From 1406bd8148e4e302b5b3fe32c33ebcb4091a74d5 Mon Sep 17 00:00:00 2001 From: huangxd- <649924757@qq.com> Date: Mon, 27 Oct 2025 22:42:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dcloudflare=E4=B8=8A=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E4=B8=8D=E7=94=9F=E6=95=88=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#78)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复cloudflare上环境变量不生效的问题 * 将源的初始化提到文件开头,避免重复创建多个对象 * 修改小版本号 --- README.md | 2 +- danmu_api/apis/dandan-api.js | 40 +++++++++++------------------------- danmu_api/configs/envs.js | 9 +++++--- danmu_api/configs/globals.js | 2 +- 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 1f8ca79..559eda6 100644 --- a/README.md +++ b/README.md @@ -223,7 +223,7 @@ LogVar 弹幕 API 服务器 > -> 如果每次访问都遇到404等问题,可能是edgeone pages修改了访问策略,每次接口请求都转发到了新的环境,没有缓存,导致获取不到对应的弹幕,推荐用vercel部署。 +> 如果每次访问都遇到404等问题,可能是edgeone pages修改了访问策略,每次接口请求都转发到了新的环境,没有缓存,导致获取不到对应的弹幕,推荐用vercel/netlify部署。 > > 解决方法:请配置环境变量`UPSTASH_REDIS_REST_URL`和`UPSTASH_REDIS_REST_TOKEN`,开启upstash redis存储 diff --git a/danmu_api/apis/dandan-api.js b/danmu_api/apis/dandan-api.js index 30336e9..034ccd7 100644 --- a/danmu_api/apis/dandan-api.js +++ b/danmu_api/apis/dandan-api.js @@ -24,6 +24,18 @@ import OtherSource from "../sources/other.js"; // 兼容弹弹play接口 // ===================== +const kan360Source = new Kan360Source(); +const vodSource = new VodSource(); +const renrenSource = new RenrenSource(); +const hanjutvSource = new HanjutvSource(); +const bahamutSource = new BahamutSource(); +const tencentSource = new TencentSource(); +const youkuSource = new YoukuSource(); +const iqiyiSource = new IqiyiSource(); +const mangoSource = new MangoSource(); +const bilibiliSource = new BilibiliSource(); +const otherSource = new OtherSource(); + function matchSeason(anime, queryTitle, season) { if (anime.animeTitle.includes(queryTitle)) { const title = anime.animeTitle.split("(")[0].trim(); @@ -51,17 +63,6 @@ function matchSeason(anime, queryTitle, season) { // Extracted function for GET /api/v2/search/anime export async function searchAnime(url) { - const kan360Source = new Kan360Source(); - const vodSource = new VodSource(); - const renrenSource = new RenrenSource(); - const hanjutvSource = new HanjutvSource(); - const bahamutSource = new BahamutSource(); - const tencentSource = new TencentSource(); - const youkuSource = new YoukuSource(); - const iqiyiSource = new IqiyiSource(); - const mangoSource = new MangoSource(); - const bilibiliSource = new BilibiliSource(); - const queryTitle = url.searchParams.get("keyword"); log("info", `Search anime with keyword: ${queryTitle}`); @@ -687,16 +688,6 @@ export async function getBangumi(path) { // Extracted function for GET /api/v2/comment/:commentId export async function getComment(path, queryFormat) { - const renrenSource = new RenrenSource(); - const hanjutvSource = new HanjutvSource(); - const bahamutSource = new BahamutSource(); - const tencentSource = new TencentSource(); - const iqiyiSource = new IqiyiSource(); - const mangoSource = new MangoSource(); - const bilibiliSource = new BilibiliSource(); - const youkuSource = new YoukuSource(); - const otherSource = new OtherSource(); - const commentId = parseInt(path.split("/").pop()); let url = findUrlById(commentId); let title = findTitleById(commentId); @@ -769,13 +760,6 @@ export async function getComment(path, queryFormat) { // Extracted function for GET /api/v2/comment?url=xxx export async function getCommentByUrl(videoUrl, queryFormat) { - const tencentSource = new TencentSource(); - const iqiyiSource = new IqiyiSource(); - const mangoSource = new MangoSource(); - const bilibiliSource = new BilibiliSource(); - const youkuSource = new YoukuSource(); - const otherSource = new OtherSource(); - try { // 验证URL参数 if (!videoUrl || typeof videoUrl !== 'string') { diff --git a/danmu_api/configs/envs.js b/danmu_api/configs/envs.js index ce5e7d2..6fd1acc 100644 --- a/danmu_api/configs/envs.js +++ b/danmu_api/configs/envs.js @@ -3,6 +3,8 @@ * 提供获取和设置环境变量的函数,支持 Cloudflare Workers 和 Node.js */ export class Envs { + static env; + // 记录获取过的环境变量 static accessedEnvVars = new Map(); @@ -19,10 +21,10 @@ export class Envs { */ static get(key, defaultValue, type = 'string', encrypt = false) { let value; - if (typeof env !== 'undefined' && env[key]) { - value = env[key]; // Cloudflare Workers + if (typeof this.env !== 'undefined' && this.env[key]) { + value = this.env[key]; } else if (typeof process !== 'undefined' && process.env?.[key]) { - value = process.env[key]; // Node.js + value = process.env[key]; } else { value = defaultValue; } @@ -176,6 +178,7 @@ export class Envs { * @returns {Object} 配置对象 */ static load(env = {}, deployPlatform = 'node') { + this.env = env; return { vodAllowedPlatforms: this.VOD_ALLOWED_PLATFORMS, allowedPlatforms: this.ALLOWED_PLATFORMS, diff --git a/danmu_api/configs/globals.js b/danmu_api/configs/globals.js index b601602..55a4f12 100644 --- a/danmu_api/configs/globals.js +++ b/danmu_api/configs/globals.js @@ -11,7 +11,7 @@ export const Globals = { accessedEnvVars: {}, // 静态常量 - VERSION: '1.6.1', + VERSION: '1.6.2', MAX_LOGS: 500, // 日志存储,最多保存 500 行 MAX_ANIMES: 100, MAX_LAST_SELECT_MAP: 100,