From 19a1f5e12dcb46e4e4044b3d9260cae512396453 Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Thu, 2 May 2024 16:45:05 +0100 Subject: [PATCH] cleanups --- apps/server/src/app.module.ts | 8 +++ apps/server/src/core/core.module.ts | 9 --- apps/server/src/helpers/index.ts | 1 + .../environment/environment.module.ts | 2 +- .../environment/environment.service.ts | 6 +- .../mail/providers/mail.provider.ts | 55 +++++++++---------- .../storage/providers/storage.provider.ts | 53 +++++++++--------- 7 files changed, 67 insertions(+), 67 deletions(-) create mode 100644 apps/server/src/helpers/index.ts diff --git a/apps/server/src/app.module.ts b/apps/server/src/app.module.ts index fcd040cd..a0bf01cb 100644 --- a/apps/server/src/app.module.ts +++ b/apps/server/src/app.module.ts @@ -9,6 +9,8 @@ import { ServeStaticModule } from '@nestjs/serve-static'; import { join } from 'path'; import { DatabaseModule } from '@docmost/db/database.module'; import * as fs from 'fs'; +import { StorageModule } from './integrations/storage/storage.module'; +import { MailModule } from './integrations/mail/mail.module'; const clientDistPath = join(__dirname, '..', '..', 'client/dist'); @@ -31,6 +33,12 @@ function getServeStaticModule() { CollaborationModule, WsModule, ...getServeStaticModule(), + StorageModule.forRootAsync({ + imports: [EnvironmentModule], + }), + MailModule.forRootAsync({ + imports: [EnvironmentModule], + }), ], controllers: [AppController], providers: [AppService], diff --git a/apps/server/src/core/core.module.ts b/apps/server/src/core/core.module.ts index db10e3a6..94a29f5a 100644 --- a/apps/server/src/core/core.module.ts +++ b/apps/server/src/core/core.module.ts @@ -8,16 +8,13 @@ import { UserModule } from './user/user.module'; import { AuthModule } from './auth/auth.module'; import { WorkspaceModule } from './workspace/workspace.module'; import { PageModule } from './page/page.module'; -import { StorageModule } from '../integrations/storage/storage.module'; import { AttachmentModule } from './attachment/attachment.module'; -import { EnvironmentModule } from '../integrations/environment/environment.module'; import { CommentModule } from './comment/comment.module'; import { SearchModule } from './search/search.module'; import { SpaceModule } from './space/space.module'; import { GroupModule } from './group/group.module'; import { CaslModule } from './casl/casl.module'; import { DomainMiddleware } from '../middlewares/domain.middleware'; -import { MailModule } from '../integrations/mail/mail.module'; @Module({ imports: [ @@ -25,12 +22,6 @@ import { MailModule } from '../integrations/mail/mail.module'; AuthModule, WorkspaceModule, PageModule, - StorageModule.forRootAsync({ - imports: [EnvironmentModule], - }), - MailModule.forRootAsync({ - imports: [EnvironmentModule], - }), AttachmentModule, CommentModule, SearchModule, diff --git a/apps/server/src/helpers/index.ts b/apps/server/src/helpers/index.ts new file mode 100644 index 00000000..04bca77e --- /dev/null +++ b/apps/server/src/helpers/index.ts @@ -0,0 +1 @@ +export * from './utils'; diff --git a/apps/server/src/integrations/environment/environment.module.ts b/apps/server/src/integrations/environment/environment.module.ts index 7c6acfa2..2fcee965 100644 --- a/apps/server/src/integrations/environment/environment.module.ts +++ b/apps/server/src/integrations/environment/environment.module.ts @@ -2,7 +2,7 @@ import { Global, Module } from '@nestjs/common'; import { EnvironmentService } from './environment.service'; import { ConfigModule } from '@nestjs/config'; import { validate } from './environment.validation'; -import { envPath } from '../../helpers/utils'; +import { envPath } from '../../helpers'; @Global() @Module({ diff --git a/apps/server/src/integrations/environment/environment.service.ts b/apps/server/src/integrations/environment/environment.service.ts index c1b5cc60..d7466378 100644 --- a/apps/server/src/integrations/environment/environment.service.ts +++ b/apps/server/src/integrations/environment/environment.service.ts @@ -9,8 +9,8 @@ export class EnvironmentService { return this.configService.get('NODE_ENV'); } - getPort(): string { - return this.configService.get('PORT'); + getPort(): number { + return parseInt(this.configService.get('PORT')); } getDatabaseURL(): string { return this.configService.get('DATABASE_URL'); @@ -80,7 +80,7 @@ export class EnvironmentService { } getMailPort(): number { - return this.configService.get('MAIL_PORT'); + return parseInt(this.configService.get('MAIL_PORT')); } getMailUsername(): string { diff --git a/apps/server/src/integrations/mail/providers/mail.provider.ts b/apps/server/src/integrations/mail/providers/mail.provider.ts index 22acffde..bf140759 100644 --- a/apps/server/src/integrations/mail/providers/mail.provider.ts +++ b/apps/server/src/integrations/mail/providers/mail.provider.ts @@ -24,37 +24,36 @@ export const mailDriverConfigProvider = { useFactory: async (environmentService: EnvironmentService) => { const driver = environmentService.getMailDriver().toLocaleLowerCase(); - if (driver === MailOption.SMTP) { - return { - driver, - config: { - host: environmentService.getMailHost(), - port: environmentService.getMailPort(), - connectionTimeout: 30 * 1000, // 30 seconds - auth: { - user: environmentService.getMailUsername(), - pass: environmentService.getMailPassword(), - }, - } as SMTPTransport.Options, - }; - } + switch (driver) { + case MailOption.SMTP: + return { + driver, + config: { + host: environmentService.getMailHost(), + port: environmentService.getMailPort(), + connectionTimeout: 30 * 1000, // 30 seconds + auth: { + user: environmentService.getMailUsername(), + pass: environmentService.getMailPassword(), + }, + } as SMTPTransport.Options, + }; - if (driver === MailOption.Postmark) { - return { - driver, - config: { - postmarkToken: environmentService.getPostmarkToken(), - } as PostmarkConfig, - }; - } + case MailOption.Postmark: + return { + driver, + config: { + postmarkToken: environmentService.getPostmarkToken(), + } as PostmarkConfig, + }; - if (driver === MailOption.Log) { - return { - driver, - }; + case MailOption.Log: + return { + driver, + }; + default: + throw new Error(`Unknown mail driver: ${driver}`); } - - throw new Error(`Unknown mail driver: ${driver}`); }, inject: [EnvironmentService], diff --git a/apps/server/src/integrations/storage/providers/storage.provider.ts b/apps/server/src/integrations/storage/providers/storage.provider.ts index 42fec5d5..011d93a5 100644 --- a/apps/server/src/integrations/storage/providers/storage.provider.ts +++ b/apps/server/src/integrations/storage/providers/storage.provider.ts @@ -26,34 +26,35 @@ function createStorageDriver(disk: StorageConfig): StorageDriver { export const storageDriverConfigProvider = { provide: STORAGE_CONFIG_TOKEN, useFactory: async (environmentService: EnvironmentService) => { - const driver = environmentService.getStorageDriver(); + const driver = environmentService.getStorageDriver().toLowerCase(); - if (driver === StorageOption.LOCAL) { - return { - driver, - config: { - storagePath: - process.cwd() + '/' + environmentService.getLocalStoragePath(), - }, - }; - } - - if (driver === StorageOption.S3) { - return { - driver, - config: { - region: environmentService.getAwsS3Region(), - endpoint: environmentService.getAwsS3Endpoint(), - bucket: environmentService.getAwsS3Bucket(), - credentials: { - accessKeyId: environmentService.getAwsS3AccessKeyId(), - secretAccessKey: environmentService.getAwsS3SecretAccessKey(), + switch (driver) { + case StorageOption.LOCAL: + return { + driver, + config: { + storagePath: + process.cwd() + '/' + environmentService.getLocalStoragePath(), }, - }, - }; - } + }; - throw new Error(`Unknown storage driver: ${driver}`); + case StorageOption.S3: + return { + driver, + config: { + region: environmentService.getAwsS3Region(), + endpoint: environmentService.getAwsS3Endpoint(), + bucket: environmentService.getAwsS3Bucket(), + credentials: { + accessKeyId: environmentService.getAwsS3AccessKeyId(), + secretAccessKey: environmentService.getAwsS3SecretAccessKey(), + }, + }, + }; + + default: + throw new Error(`Unknown storage driver: ${driver}`); + } }, inject: [EnvironmentService], @@ -61,6 +62,6 @@ export const storageDriverConfigProvider = { export const storageDriverProvider = { provide: STORAGE_DRIVER_TOKEN, - useFactory: (config) => createStorageDriver(config), + useFactory: (config: StorageConfig) => createStorageDriver(config), inject: [STORAGE_CONFIG_TOKEN], };