Skip to content

Commit

Permalink
✨ feat: 支持泛域名配置
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Nov 5, 2024
1 parent 293c4d6 commit d0dfba2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
PORT = 6688

# 允许的域名
ALLOWED_DOMAIN = "*"
ALLOWED_DOMAIN = "https://api-hot.imsyy.top"

# 允许的主域名,请填写为 imsyy.top
## 若填写该项,将忽略 ALLOWED_DOMAIN
ALLOWED_HOST = "imsyy.top"

# ROBOT
DISALLOW_ROBOT = true
Expand Down
6 changes: 5 additions & 1 deletion src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ app.use(
"*",
cors({
// 可写为数组
origin: config.ALLOWED_DOMAIN,
origin: (origin) => {
// 是否指定域名
const isSame = origin.endsWith(config.ALLOWED_HOST);
return isSame ? origin : config.ALLOWED_DOMAIN;
},
allowMethods: ["POST", "GET", "OPTIONS"],
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
credentials: true,
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Config = {
CACHE_TTL: number;
REQUEST_TIMEOUT: number;
ALLOWED_DOMAIN: string;
ALLOWED_HOST: string;
USE_LOG_FILE: boolean;
RSS_MODE: boolean;
};
Expand Down Expand Up @@ -45,6 +46,7 @@ export const config: Config = {
CACHE_TTL: getNumericEnvVariable("CACHE_TTL", 3600),
REQUEST_TIMEOUT: getNumericEnvVariable("CACHE_TTL", 6000),
ALLOWED_DOMAIN: getEnvVariable("ALLOWED_DOMAIN") || "*",
ALLOWED_HOST: getEnvVariable("ALLOWED_HOST") || "imsyy.top",
USE_LOG_FILE: getBooleanEnvVariable("USE_LOG_FILE", true),
RSS_MODE: getBooleanEnvVariable("RSS_MODE", false),
};

0 comments on commit d0dfba2

Please sign in to comment.