feat: 隔离公开工作空间缓存与品牌解析
This commit is contained in:
1 parent
ff5dc4e3ba
commit
aeb1605a2a
2 files changed
+14
-2
No files matched your search
+12
-2
@@ -1,6 +1,7 @@
|
|||||||
import "reflect-metadata";
|
import "reflect-metadata";
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
|
NotFoundException,
|
||||||
UnauthorizedException,
|
UnauthorizedException,
|
||||||
ForbiddenException,
|
ForbiddenException,
|
||||||
Body,
|
Body,
|
||||||
@@ -147,13 +148,20 @@ export class PublicTemplateController {
|
|||||||
@Controller("public/workspace")
|
@Controller("public/workspace")
|
||||||
export class PublicWorkspaceController {
|
export class PublicWorkspaceController {
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
private readonly cache = new Map<string, { expiresAt: number; value: unknown }>();
|
||||||
@Get()
|
@Get()
|
||||||
async get(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
|
async get(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
|
||||||
const configuredId = process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
|
const configuredId = process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
|
||||||
const selected = brandSlug
|
const selected = brandSlug
|
||||||
? await this.prisma.brandProfile.findUnique({ where: { slug: brandSlug }, select: { id: true } })
|
? await this.prisma.brandProfile.findUnique({
|
||||||
|
where: { slug: brandSlug },
|
||||||
|
select: { id: true },
|
||||||
|
})
|
||||||
: null;
|
: null;
|
||||||
|
if (brandSlug && !selected) throw new NotFoundException("品牌不存在");
|
||||||
const brandId = selected?.id ?? configuredId;
|
const brandId = selected?.id ?? configuredId;
|
||||||
|
const cached = this.cache.get(brandId);
|
||||||
|
if (cached && cached.expiresAt > Date.now()) return cached.value;
|
||||||
const [
|
const [
|
||||||
brand,
|
brand,
|
||||||
modules,
|
modules,
|
||||||
@@ -307,7 +315,7 @@ export class PublicWorkspaceController {
|
|||||||
},
|
},
|
||||||
...(visualTemplate?.layout ? { layout: visualTemplate.layout } : {}),
|
...(visualTemplate?.layout ? { layout: visualTemplate.layout } : {}),
|
||||||
};
|
};
|
||||||
return {
|
const response = {
|
||||||
...runtimeWorkspace,
|
...runtimeWorkspace,
|
||||||
contractVersion: "1.0",
|
contractVersion: "1.0",
|
||||||
brand,
|
brand,
|
||||||
@@ -335,6 +343,8 @@ export class PublicWorkspaceController {
|
|||||||
trustContent: trustContents,
|
trustContent: trustContents,
|
||||||
inquiryForm,
|
inquiryForm,
|
||||||
};
|
};
|
||||||
|
this.cache.set(brandId, { expiresAt: Date.now() + 30_000, value: response });
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
private jsonObject(value: unknown): Record<string, unknown> {
|
private jsonObject(value: unknown): Record<string, unknown> {
|
||||||
return value && typeof value === "object" && !Array.isArray(value)
|
return value && typeof value === "object" && !Array.isArray(value)
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ test("health and public content are available without authentication", async ()
|
|||||||
const bySlug = await request("/public/workspace?brandSlug=default-brand");
|
const bySlug = await request("/public/workspace?brandSlug=default-brand");
|
||||||
assert.equal(bySlug.statusCode, 200);
|
assert.equal(bySlug.statusCode, 200);
|
||||||
assert.equal(JSON.parse(bySlug.body).brand.id, brandId);
|
assert.equal(JSON.parse(bySlug.body).brand.id, brandId);
|
||||||
|
const missingSlug = await request("/public/workspace?brandSlug=missing-brand");
|
||||||
|
assert.equal(missingSlug.statusCode, 404);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("public workspace uses brand-scoped capability installations and preserves status", async () => {
|
test("public workspace uses brand-scoped capability installations and preserves status", async () => {
|
||||||
|
|||||||
Reference in new issue
Block a user