2023-06-01 06:32:51 -04:00
|
|
|
import { BullModule } from '@nestjs/bullmq';
|
2024-06-27 15:54:20 -04:00
|
|
|
import { Inject, Module, OnModuleDestroy, OnModuleInit, ValidationPipe } from '@nestjs/common';
|
2025-03-06 13:33:24 -05:00
|
|
|
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
|
2023-10-31 23:40:35 -05:00
|
|
|
import { ScheduleModule, SchedulerRegistry } from '@nestjs/schedule';
|
2024-04-15 19:39:06 -04:00
|
|
|
import { ClsModule } from 'nestjs-cls';
|
2025-01-09 11:15:41 -05:00
|
|
|
import { KyselyModule } from 'nestjs-kysely';
|
2024-03-12 01:19:12 -04:00
|
|
|
import { OpenTelemetryModule } from 'nestjs-otel';
|
2024-03-30 00:16:06 -04:00
|
|
|
import { commands } from 'src/commands';
|
2024-11-01 17:19:36 -04:00
|
|
|
import { IWorker } from 'src/constants';
|
2024-03-30 00:16:06 -04:00
|
|
|
import { controllers } from 'src/controllers';
|
2024-10-04 16:57:34 -04:00
|
|
|
import { ImmichWorker } from 'src/enum';
|
2024-03-21 09:08:29 -05:00
|
|
|
import { AuthGuard } from 'src/middleware/auth.guard';
|
|
|
|
import { ErrorInterceptor } from 'src/middleware/error.interceptor';
|
|
|
|
import { FileUploadInterceptor } from 'src/middleware/file-upload.interceptor';
|
2024-09-04 13:32:43 -04:00
|
|
|
import { GlobalExceptionFilter } from 'src/middleware/global-exception.filter';
|
2024-04-16 19:21:57 -04:00
|
|
|
import { LoggingInterceptor } from 'src/middleware/logging.interceptor';
|
2025-02-11 17:15:56 -05:00
|
|
|
import { repositories } from 'src/repositories';
|
2024-10-17 10:50:54 -04:00
|
|
|
import { ConfigRepository } from 'src/repositories/config.repository';
|
2025-02-11 15:12:31 -05:00
|
|
|
import { EventRepository } from 'src/repositories/event.repository';
|
2025-02-11 17:15:56 -05:00
|
|
|
import { JobRepository } from 'src/repositories/job.repository';
|
2025-01-23 08:31:30 -05:00
|
|
|
import { LoggingRepository } from 'src/repositories/logging.repository';
|
2025-01-23 18:10:17 -05:00
|
|
|
import { teardownTelemetry, TelemetryRepository } from 'src/repositories/telemetry.repository';
|
2024-03-30 00:16:06 -04:00
|
|
|
import { services } from 'src/services';
|
2025-02-11 15:12:31 -05:00
|
|
|
import { AuthService } from 'src/services/auth.service';
|
2025-01-13 19:30:34 -06:00
|
|
|
import { CliService } from 'src/services/cli.service';
|
2025-04-15 13:26:56 -04:00
|
|
|
import { getKyselyConfig } from 'src/utils/database';
|
2023-01-11 21:34:36 -05:00
|
|
|
|
2025-02-21 04:37:57 +00:00
|
|
|
const common = [...repositories, ...services, GlobalExceptionFilter];
|
2023-01-11 21:34:36 -05:00
|
|
|
|
2024-03-21 09:08:29 -05:00
|
|
|
const middleware = [
|
|
|
|
FileUploadInterceptor,
|
2024-09-04 13:32:43 -04:00
|
|
|
{ provide: APP_FILTER, useClass: GlobalExceptionFilter },
|
2024-03-21 09:08:29 -05:00
|
|
|
{ provide: APP_PIPE, useValue: new ValidationPipe({ transform: true, whitelist: true }) },
|
2024-04-16 19:21:57 -04:00
|
|
|
{ provide: APP_INTERCEPTOR, useClass: LoggingInterceptor },
|
2024-03-21 09:08:29 -05:00
|
|
|
{ provide: APP_INTERCEPTOR, useClass: ErrorInterceptor },
|
|
|
|
{ provide: APP_GUARD, useClass: AuthGuard },
|
|
|
|
];
|
|
|
|
|
2024-10-17 10:50:54 -04:00
|
|
|
const configRepository = new ConfigRepository();
|
2024-10-29 16:41:47 -04:00
|
|
|
const { bull, cls, database, otel } = configRepository.getEnv();
|
2024-10-17 10:50:54 -04:00
|
|
|
|
2024-03-21 09:08:29 -05:00
|
|
|
const imports = [
|
2024-10-17 10:50:54 -04:00
|
|
|
BullModule.forRoot(bull.config),
|
|
|
|
BullModule.registerQueue(...bull.queues),
|
2024-10-29 16:41:47 -04:00
|
|
|
ClsModule.forRoot(cls.config),
|
2024-10-17 18:04:25 -04:00
|
|
|
OpenTelemetryModule.forRoot(otel),
|
2025-04-15 13:26:56 -04:00
|
|
|
KyselyModule.forRoot(getKyselyConfig(database.config.kysely)),
|
2024-03-21 09:08:29 -05:00
|
|
|
];
|
|
|
|
|
2024-11-01 17:19:36 -04:00
|
|
|
class BaseModule implements OnModuleInit, OnModuleDestroy {
|
2024-06-27 15:54:20 -04:00
|
|
|
constructor(
|
2024-11-01 17:19:36 -04:00
|
|
|
@Inject(IWorker) private worker: ImmichWorker,
|
2025-01-23 08:31:30 -05:00
|
|
|
logger: LoggingRepository,
|
2025-02-11 15:12:31 -05:00
|
|
|
private eventRepository: EventRepository,
|
2025-02-11 17:15:56 -05:00
|
|
|
private jobRepository: JobRepository,
|
2025-01-23 18:10:17 -05:00
|
|
|
private telemetryRepository: TelemetryRepository,
|
2025-02-11 15:12:31 -05:00
|
|
|
private authService: AuthService,
|
2024-10-03 17:49:03 -04:00
|
|
|
) {
|
2024-10-04 16:57:34 -04:00
|
|
|
logger.setAppName(this.worker);
|
2024-10-03 17:49:03 -04:00
|
|
|
}
|
2024-03-21 09:08:29 -05:00
|
|
|
|
2024-10-04 16:57:34 -04:00
|
|
|
async onModuleInit() {
|
2025-02-11 17:15:56 -05:00
|
|
|
this.telemetryRepository.setup({ repositories });
|
2024-10-31 13:42:58 -04:00
|
|
|
|
|
|
|
this.jobRepository.setup({ services });
|
|
|
|
if (this.worker === ImmichWorker.MICROSERVICES) {
|
|
|
|
this.jobRepository.startWorkers();
|
|
|
|
}
|
|
|
|
|
2025-02-11 15:12:31 -05:00
|
|
|
this.eventRepository.setAuthFn(async (client) =>
|
|
|
|
this.authService.authenticate({
|
|
|
|
headers: client.request.headers,
|
|
|
|
queryParams: {},
|
|
|
|
metadata: { adminRoute: false, sharedLinkRoute: false, uri: '/api/socket.io' },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2024-10-04 16:57:34 -04:00
|
|
|
this.eventRepository.setup({ services });
|
2024-11-05 16:30:56 +00:00
|
|
|
await this.eventRepository.emit('app.bootstrap');
|
2024-06-27 15:54:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async onModuleDestroy() {
|
2024-11-05 16:30:56 +00:00
|
|
|
await this.eventRepository.emit('app.shutdown');
|
2024-10-21 19:52:30 -04:00
|
|
|
await teardownTelemetry();
|
2024-03-21 09:08:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Module({
|
2024-10-04 16:57:34 -04:00
|
|
|
imports: [...imports, ScheduleModule.forRoot()],
|
|
|
|
controllers: [...controllers],
|
2024-11-01 17:19:36 -04:00
|
|
|
providers: [...common, ...middleware, { provide: IWorker, useValue: ImmichWorker.API }],
|
2023-01-11 21:34:36 -05:00
|
|
|
})
|
2024-11-01 17:19:36 -04:00
|
|
|
export class ApiModule extends BaseModule {}
|
2024-06-27 15:54:20 -04:00
|
|
|
|
2024-10-04 16:57:34 -04:00
|
|
|
@Module({
|
|
|
|
imports: [...imports],
|
2024-11-01 17:19:36 -04:00
|
|
|
providers: [...common, { provide: IWorker, useValue: ImmichWorker.MICROSERVICES }, SchedulerRegistry],
|
2024-10-04 16:57:34 -04:00
|
|
|
})
|
2024-11-01 17:19:36 -04:00
|
|
|
export class MicroservicesModule extends BaseModule {}
|
2024-03-21 09:08:29 -05:00
|
|
|
|
|
|
|
@Module({
|
|
|
|
imports: [...imports],
|
2024-03-30 00:16:06 -04:00
|
|
|
providers: [...common, ...commands, SchedulerRegistry],
|
2024-03-21 09:08:29 -05:00
|
|
|
})
|
2025-01-13 19:30:34 -06:00
|
|
|
export class ImmichAdminModule implements OnModuleDestroy {
|
|
|
|
constructor(private service: CliService) {}
|
|
|
|
|
|
|
|
async onModuleDestroy() {
|
|
|
|
await this.service.cleanup();
|
|
|
|
}
|
|
|
|
}
|