Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move apps and test utils #8129

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/e2e/jobs/specs/library-watcher.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
restoreTempFolder,
testApp,
waitForEvent,
} from 'src/test-utils/utils';
} from 'test/utils';

describe(`Library watcher (e2e)`, () => {
let server: any;
Expand Down
2 changes: 1 addition & 1 deletion server/e2e/jobs/specs/library.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)`, () => {
Expand Down
10 changes: 5 additions & 5 deletions server/src/immich/main.ts → server/src/apps/api.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<NestExpressApplication>(AppModule, { bufferLogs: true });
const app = await NestFactory.create<NestExpressApplication>(ApiModule, { bufferLogs: true });

app.useLogger(app.get(ImmichLogger));
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions server/src/apps/immich-admin.main.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ import { InfraModule } from 'src/infra/infra.module';
ListUsersCommand,
],
})
export class AppModule {}
export class ImmichAdminModule {}
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions server/src/immich-admin/main.ts

This file was deleted.

12 changes: 6 additions & 6 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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`);
Expand Down
16 changes: 8 additions & 8 deletions server/src/test-utils/utils.ts → server/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -106,8 +106,8 @@ let app: INestApplication;
export const testApp = {
create: async (): Promise<INestApplication> => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
providers: [AppService, MicroAppService],
imports: [ApiModule],
providers: [ApiService, MicroservicesService],
})
.overrideModule(InfraModule)
.useModule(InfraTestModule)
Expand All @@ -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';
Expand All @@ -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();
Expand Down
Loading