Skip to content

Commit 1406bd8

Browse files
authored
修复cloudflare上环境变量不生效的问题 (huangxd-#78)
* 修复cloudflare上环境变量不生效的问题 * 将源的初始化提到文件开头,避免重复创建多个对象 * 修改小版本号
1 parent b794f02 commit 1406bd8

File tree

4 files changed

+20
-33
lines changed

4 files changed

+20
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ LogVar 弹幕 API 服务器
223223
>
224224
<img src="https://i.mji.rip/2025/09/17/3a675876dabb92e4ce45c10d543ce66b.png" style="width:400px" />
225225

226-
> 如果每次访问都遇到404等问题,可能是edgeone pages修改了访问策略,每次接口请求都转发到了新的环境,没有缓存,导致获取不到对应的弹幕,推荐用vercel部署
226+
> 如果每次访问都遇到404等问题,可能是edgeone pages修改了访问策略,每次接口请求都转发到了新的环境,没有缓存,导致获取不到对应的弹幕,推荐用vercel/netlify部署
227227
>
228228
> 解决方法:请配置环境变量`UPSTASH_REDIS_REST_URL`和`UPSTASH_REDIS_REST_TOKEN`,开启upstash redis存储
229229

danmu_api/apis/dandan-api.js

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ import OtherSource from "../sources/other.js";
2424
// 兼容弹弹play接口
2525
// =====================
2626

27+
const kan360Source = new Kan360Source();
28+
const vodSource = new VodSource();
29+
const renrenSource = new RenrenSource();
30+
const hanjutvSource = new HanjutvSource();
31+
const bahamutSource = new BahamutSource();
32+
const tencentSource = new TencentSource();
33+
const youkuSource = new YoukuSource();
34+
const iqiyiSource = new IqiyiSource();
35+
const mangoSource = new MangoSource();
36+
const bilibiliSource = new BilibiliSource();
37+
const otherSource = new OtherSource();
38+
2739
function matchSeason(anime, queryTitle, season) {
2840
if (anime.animeTitle.includes(queryTitle)) {
2941
const title = anime.animeTitle.split("(")[0].trim();
@@ -51,17 +63,6 @@ function matchSeason(anime, queryTitle, season) {
5163

5264
// Extracted function for GET /api/v2/search/anime
5365
export async function searchAnime(url) {
54-
const kan360Source = new Kan360Source();
55-
const vodSource = new VodSource();
56-
const renrenSource = new RenrenSource();
57-
const hanjutvSource = new HanjutvSource();
58-
const bahamutSource = new BahamutSource();
59-
const tencentSource = new TencentSource();
60-
const youkuSource = new YoukuSource();
61-
const iqiyiSource = new IqiyiSource();
62-
const mangoSource = new MangoSource();
63-
const bilibiliSource = new BilibiliSource();
64-
6566
const queryTitle = url.searchParams.get("keyword");
6667
log("info", `Search anime with keyword: ${queryTitle}`);
6768

@@ -687,16 +688,6 @@ export async function getBangumi(path) {
687688

688689
// Extracted function for GET /api/v2/comment/:commentId
689690
export async function getComment(path, queryFormat) {
690-
const renrenSource = new RenrenSource();
691-
const hanjutvSource = new HanjutvSource();
692-
const bahamutSource = new BahamutSource();
693-
const tencentSource = new TencentSource();
694-
const iqiyiSource = new IqiyiSource();
695-
const mangoSource = new MangoSource();
696-
const bilibiliSource = new BilibiliSource();
697-
const youkuSource = new YoukuSource();
698-
const otherSource = new OtherSource();
699-
700691
const commentId = parseInt(path.split("/").pop());
701692
let url = findUrlById(commentId);
702693
let title = findTitleById(commentId);
@@ -769,13 +760,6 @@ export async function getComment(path, queryFormat) {
769760

770761
// Extracted function for GET /api/v2/comment?url=xxx
771762
export async function getCommentByUrl(videoUrl, queryFormat) {
772-
const tencentSource = new TencentSource();
773-
const iqiyiSource = new IqiyiSource();
774-
const mangoSource = new MangoSource();
775-
const bilibiliSource = new BilibiliSource();
776-
const youkuSource = new YoukuSource();
777-
const otherSource = new OtherSource();
778-
779763
try {
780764
// 验证URL参数
781765
if (!videoUrl || typeof videoUrl !== 'string') {

danmu_api/configs/envs.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* 提供获取和设置环境变量的函数,支持 Cloudflare Workers 和 Node.js
44
*/
55
export class Envs {
6+
static env;
7+
68
// 记录获取过的环境变量
79
static accessedEnvVars = new Map();
810

@@ -19,10 +21,10 @@ export class Envs {
1921
*/
2022
static get(key, defaultValue, type = 'string', encrypt = false) {
2123
let value;
22-
if (typeof env !== 'undefined' && env[key]) {
23-
value = env[key]; // Cloudflare Workers
24+
if (typeof this.env !== 'undefined' && this.env[key]) {
25+
value = this.env[key];
2426
} else if (typeof process !== 'undefined' && process.env?.[key]) {
25-
value = process.env[key]; // Node.js
27+
value = process.env[key];
2628
} else {
2729
value = defaultValue;
2830
}
@@ -176,6 +178,7 @@ export class Envs {
176178
* @returns {Object} 配置对象
177179
*/
178180
static load(env = {}, deployPlatform = 'node') {
181+
this.env = env;
179182
return {
180183
vodAllowedPlatforms: this.VOD_ALLOWED_PLATFORMS,
181184
allowedPlatforms: this.ALLOWED_PLATFORMS,

danmu_api/configs/globals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const Globals = {
1111
accessedEnvVars: {},
1212

1313
// 静态常量
14-
VERSION: '1.6.1',
14+
VERSION: '1.6.2',
1515
MAX_LOGS: 500, // 日志存储,最多保存 500 行
1616
MAX_ANIMES: 100,
1717
MAX_LAST_SELECT_MAP: 100,

0 commit comments

Comments
 (0)