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';
|
2025-07-18 10:57:29 -04:00
|
|
|
import { commandsAndQuestions } 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';
|
2025-11-19 15:27:16 +00:00
|
|
|
import { StorageCore } from 'src/cores/storage.core';
|
2024-10-04 16:57:34 -04:00
|
|
|
import { ImmichWorker } from 'src/enum';
|
2025-11-17 17:15:44 +00:00
|
|
|
import { MaintenanceAuthGuard } from 'src/maintenance/maintenance-auth.guard';
|
|
|
|
|
import { MaintenanceWebsocketRepository } from 'src/maintenance/maintenance-websocket.repository';
|
|
|
|
|
import { MaintenanceWorkerController } from 'src/maintenance/maintenance-worker.controller';
|
|
|
|
|
import { MaintenanceWorkerService } from 'src/maintenance/maintenance-worker.service';
|
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';
|
2025-11-17 17:15:44 +00:00
|
|
|
import { AppRepository } from 'src/repositories/app.repository';
|
2024-10-17 10:50:54 -04:00
|
|
|
import { ConfigRepository } from 'src/repositories/config.repository';
|
2025-11-19 15:11:37 +00:00
|
|
|
import { DatabaseRepository } from 'src/repositories/database.repository';
|
2025-02-11 15:12:31 -05:00
|
|
|
import { EventRepository } from 'src/repositories/event.repository';
|
2025-01-23 08:31:30 -05:00
|
|
|
import { LoggingRepository } from 'src/repositories/logging.repository';
|
2025-11-19 15:11:37 +00:00
|
|
|
import { ProcessRepository } from 'src/repositories/process.repository';
|
|
|
|
|
import { StorageRepository } from 'src/repositories/storage.repository';
|
2025-11-17 17:15:44 +00:00
|
|
|
import { SystemMetadataRepository } from 'src/repositories/system-metadata.repository';
|
2025-01-23 18:10:17 -05:00
|
|
|
import { teardownTelemetry, TelemetryRepository } from 'src/repositories/telemetry.repository';
|
2025-10-24 16:26:27 -04:00
|
|
|
import { WebsocketRepository } from 'src/repositories/websocket.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-11-14 14:42:00 -05:00
|
|
|
import { QueueService } from 'src/services/queue.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
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
const commonMiddleware = [
|
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 },
|
|
|
|
|
];
|
|
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
const apiMiddleware = [FileUploadInterceptor, ...commonMiddleware, { 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
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
const commonImports = [
|
2024-10-29 16:41:47 -04:00
|
|
|
ClsModule.forRoot(cls.config),
|
2025-04-24 12:58:29 -04:00
|
|
|
KyselyModule.forRoot(getKyselyConfig(database.config)),
|
2025-11-17 17:15:44 +00:00
|
|
|
OpenTelemetryModule.forRoot(otel),
|
2024-03-21 09:08:29 -05:00
|
|
|
];
|
|
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
const bullImports = [BullModule.forRoot(bull.config), BullModule.registerQueue(...bull.queues)];
|
|
|
|
|
|
|
|
|
|
export 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-11-14 14:42:00 -05:00
|
|
|
private authService: AuthService,
|
2025-02-11 15:12:31 -05:00
|
|
|
private eventRepository: EventRepository,
|
2025-11-14 14:42:00 -05:00
|
|
|
private queueService: QueueService,
|
2025-01-23 18:10:17 -05:00
|
|
|
private telemetryRepository: TelemetryRepository,
|
2025-11-14 14:42:00 -05:00
|
|
|
private websocketRepository: WebsocketRepository,
|
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
|
|
|
|
2025-11-14 14:42:00 -05:00
|
|
|
this.queueService.setServices(services);
|
2024-10-31 13:42:58 -04:00
|
|
|
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.setAuthFn(async (client) =>
|
2025-02-11 15:12:31 -05:00
|
|
|
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 });
|
2025-07-15 13:41:19 -04:00
|
|
|
await this.eventRepository.emit('AppBootstrap');
|
2024-06-27 15:54:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onModuleDestroy() {
|
2025-07-15 13:41:19 -04:00
|
|
|
await this.eventRepository.emit('AppShutdown');
|
2024-10-21 19:52:30 -04:00
|
|
|
await teardownTelemetry();
|
2024-03-21 09:08:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Module({
|
2025-11-17 17:15:44 +00:00
|
|
|
imports: [...bullImports, ...commonImports, ScheduleModule.forRoot()],
|
2024-10-04 16:57:34 -04:00
|
|
|
controllers: [...controllers],
|
2025-11-17 17:15:44 +00:00
|
|
|
providers: [...common, ...apiMiddleware, { 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({
|
2025-11-17 17:15:44 +00:00
|
|
|
imports: [...commonImports],
|
|
|
|
|
controllers: [MaintenanceWorkerController],
|
|
|
|
|
providers: [
|
|
|
|
|
ConfigRepository,
|
|
|
|
|
LoggingRepository,
|
2025-11-19 15:11:37 +00:00
|
|
|
StorageRepository,
|
|
|
|
|
ProcessRepository,
|
|
|
|
|
DatabaseRepository,
|
2025-11-17 17:15:44 +00:00
|
|
|
SystemMetadataRepository,
|
|
|
|
|
AppRepository,
|
|
|
|
|
MaintenanceWebsocketRepository,
|
|
|
|
|
MaintenanceWorkerService,
|
|
|
|
|
...commonMiddleware,
|
|
|
|
|
{ provide: APP_GUARD, useClass: MaintenanceAuthGuard },
|
|
|
|
|
{ provide: IWorker, useValue: ImmichWorker.Maintenance },
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
export class MaintenanceModule {
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(IWorker) private worker: ImmichWorker,
|
|
|
|
|
logger: LoggingRepository,
|
2025-11-19 15:13:11 +00:00
|
|
|
private maintenanceWorkerService: MaintenanceWorkerService,
|
2025-11-17 17:15:44 +00:00
|
|
|
) {
|
|
|
|
|
logger.setAppName(this.worker);
|
|
|
|
|
}
|
2025-11-19 15:13:11 +00:00
|
|
|
|
|
|
|
|
async onModuleInit() {
|
2025-11-19 15:27:16 +00:00
|
|
|
StorageCore.setMediaLocation(this.maintenanceWorkerService.detectMediaLocation());
|
|
|
|
|
|
2025-11-19 15:13:11 +00:00
|
|
|
await this.maintenanceWorkerService.logSecret();
|
|
|
|
|
}
|
2025-11-17 17:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Module({
|
|
|
|
|
imports: [...bullImports, ...commonImports],
|
2025-07-15 14:50:13 -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({
|
2025-11-17 17:15:44 +00:00
|
|
|
imports: [...bullImports, ...commonImports],
|
2025-07-18 10:57:29 -04:00
|
|
|
providers: [...common, ...commandsAndQuestions, 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();
|
|
|
|
|
}
|
|
|
|
|
}
|