diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 41a49d6..8d590c9 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -3,11 +3,15 @@ import { AppController } from './app.controller'; import { AppService } from './app.service'; import { PostsModule } from './posts/posts.module'; import { DatabaseModule } from './database/database.module'; +import configuration from './config/configuration'; @Module({ imports: [ + ConfigModule.forRoot({ + load: [configuration], + }), + DatabaseModule, PostsModule, - DatabaseModule ], controllers: [AppController], providers: [AppService], diff --git a/backend/src/config/configuration.ts b/backend/src/config/configuration.ts new file mode 100644 index 0000000..2c2e6e7 --- /dev/null +++ b/backend/src/config/configuration.ts @@ -0,0 +1,10 @@ +export default () => ({ + port: parseInt(process.env.BLOG_PORT, 10) || 3000, + database: { + host: process.env.POSTGRES_HOST, + port: parseInt(process.env.POSTGRES_PORT, 10) || 5432, + name: process.env.BLOG_DB_NAME, + user: process.env.BLOG_DB_USER, + password: process.env.BLOG_DB_PASSWORD, + }, +}); diff --git a/backend/src/database/database.module.ts b/backend/src/database/database.module.ts index eec3393..aa0e51c 100644 --- a/backend/src/database/database.module.ts +++ b/backend/src/database/database.module.ts @@ -1,19 +1,22 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { ConfigModule, ConfigService } from '@nestjs/config'; +import configuration from './config/configuration'; @Module({ imports: [ - ConfigModule.forRoot(), + ConfigModule.forRoot({ + load: [configuration] + }), TypeOrmModule.forRootAsync({ imports: [ConfigModule], useFactory: (configService: ConfigService) => ({ type: 'postgres', - host: configService.get('POSTGRES_HOST'), - port: configService.get('POSTGRES_PORT'), - username: configService.get('BLOG_DB_USER'), - password: configService.get('BLOG_DB_PASSWORD'), - database: configService.get('BLOG_DB_NAME'), + host: configService.get('database.host'), + port: configService.get('database.port'), + username: configService.get('database.user'), + password: configService.get('database.password'), + database: configService.get('database.name'), entities: [__dirname + '/../**/*.entity{.ts,.js}'], synchronize: true, // Be cautious about using synchronize in production }), diff --git a/backend/src/main.ts b/backend/src/main.ts index 4d34b3b..be7d74e 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -6,4 +6,5 @@ async function bootstrap() { app.enableCors(); await app.listen(process.env.PORT ?? 3000); } -bootstrap(); + +await bootstrap(); diff --git a/backend/src/posts/post.entity.ts b/backend/src/posts/post.entity.ts index bf9ba25..4fcf073 100644 --- a/backend/src/posts/post.entity.ts +++ b/backend/src/posts/post.entity.ts @@ -1,7 +1,7 @@ -export class Post { - id: number; - title: string; - uri: string; - body: string; - created: Date; -} +export class Post { + id: number; + title: string; + uri: string; + body: string; + created: Date; +} diff --git a/backend/src/posts/posts.module.ts b/backend/src/posts/posts.module.ts index 25cf794..aba4b75 100644 --- a/backend/src/posts/posts.module.ts +++ b/backend/src/posts/posts.module.ts @@ -4,6 +4,6 @@ import { PostsService } from './posts.service'; @Module({ controllers: [PostsController], - providers: [PostsService] + providers: [PostsService], }) export class PostsModule {}