From 907eb869bc8804de505da827869164f7f7982639 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Wed, 20 Mar 2024 16:22:47 -0500 Subject: [PATCH] chore: move apps and test utils (#8129) --- .../e2e/jobs/specs/library-watcher.e2e-spec.ts | 2 +- server/e2e/jobs/specs/library.e2e-spec.ts | 2 +- server/src/{immich/main.ts => apps/api.main.ts} | 10 +++++----- .../{immich/app.module.ts => apps/api.module.ts} | 8 ++++---- .../app.service.ts => apps/api.service.ts} | 4 ++-- server/src/apps/immich-admin.main.ts | 8 ++++++++ .../immich-admin.module.ts} | 2 +- .../main.ts => apps/microservices.main.ts} | 4 ++-- .../microservices.module.ts | 6 +++--- .../microservices.service.ts} | 2 +- server/src/immich-admin/main.ts | 8 -------- server/src/main.ts | 12 ++++++------ server/{src/test-utils => test}/utils.ts | 16 ++++++++-------- 13 files changed, 42 insertions(+), 42 deletions(-) rename server/src/{immich/main.ts => apps/api.main.ts} (89%) rename server/src/{immich/app.module.ts => apps/api.module.ts} (95%) rename server/src/{immich/app.service.ts => apps/api.service.ts} (97%) create mode 100755 server/src/apps/immich-admin.main.ts rename server/src/{immich-admin/app.module.ts => apps/immich-admin.module.ts} (95%) rename server/src/{microservices/main.ts => apps/microservices.main.ts} (86%) rename server/src/{microservices => apps}/microservices.module.ts (67%) rename server/src/{microservices/app.service.ts => apps/microservices.service.ts} (99%) delete mode 100755 server/src/immich-admin/main.ts rename server/{src/test-utils => test}/utils.ts (93%) diff --git a/server/e2e/jobs/specs/library-watcher.e2e-spec.ts b/server/e2e/jobs/specs/library-watcher.e2e-spec.ts index e8be570ca4..3d43227b7b 100644 --- a/server/e2e/jobs/specs/library-watcher.e2e-spec.ts +++ b/server/e2e/jobs/specs/library-watcher.e2e-spec.ts @@ -13,7 +13,7 @@ import { restoreTempFolder, testApp, waitForEvent, -} from 'src/test-utils/utils'; +} from 'test/utils'; describe(`Library watcher (e2e)`, () => { let server: any; diff --git a/server/e2e/jobs/specs/library.e2e-spec.ts b/server/e2e/jobs/specs/library.e2e-spec.ts index c2839a7d12..657c53fc24 100644 --- a/server/e2e/jobs/specs/library.e2e-spec.ts +++ b/server/e2e/jobs/specs/library.e2e-spec.ts @@ -3,10 +3,10 @@ import fs from 'node:fs'; import { LibraryController } from 'src/controllers/library.controller'; import { LoginResponseDto } from 'src/domain/auth/auth.dto'; import { LibraryType } from 'src/entities/library.entity'; -import { IMMICH_TEST_ASSET_PATH, IMMICH_TEST_ASSET_TEMP_PATH, restoreTempFolder, testApp } from 'src/test-utils/utils'; import request from 'supertest'; import { errorStub } from 'test/fixtures/error.stub'; import { uuidStub } from 'test/fixtures/uuid.stub'; +import { IMMICH_TEST_ASSET_PATH, IMMICH_TEST_ASSET_TEMP_PATH, restoreTempFolder, testApp } from 'test/utils'; import { utimes } from 'utimes'; describe(`${LibraryController.name} (e2e)`, () => { diff --git a/server/src/immich/main.ts b/server/src/apps/api.main.ts similarity index 89% rename from server/src/immich/main.ts rename to server/src/apps/api.main.ts index 310ac93023..9ffdd1d482 100644 --- a/server/src/immich/main.ts +++ b/server/src/apps/api.main.ts @@ -4,10 +4,10 @@ import { json } from 'body-parser'; import cookieParser from 'cookie-parser'; import { existsSync } from 'node:fs'; import sirv from 'sirv'; +import { ApiModule } from 'src/apps/api.module'; +import { ApiService } from 'src/apps/api.service'; import { excludePaths } from 'src/config'; import { WEB_ROOT, envName, isDev, serverVersion } from 'src/domain/domain.constant'; -import { AppModule } from 'src/immich/app.module'; -import { AppService } from 'src/immich/app.service'; import { useSwagger } from 'src/immich/app.utils'; import { otelSDK } from 'src/infra/instrumentation'; import { ImmichLogger } from 'src/infra/logger'; @@ -16,9 +16,9 @@ import { WebSocketAdapter } from 'src/infra/websocket.adapter'; const logger = new ImmichLogger('ImmichServer'); const port = Number(process.env.SERVER_PORT) || 3001; -export async function bootstrap() { +export async function bootstrapApi() { otelSDK.start(); - const app = await NestFactory.create(AppModule, { bufferLogs: true }); + const app = await NestFactory.create(ApiModule, { bufferLogs: true }); app.useLogger(app.get(ImmichLogger)); app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']); @@ -48,7 +48,7 @@ export async function bootstrap() { }), ); } - app.use(app.get(AppService).ssr(excludePaths)); + app.use(app.get(ApiService).ssr(excludePaths)); const server = await app.listen(port); server.requestTimeout = 30 * 60 * 1000; diff --git a/server/src/immich/app.module.ts b/server/src/apps/api.module.ts similarity index 95% rename from server/src/immich/app.module.ts rename to server/src/apps/api.module.ts index a5b88473e7..04c95f1999 100644 --- a/server/src/immich/app.module.ts +++ b/server/src/apps/api.module.ts @@ -2,6 +2,7 @@ import { Module, OnModuleInit, ValidationPipe } from '@nestjs/common'; import { APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core'; import { ScheduleModule } from '@nestjs/schedule'; import { TypeOrmModule } from '@nestjs/typeorm'; +import { ApiService } from 'src/apps/api.service'; import { ActivityController } from 'src/controllers/activity.controller'; import { AlbumController } from 'src/controllers/album.controller'; import { APIKeyController } from 'src/controllers/api-key.controller'; @@ -29,7 +30,6 @@ import { ExifEntity } from 'src/entities/exif.entity'; import { AssetRepositoryV1, IAssetRepositoryV1 } from 'src/immich/api-v1/asset/asset-repository'; import { AssetController as AssetControllerV1 } from 'src/immich/api-v1/asset/asset.controller'; import { AssetService as AssetServiceV1 } from 'src/immich/api-v1/asset/asset.service'; -import { AppService } from 'src/immich/app.service'; import { InfraModule } from 'src/infra/infra.module'; import { AuthGuard } from 'src/middleware/auth.guard'; import { ErrorInterceptor } from 'src/middleware/error.interceptor'; @@ -73,13 +73,13 @@ import { FileUploadInterceptor } from 'src/middleware/file-upload.interceptor'; { provide: APP_INTERCEPTOR, useClass: ErrorInterceptor }, { provide: APP_GUARD, useClass: AuthGuard }, { provide: IAssetRepositoryV1, useClass: AssetRepositoryV1 }, - AppService, + ApiService, AssetServiceV1, FileUploadInterceptor, ], }) -export class AppModule implements OnModuleInit { - constructor(private appService: AppService) {} +export class ApiModule implements OnModuleInit { + constructor(private appService: ApiService) {} async onModuleInit() { await this.appService.init(); diff --git a/server/src/immich/app.service.ts b/server/src/apps/api.service.ts similarity index 97% rename from server/src/immich/app.service.ts rename to server/src/apps/api.service.ts index f7f1664be5..31134cefad 100644 --- a/server/src/immich/app.service.ts +++ b/server/src/apps/api.service.ts @@ -35,8 +35,8 @@ const render = (index: string, meta: OpenGraphTags) => { }; @Injectable() -export class AppService { - private logger = new ImmichLogger(AppService.name); +export class ApiService { + private logger = new ImmichLogger(ApiService.name); constructor( private authService: AuthService, diff --git a/server/src/apps/immich-admin.main.ts b/server/src/apps/immich-admin.main.ts new file mode 100755 index 0000000000..5f528a21b6 --- /dev/null +++ b/server/src/apps/immich-admin.main.ts @@ -0,0 +1,8 @@ +import { CommandFactory } from 'nest-commander'; +import { ImmichAdminModule } from 'src/apps/immich-admin.module'; +import { LogLevel } from 'src/entities/system-config.entity'; + +export async function bootstrapImmichAdmin() { + process.env.LOG_LEVEL = LogLevel.WARN; + await CommandFactory.run(ImmichAdminModule); +} diff --git a/server/src/immich-admin/app.module.ts b/server/src/apps/immich-admin.module.ts similarity index 95% rename from server/src/immich-admin/app.module.ts rename to server/src/apps/immich-admin.module.ts index b491fbb556..d0e5cab4cf 100644 --- a/server/src/immich-admin/app.module.ts +++ b/server/src/apps/immich-admin.module.ts @@ -18,4 +18,4 @@ import { InfraModule } from 'src/infra/infra.module'; ListUsersCommand, ], }) -export class AppModule {} +export class ImmichAdminModule {} diff --git a/server/src/microservices/main.ts b/server/src/apps/microservices.main.ts similarity index 86% rename from server/src/microservices/main.ts rename to server/src/apps/microservices.main.ts index 01949dd029..552fb714c8 100644 --- a/server/src/microservices/main.ts +++ b/server/src/apps/microservices.main.ts @@ -1,14 +1,14 @@ import { NestFactory } from '@nestjs/core'; +import { MicroservicesModule } from 'src/apps/microservices.module'; import { envName, serverVersion } from 'src/domain/domain.constant'; import { otelSDK } from 'src/infra/instrumentation'; import { ImmichLogger } from 'src/infra/logger'; import { WebSocketAdapter } from 'src/infra/websocket.adapter'; -import { MicroservicesModule } from 'src/microservices/microservices.module'; const logger = new ImmichLogger('ImmichMicroservice'); const port = Number(process.env.MICROSERVICES_PORT) || 3002; -export async function bootstrap() { +export async function bootstrapMicroservices() { otelSDK.start(); const app = await NestFactory.create(MicroservicesModule, { bufferLogs: true }); app.useLogger(app.get(ImmichLogger)); diff --git a/server/src/microservices/microservices.module.ts b/server/src/apps/microservices.module.ts similarity index 67% rename from server/src/microservices/microservices.module.ts rename to server/src/apps/microservices.module.ts index 6fc9f0890c..3ddfb28f3c 100644 --- a/server/src/microservices/microservices.module.ts +++ b/server/src/apps/microservices.module.ts @@ -1,14 +1,14 @@ import { Module, OnModuleInit } from '@nestjs/common'; +import { MicroservicesService } from 'src/apps/microservices.service'; import { DomainModule } from 'src/domain/domain.module'; import { InfraModule } from 'src/infra/infra.module'; -import { AppService } from 'src/microservices/app.service'; @Module({ imports: [InfraModule, DomainModule], - providers: [AppService], + providers: [MicroservicesService], }) export class MicroservicesModule implements OnModuleInit { - constructor(private appService: AppService) {} + constructor(private appService: MicroservicesService) {} async onModuleInit() { await this.appService.init(); diff --git a/server/src/microservices/app.service.ts b/server/src/apps/microservices.service.ts similarity index 99% rename from server/src/microservices/app.service.ts rename to server/src/apps/microservices.service.ts index 043ee9f03c..bfdc37576b 100644 --- a/server/src/microservices/app.service.ts +++ b/server/src/apps/microservices.service.ts @@ -17,7 +17,7 @@ import { UserService } from 'src/domain/user/user.service'; import { otelSDK } from 'src/infra/instrumentation'; @Injectable() -export class AppService { +export class MicroservicesService { constructor( private auditService: AuditService, private assetService: AssetService, diff --git a/server/src/immich-admin/main.ts b/server/src/immich-admin/main.ts deleted file mode 100755 index 4a561ec884..0000000000 --- a/server/src/immich-admin/main.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { CommandFactory } from 'nest-commander'; -import { LogLevel } from 'src/entities/system-config.entity'; -import { AppModule } from 'src/immich-admin/app.module'; - -export async function bootstrap() { - process.env.LOG_LEVEL = LogLevel.WARN; - await CommandFactory.run(AppModule); -} diff --git a/server/src/main.ts b/server/src/main.ts index d598751336..ee60c793c7 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -1,6 +1,6 @@ -import { bootstrap as admin } from 'src/immich-admin/main'; -import { bootstrap as server } from 'src/immich/main'; -import { bootstrap as microservices } from 'src/microservices/main'; +import { bootstrapApi } from 'src/apps/api.main'; +import { bootstrapImmichAdmin } from 'src/apps/immich-admin.main'; +import { bootstrapMicroservices } from 'src/apps/microservices.main'; const immichApp = process.argv[2] || process.env.IMMICH_APP; @@ -12,15 +12,15 @@ function bootstrap() { switch (immichApp) { case 'immich': { process.title = 'immich_server'; - return server(); + return bootstrapApi(); } case 'microservices': { process.title = 'immich_microservices'; - return microservices(); + return bootstrapMicroservices(); } case 'immich-admin': { process.title = 'immich_admin_cli'; - return admin(); + return bootstrapImmichAdmin(); } default: { throw new Error(`Invalid app name: ${immichApp}. Expected one of immich|microservices|cli`); diff --git a/server/src/test-utils/utils.ts b/server/test/utils.ts similarity index 93% rename from server/src/test-utils/utils.ts rename to server/test/utils.ts index 9b8ab059bf..777a6a00c1 100644 --- a/server/src/test-utils/utils.ts +++ b/server/test/utils.ts @@ -6,15 +6,15 @@ import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { EventEmitter } from 'node:stream'; import { Server } from 'node:tls'; +import { ApiModule } from 'src/apps/api.module'; +import { ApiService } from 'src/apps/api.service'; +import { MicroservicesService } from 'src/apps/microservices.service'; import { QueueName } from 'src/domain/job/job.constants'; -import { AppModule } from 'src/immich/app.module'; -import { AppService } from 'src/immich/app.service'; import { dataSource } from 'src/infra/database.config'; import { InfraModule, InfraTestModule } from 'src/infra/infra.module'; import { IJobRepository, JobItem, JobItemHandler } from 'src/interfaces/job.repository'; import { IMediaRepository } from 'src/interfaces/media.repository'; import { StorageEventType } from 'src/interfaces/storage.repository'; -import { AppService as MicroAppService } from 'src/microservices/app.service'; import { MediaRepository } from 'src/repositories/media.repository'; import { EntityTarget, ObjectLiteral } from 'typeorm'; @@ -106,8 +106,8 @@ let app: INestApplication; export const testApp = { create: async (): Promise => { const moduleFixture = await Test.createTestingModule({ - imports: [AppModule], - providers: [AppService, MicroAppService], + imports: [ApiModule], + providers: [ApiService, MicroservicesService], }) .overrideModule(InfraModule) .useModule(InfraTestModule) @@ -120,8 +120,8 @@ export const testApp = { app = await moduleFixture.createNestApplication().init(); await app.listen(0); await db.reset(); - await app.get(AppService).init(); - await app.get(MicroAppService).init(); + await app.get(ApiService).init(); + await app.get(MicroservicesService).init(); const port = app.getHttpServer().address().port; const protocol = app instanceof Server ? 'https' : 'http'; @@ -135,7 +135,7 @@ export const testApp = { get: (member: any) => app.get(member), teardown: async () => { if (app) { - await app.get(MicroAppService).teardown(); + await app.get(MicroservicesService).teardown(); await app.close(); } await db.disconnect();