9 changed files with 68 additions and 67 deletions
@ -1,6 +1,8 @@ |
|||||
# DATABASE_URL=postgresql://postgres:xxxxxx@localhost:6666/postgres |
# DATABASE_URL=postgresql://postgres:xxxxxx@localhost:6666/postgres |
||||
DATABASE_URL=file:./db.sqlite |
DATABASE_URL=file:./db.sqlite |
||||
NITRO_PORT=3399 |
NITRO_PORT=3399 |
||||
|
# 站点对外根 URL(含协议与域名,可带端口)。用于:① 媒体库复制绝对链接 ② 文章/资料里绝对地址图片是否计为本站 /public/assets/ 引用。生产环境务必设置,与浏览器访问地址一致。 |
||||
|
# NUXT_PUBLIC_SITE_URL=https://example.com |
||||
# Optional: first admin for an empty instance. Creates an admin only when no user has role=admin yet (same username/password rules as registration). |
# Optional: first admin for an empty instance. Creates an admin only when no user has role=admin yet (same username/password rules as registration). |
||||
BOOTSTRAP_ADMIN_USERNAME= |
BOOTSTRAP_ADMIN_USERNAME= |
||||
BOOTSTRAP_ADMIN_PASSWORD= |
BOOTSTRAP_ADMIN_PASSWORD= |
||||
@ -0,0 +1,26 @@ |
|||||
|
import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
||||
|
import { allowedOriginsFromSitePublicEnv, getSitePublicUrlFromEnv } from "./site-public"; |
||||
|
|
||||
|
describe("site-public env", () => { |
||||
|
const prev = process.env.NUXT_PUBLIC_SITE_URL; |
||||
|
|
||||
|
afterEach(() => { |
||||
|
if (prev === undefined) { |
||||
|
delete process.env.NUXT_PUBLIC_SITE_URL; |
||||
|
} else { |
||||
|
process.env.NUXT_PUBLIC_SITE_URL = prev; |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
test("empty when env unset", () => { |
||||
|
delete process.env.NUXT_PUBLIC_SITE_URL; |
||||
|
expect(getSitePublicUrlFromEnv()).toBe(""); |
||||
|
expect(allowedOriginsFromSitePublicEnv()).toEqual([]); |
||||
|
}); |
||||
|
|
||||
|
test("parses https origin", () => { |
||||
|
process.env.NUXT_PUBLIC_SITE_URL = "https://blog.example.com/posts"; |
||||
|
expect(getSitePublicUrlFromEnv()).toBe("https://blog.example.com/posts"); |
||||
|
expect(allowedOriginsFromSitePublicEnv()).toEqual(["https://blog.example.com"]); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,24 @@ |
|||||
|
/** |
||||
|
* 与 Nuxt `runtimeConfig.public.siteUrl` 同源:环境变量 **`NUXT_PUBLIC_SITE_URL`** |
||||
|
*(构建/启动时注入,见 `.env.example`)。 |
||||
|
*/ |
||||
|
export function getSitePublicUrlFromEnv(): string { |
||||
|
return (process.env.NUXT_PUBLIC_SITE_URL ?? "").trim(); |
||||
|
} |
||||
|
|
||||
|
/** 用于 `mergePostMediaUrls` 的 `allowedAssetOrigins`;无效或空则返回空数组。 */ |
||||
|
export function allowedOriginsFromSitePublicEnv(): string[] { |
||||
|
const raw = getSitePublicUrlFromEnv(); |
||||
|
if (!raw) { |
||||
|
return []; |
||||
|
} |
||||
|
try { |
||||
|
const u = new URL(raw); |
||||
|
if (u.protocol !== "http:" && u.protocol !== "https:") { |
||||
|
return []; |
||||
|
} |
||||
|
return [u.origin]; |
||||
|
} catch { |
||||
|
return []; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue