Skip to content

Commit 1dff537

Browse files
authored
feat: tanent caching (#437)
1 parent 9c075d4 commit 1dff537

File tree

5 files changed

+30
-48
lines changed

5 files changed

+30
-48
lines changed

src/domain/definitions.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
import type { Validation } from '^/integrations/validation';
33

4-
export const TENANT_BY_ORIGIN_PATH = 'domain/tenant/getByOriginConverted';
5-
64
export const SortOrders = {
75
POPULAR: 'popular',
86
RECENT: 'recent'
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
import identityProvider from '^/integrations/authentication';
33

4-
import { TENANT_BY_ORIGIN_PATH } from '^/domain/definitions';
5-
64
import AuthenticationMiddleware from './middlewares/AuthenticationMiddleware';
75

86
const authProcedures = {
@@ -13,6 +11,8 @@ const authProcedures = {
1311

1412
const redirectPath = process.env.AUTHENTICATION_CLIENT_PATH || 'undefined';
1513

16-
const whiteList: string[] = [TENANT_BY_ORIGIN_PATH];
14+
const whiteList: string[] = [
15+
'domain/tenant/getByOriginConverted'
16+
];
1717

1818
export default new AuthenticationMiddleware(identityProvider, authProcedures, redirectPath, whiteList);
Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,54 @@
11

2-
import type { Middleware, NextHandler, Request, Response } from 'jitar';
2+
import type { Middleware, NextHandler, Request } from 'jitar';
3+
import { Response } from 'jitar';
34

5+
import type { Tenant } from '^/domain/tenant';
6+
import getByOrigin from '^/domain/tenant/getByOriginConverted';
7+
8+
const GEY_BY_ORIGIN_FQN = 'domain/tenant/getByOriginConverted';
49
const TENANT_PARAMETER = '*tenant';
510

611
export default class TenantMiddleware implements Middleware
712
{
8-
readonly #cache = new Map<string, Response>();
9-
readonly #getTenantPath: string;
10-
11-
constructor(tenantPath: string)
12-
{
13-
this.#getTenantPath = tenantPath;
14-
}
13+
readonly #tenants = new Map<string, Tenant>();
1514

1615
async handle(request: Request, next: NextHandler): Promise<Response>
1716
{
18-
return request.fqn === this.#getTenantPath
19-
? this.#getTenant(request, next)
17+
return request.fqn === GEY_BY_ORIGIN_FQN
18+
? this.#getTenant(request)
2019
: this.#handleRequest(request, next);
2120
}
2221

23-
async #getTenant(request: Request, next: NextHandler): Promise<Response>
22+
async #resolveTenant(request: Request): Promise<Tenant>
2423
{
25-
const origin = this.#getOrigin(request);
26-
const cached = this.#cache.get(origin);
24+
const origin = request.getHeader('origin') as string;
25+
const tenant = this.#tenants.get(origin);
2726

28-
if (cached === undefined)
27+
if (tenant === undefined)
2928
{
30-
request.setArgument('origin', origin);
29+
const tenant = await getByOrigin(origin);
3130

32-
const response = await next();
31+
this.#tenants.set(origin, tenant);
3332

34-
if (response.status === 200)
35-
{
36-
this.#cache.set(origin, response);
37-
}
38-
39-
return response;
33+
return tenant;
4034
}
4135

42-
return cached;
36+
return tenant;
4337
}
4438

45-
async #handleRequest(request: Request, next: NextHandler): Promise<Response>
39+
async #getTenant(request: Request): Promise<Response>
4640
{
47-
const origin = this.#getOrigin(request);
48-
const cached = this.#cache.get(origin);
49-
50-
if (cached !== undefined)
51-
{
52-
request.setArgument(TENANT_PARAMETER, cached.result);
53-
}
41+
const tenant = await this.#resolveTenant(request);
5442

55-
return next();
43+
return new Response(200, tenant);
5644
}
5745

58-
#getOrigin(request: Request): string
46+
async #handleRequest(request: Request, next: NextHandler): Promise<Response>
5947
{
60-
return request.getHeader('origin') as string;
48+
const tenant = await this.#resolveTenant(request);
49+
50+
request.setArgument(TENANT_PARAMETER, tenant);
51+
52+
return next();
6153
}
6254
}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11

22
import database from '^/integrations/database';
3-
import eventBroker from '^/integrations/eventbroker';
43
import fileStore from '^/integrations/filestore';
5-
import notificationService from '^/integrations/notification';
64

75
const disconnections = [];
86

97
if (database.connected) disconnections.push(database.disconnect());
10-
if (eventBroker.connected) disconnections.push(eventBroker.disconnect());
118
if (fileStore.connected) disconnections.push(fileStore.disconnect());
12-
if (notificationService.connected) disconnections.push(notificationService.disconnect());
139

1410
await Promise.allSettled(disconnections);
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11

2-
import { TENANT_BY_ORIGIN_PATH } from '^/domain/definitions';
3-
42
import TenantMiddleware from './middlewares/TenantMiddleware';
53

6-
const tenantPath = TENANT_BY_ORIGIN_PATH;
7-
8-
export default new TenantMiddleware(tenantPath);
4+
export default new TenantMiddleware();

0 commit comments

Comments
 (0)