diff --git a/server/src/app.module.ts b/server/src/app.module.ts index 0a7f47959d..ef02416e58 100644 --- a/server/src/app.module.ts +++ b/server/src/app.module.ts @@ -7,7 +7,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { ClsModule } from 'nestjs-cls'; import { OpenTelemetryModule } from 'nestjs-otel'; import { commands } from 'src/commands'; -import { clsConfig, immichAppConfig } from 'src/config'; +import { immichAppConfig } from 'src/config'; import { controllers } from 'src/controllers'; import { entities } from 'src/entities'; import { ImmichWorker } from 'src/enum'; @@ -37,12 +37,12 @@ const middleware = [ ]; const configRepository = new ConfigRepository(); -const { bull, database, otel } = configRepository.getEnv(); +const { bull, cls, database, otel } = configRepository.getEnv(); const imports = [ BullModule.forRoot(bull.config), BullModule.registerQueue(...bull.queues), - ClsModule.forRoot(clsConfig), + ClsModule.forRoot(cls.config), ConfigModule.forRoot(immichAppConfig), OpenTelemetryModule.forRoot(otel), TypeOrmModule.forRootAsync({ diff --git a/server/src/config.ts b/server/src/config.ts index 0b750afeff..7bc93d7608 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -1,15 +1,12 @@ import { ConfigModuleOptions } from '@nestjs/config'; import { CronExpression } from '@nestjs/schedule'; -import { Request, Response } from 'express'; import Joi, { Root } from 'joi'; -import { CLS_ID, ClsModuleOptions } from 'nestjs-cls'; import { AudioCodec, Colorspace, CQMode, ImageFormat, ImmichEnvironment, - ImmichHeader, LogLevel, ToneMapping, 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); - }, - }, -}; diff --git a/server/src/interfaces/config.interface.ts b/server/src/interfaces/config.interface.ts index f221b7ee4f..ec5397cc2c 100644 --- a/server/src/interfaces/config.interface.ts +++ b/server/src/interfaces/config.interface.ts @@ -1,6 +1,7 @@ import { RegisterQueueOptions } from '@nestjs/bullmq'; import { QueueOptions } from 'bullmq'; import { RedisOptions } from 'ioredis'; +import { ClsModuleOptions } from 'nestjs-cls'; import { OpenTelemetryModuleOptions } from 'nestjs-otel/lib/interfaces'; import { ImmichEnvironment, ImmichTelemetry, ImmichWorker, LogLevel } from 'src/enum'; import { DatabaseConnectionParams, VectorExtension } from 'src/interfaces/database.interface'; @@ -36,6 +37,10 @@ export interface EnvData { queues: RegisterQueueOptions[]; }; + cls: { + config: ClsModuleOptions; + }; + database: { config: PostgresConnectionOptions & DatabaseConnectionParams; skipMigrations: boolean; diff --git a/server/src/repositories/config.repository.ts b/server/src/repositories/config.repository.ts index 3cb1ada412..4fdda028e3 100644 --- a/server/src/repositories/config.repository.ts +++ b/server/src/repositories/config.repository.ts @@ -1,8 +1,10 @@ import { Injectable } from '@nestjs/common'; +import { Request, Response } from 'express'; +import { CLS_ID } from 'nestjs-cls'; import { join, resolve } from 'node:path'; import { citiesFile, excludePaths } from 'src/constants'; 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 { DatabaseExtension } from 'src/interfaces/database.interface'; import { QueueName } from 'src/interfaces/job.interface'; @@ -121,6 +123,22 @@ const getEnv = (): EnvData => { 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: { config: { type: 'postgres', diff --git a/server/test/repositories/config.repository.mock.ts b/server/test/repositories/config.repository.mock.ts index ba85cc7d09..55a890fa4a 100644 --- a/server/test/repositories/config.repository.mock.ts +++ b/server/test/repositories/config.repository.mock.ts @@ -15,6 +15,10 @@ const envData: EnvData = { queues: [{ name: 'queue-1' }], }, + cls: { + config: {}, + }, + database: { config: { connectionType: 'parts',