1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-27 10:58:13 +02:00

refactor(server): cls (#13814)

This commit is contained in:
Jason Rasmussen 2024-10-29 16:41:47 -04:00 committed by GitHub
parent 2efba6326d
commit 19eb3ed8b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 31 additions and 21 deletions

View File

@ -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({

View File

@ -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);
},
},
};

View File

@ -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;

View File

@ -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',

View File

@ -15,6 +15,10 @@ const envData: EnvData = {
queues: [{ name: 'queue-1' }],
},
cls: {
config: {},
},
database: {
config: {
connectionType: 'parts',