Skip to content

Commit

Permalink
refactor api configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 7, 2024
1 parent 5f70ee9 commit b2f4e2a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 33 deletions.
35 changes: 2 additions & 33 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { join } from 'path';
import { TypeOrmModule } from '@nestjs/typeorm';
import * as process from 'node:process';
import { ApiConfigModule } from '@api/modules/config/app-config.module';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
envFilePath: [
join(__dirname, `../../shared/config/.env.${process.env.NODE_ENV}`),
join(__dirname, '../../shared/config/.env'),
],
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
// TODO: Move this to config service method
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
database: configService.get('DB_NAME'),
entities: [join(__dirname, '**', '*.entity.{ts,js}')],
synchronize: true,
ssl:
process.env.NODE_ENV === 'production'
? { require: true, rejectUnauthorized: false }
: false,
}),
inject: [ConfigService],
}),
],
imports: [ApiConfigModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';

@Controller('authentication')
export class AuthenticationController {}
9 changes: 9 additions & 0 deletions api/src/modules/auth/authentication/authentication.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AuthenticationService } from './authentication.service';
import { AuthenticationController } from './authentication.controller';

@Module({
providers: [AuthenticationService],
controllers: [AuthenticationController],
})
export class AuthenticationModule {}
4 changes: 4 additions & 0 deletions api/src/modules/auth/authentication/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AuthenticationService {}
4 changes: 4 additions & 0 deletions api/src/modules/auth/authorisation/authorisation.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Module } from '@nestjs/common';

@Module({})
export class AuthorisationModule {}
29 changes: 29 additions & 0 deletions api/src/modules/config/app-config.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Global, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { join } from 'path';
import { ApiConfigService } from '@api/modules/config/app-config.service';
import { DatabaseModule } from '@api/modules/config/database/database.module';

@Global()
@Module({
imports: [
DatabaseModule,
/**
* @note: Check if we can abstract the conf to ApiConfigService
*/
ConfigModule.forRoot({
isGlobal: true,
cache: true,
envFilePath: [
join(
__dirname,
`../../../../shared/config/.env.${process.env.NODE_ENV}`,
),
join(__dirname, '../../../../shared/config/.env'),
],
}),
],
providers: [ConfigService, ApiConfigService],
exports: [ApiConfigService, DatabaseModule],
})
export class ApiConfigModule {}
31 changes: 31 additions & 0 deletions api/src/modules/config/app-config.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { join } from 'path';

@Injectable()
export class ApiConfigService {
constructor(private configService: ConfigService) {}

/**
* @note We could abstract this to a data layer access config specific class within database module, as well for other configs when the thing gets more complex.
* we could also abstract the underlying engine type, which is now set in the main app module
*/
getDatabaseConfig() {
return {
host: this.configService.get('DB_HOST'),
port: this.configService.get('DB_PORT'),
username: this.configService.get('DB_USERNAME'),
password: this.configService.get('DB_PASSWORD'),
database: this.configService.get('DB_NAME'),
entities: [join(__dirname, '**', '*.entity.{ts,js}')],
synchronize: true,
ssl: this.isProduction()
? { require: true, rejectUnauthorized: false }
: false,
};
}

private isProduction(): boolean {
return this.configService.get('NODE_ENV') === 'production';
}
}
20 changes: 20 additions & 0 deletions api/src/modules/config/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { forwardRef, Module } from '@nestjs/common';
import { ApiConfigService } from '@api/modules/config/app-config.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ApiConfigModule } from '@api/modules/config/app-config.module';

@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [forwardRef(() => ApiConfigModule)],

useFactory: (config: ApiConfigService) => ({
...config.getDatabaseConfig(),
type: 'postgres',
}),
inject: [ApiConfigService],
}),
],
exports: [TypeOrmModule],
})
export class DatabaseModule {}

0 comments on commit b2f4e2a

Please sign in to comment.