mirror of
https://github.com/immich-app/immich.git
synced 2025-01-01 08:31:59 +00:00
refactor(server): cls (#13814)
This commit is contained in:
parent
2efba6326d
commit
19eb3ed8b9
5 changed files with 31 additions and 21 deletions
|
@ -7,7 +7,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { ClsModule } from 'nestjs-cls';
|
import { ClsModule } from 'nestjs-cls';
|
||||||
import { OpenTelemetryModule } from 'nestjs-otel';
|
import { OpenTelemetryModule } from 'nestjs-otel';
|
||||||
import { commands } from 'src/commands';
|
import { commands } from 'src/commands';
|
||||||
import { clsConfig, immichAppConfig } from 'src/config';
|
import { immichAppConfig } from 'src/config';
|
||||||
import { controllers } from 'src/controllers';
|
import { controllers } from 'src/controllers';
|
||||||
import { entities } from 'src/entities';
|
import { entities } from 'src/entities';
|
||||||
import { ImmichWorker } from 'src/enum';
|
import { ImmichWorker } from 'src/enum';
|
||||||
|
@ -37,12 +37,12 @@ const middleware = [
|
||||||
];
|
];
|
||||||
|
|
||||||
const configRepository = new ConfigRepository();
|
const configRepository = new ConfigRepository();
|
||||||
const { bull, database, otel } = configRepository.getEnv();
|
const { bull, cls, database, otel } = configRepository.getEnv();
|
||||||
|
|
||||||
const imports = [
|
const imports = [
|
||||||
BullModule.forRoot(bull.config),
|
BullModule.forRoot(bull.config),
|
||||||
BullModule.registerQueue(...bull.queues),
|
BullModule.registerQueue(...bull.queues),
|
||||||
ClsModule.forRoot(clsConfig),
|
ClsModule.forRoot(cls.config),
|
||||||
ConfigModule.forRoot(immichAppConfig),
|
ConfigModule.forRoot(immichAppConfig),
|
||||||
OpenTelemetryModule.forRoot(otel),
|
OpenTelemetryModule.forRoot(otel),
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
import { ConfigModuleOptions } from '@nestjs/config';
|
import { ConfigModuleOptions } from '@nestjs/config';
|
||||||
import { CronExpression } from '@nestjs/schedule';
|
import { CronExpression } from '@nestjs/schedule';
|
||||||
import { Request, Response } from 'express';
|
|
||||||
import Joi, { Root } from 'joi';
|
import Joi, { Root } from 'joi';
|
||||||
import { CLS_ID, ClsModuleOptions } from 'nestjs-cls';
|
|
||||||
import {
|
import {
|
||||||
AudioCodec,
|
AudioCodec,
|
||||||
Colorspace,
|
Colorspace,
|
||||||
CQMode,
|
CQMode,
|
||||||
ImageFormat,
|
ImageFormat,
|
||||||
ImmichEnvironment,
|
ImmichEnvironment,
|
||||||
ImmichHeader,
|
|
||||||
LogLevel,
|
LogLevel,
|
||||||
ToneMapping,
|
ToneMapping,
|
||||||
TranscodeHWAccel,
|
TranscodeHWAccel,
|
||||||
|
@ -354,17 +351,3 @@ export const immichAppConfig: ConfigModuleOptions = {
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
export const clsConfig: ClsModuleOptions = {
|
|
||||||
middleware: {
|
|
||||||
mount: true,
|
|
||||||
generateId: true,
|
|
||||||
setup: (cls, req: Request, res: Response) => {
|
|
||||||
const headerValues = req.headers[ImmichHeader.CID];
|
|
||||||
const headerValue = Array.isArray(headerValues) ? headerValues[0] : headerValues;
|
|
||||||
const cid = headerValue || cls.get(CLS_ID);
|
|
||||||
cls.set(CLS_ID, cid);
|
|
||||||
res.header(ImmichHeader.CID, cid);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { RegisterQueueOptions } from '@nestjs/bullmq';
|
import { RegisterQueueOptions } from '@nestjs/bullmq';
|
||||||
import { QueueOptions } from 'bullmq';
|
import { QueueOptions } from 'bullmq';
|
||||||
import { RedisOptions } from 'ioredis';
|
import { RedisOptions } from 'ioredis';
|
||||||
|
import { ClsModuleOptions } from 'nestjs-cls';
|
||||||
import { OpenTelemetryModuleOptions } from 'nestjs-otel/lib/interfaces';
|
import { OpenTelemetryModuleOptions } from 'nestjs-otel/lib/interfaces';
|
||||||
import { ImmichEnvironment, ImmichTelemetry, ImmichWorker, LogLevel } from 'src/enum';
|
import { ImmichEnvironment, ImmichTelemetry, ImmichWorker, LogLevel } from 'src/enum';
|
||||||
import { DatabaseConnectionParams, VectorExtension } from 'src/interfaces/database.interface';
|
import { DatabaseConnectionParams, VectorExtension } from 'src/interfaces/database.interface';
|
||||||
|
@ -36,6 +37,10 @@ export interface EnvData {
|
||||||
queues: RegisterQueueOptions[];
|
queues: RegisterQueueOptions[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
cls: {
|
||||||
|
config: ClsModuleOptions;
|
||||||
|
};
|
||||||
|
|
||||||
database: {
|
database: {
|
||||||
config: PostgresConnectionOptions & DatabaseConnectionParams;
|
config: PostgresConnectionOptions & DatabaseConnectionParams;
|
||||||
skipMigrations: boolean;
|
skipMigrations: boolean;
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { Request, Response } from 'express';
|
||||||
|
import { CLS_ID } from 'nestjs-cls';
|
||||||
import { join, resolve } from 'node:path';
|
import { join, resolve } from 'node:path';
|
||||||
import { citiesFile, excludePaths } from 'src/constants';
|
import { citiesFile, excludePaths } from 'src/constants';
|
||||||
import { Telemetry } from 'src/decorators';
|
import { Telemetry } from 'src/decorators';
|
||||||
import { ImmichEnvironment, ImmichTelemetry, ImmichWorker, LogLevel } from 'src/enum';
|
import { ImmichEnvironment, ImmichHeader, ImmichTelemetry, ImmichWorker, LogLevel } from 'src/enum';
|
||||||
import { EnvData, IConfigRepository } from 'src/interfaces/config.interface';
|
import { EnvData, IConfigRepository } from 'src/interfaces/config.interface';
|
||||||
import { DatabaseExtension } from 'src/interfaces/database.interface';
|
import { DatabaseExtension } from 'src/interfaces/database.interface';
|
||||||
import { QueueName } from 'src/interfaces/job.interface';
|
import { QueueName } from 'src/interfaces/job.interface';
|
||||||
|
@ -121,6 +123,22 @@ const getEnv = (): EnvData => {
|
||||||
queues: Object.values(QueueName).map((name) => ({ name })),
|
queues: Object.values(QueueName).map((name) => ({ name })),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
cls: {
|
||||||
|
config: {
|
||||||
|
middleware: {
|
||||||
|
mount: true,
|
||||||
|
generateId: true,
|
||||||
|
setup: (cls, req: Request, res: Response) => {
|
||||||
|
const headerValues = req.headers[ImmichHeader.CID];
|
||||||
|
const headerValue = Array.isArray(headerValues) ? headerValues[0] : headerValues;
|
||||||
|
const cid = headerValue || cls.get(CLS_ID);
|
||||||
|
cls.set(CLS_ID, cid);
|
||||||
|
res.header(ImmichHeader.CID, cid);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
database: {
|
database: {
|
||||||
config: {
|
config: {
|
||||||
type: 'postgres',
|
type: 'postgres',
|
||||||
|
|
|
@ -15,6 +15,10 @@ const envData: EnvData = {
|
||||||
queues: [{ name: 'queue-1' }],
|
queues: [{ name: 'queue-1' }],
|
||||||
},
|
},
|
||||||
|
|
||||||
|
cls: {
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
|
||||||
database: {
|
database: {
|
||||||
config: {
|
config: {
|
||||||
connectionType: 'parts',
|
connectionType: 'parts',
|
||||||
|
|
Loading…
Reference in a new issue