-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Brints/feat/environment_configurations
feat: environment configurations
- Loading branch information
Showing
8 changed files
with
237 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { AuthModule } from './auth/auth.module'; | ||
import { UsersModule } from './users/users.module'; | ||
// import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { AuthModule } from './auth/auth.module'; | ||
import { environmentValidationSchema } from './config/environment.validation'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
isGlobal: true, | ||
envFilePath: !process.env.NODE_ENV | ||
? '.env' | ||
: `.env.${process.env.NODE_ENV}`, | ||
validationSchema: environmentValidationSchema, | ||
}), | ||
TypeOrmModule.forRootAsync({ | ||
inject: [ConfigService], | ||
useFactory: (configService: ConfigService) => ({ | ||
type: 'postgres', | ||
host: configService.get('DB_HOST'), | ||
port: configService.get('DB_PORT'), | ||
username: configService.get('DB_USER'), | ||
password: configService.get('DB_PASSWORD'), | ||
database: configService.get('DB_NAME'), | ||
autoLoadEntities: process.env.NODE_ENV === 'development', | ||
synchronize: process.env.NODE_ENV === 'development', | ||
entities: [__dirname + '/**/*.entity{.ts,.js}'], | ||
// entities: [User, UserAuth], | ||
}), | ||
}), | ||
AuthModule, | ||
UsersModule, | ||
// TypeOrmModule.forRootAsync({ | ||
// useFactory: () => ({ | ||
// type: 'postgres', | ||
// host: 'localhost', | ||
// port: 5432, | ||
// username: 'postgres', | ||
// password: 'postgres', | ||
// database: 'brints-estate-backend', | ||
// synchronize: true, | ||
// autoLoadEntities: true, | ||
// }) | ||
// }) | ||
], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class AppConfigService { | ||
constructor(private readonly configService: ConfigService) {} | ||
|
||
getConfig() { | ||
return { | ||
database: { | ||
dbHost: this.configService.get<string>('DB_HOST') as string, | ||
dbPort: this.configService.get<number>('DB_PORT') as number, | ||
dbUser: this.configService.get<string>('DB_USER') as string, | ||
dbPassword: this.configService.get<string>('DB_PASSWORD') as string, | ||
dbName: this.configService.get<string>('DB_NAME') as string, | ||
}, | ||
app: { | ||
port: this.configService.get<number>('APP_PORT') as number, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; | ||
|
||
export function swaggerInitializer(app: INestApplication<any>) { | ||
const config = new DocumentBuilder() | ||
.setTitle('Brints Estate API') | ||
.setDescription( | ||
'The modern day Real Estate API simplifying the way we buy and sell properties', | ||
) | ||
.addServer('http://localhost:3001', 'Development Server') | ||
.addServer('https://brints-estate-api.herokuapp.com', 'Production Server') | ||
.setTermsOfService('http://localhost:3001/terms') | ||
.setLicense( | ||
'MIT', | ||
'https://github.com/Brints/nestjs-brints-group-estate/blob/main/LICENSE', | ||
) | ||
.setVersion('1.0') | ||
.build(); | ||
const document = SwaggerModule.createDocument(app, config); | ||
SwaggerModule.setup('api-docs', app, document); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// import Joi from '@hapi/joi'; | ||
import * as Joi from 'joi'; | ||
|
||
// export const environmentValidationSchema = Joi.object({ | ||
// APP_PORT: Joi.number().default(8000), | ||
// DB_HOST: Joi.string().required(), | ||
// DB_PORT: Joi.number().default(5432), | ||
// DB_USER: Joi.string().required(), | ||
// DB_PASSWORD: Joi.string().required(), | ||
// DB_NAME: Joi.string().required(), | ||
// NODE_ENV: Joi.string() | ||
// .required() | ||
// .valid('development', 'production', 'test') | ||
// .default('development'), | ||
// }); | ||
|
||
export const environmentValidationSchema = Joi.object({ | ||
APP_PORT: Joi.number().port().default(8000), | ||
DB_HOST: Joi.string().required(), | ||
DB_PORT: Joi.number().port().default(5432), | ||
DB_USER: Joi.string().required(), | ||
DB_PASSWORD: Joi.string().required(), | ||
DB_NAME: Joi.string().required(), | ||
NODE_ENV: Joi.string() | ||
.required() | ||
.valid('development', 'production', 'test', 'staging') | ||
.default('development'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,20 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Logger } from '@nestjs/common'; | ||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; | ||
|
||
import { AppModule } from './app.module'; | ||
import { initializeDataSource } from './database/data-source'; | ||
import { swaggerInitializer } from './config/config.swagger'; | ||
|
||
async function bootstrap() { | ||
await initializeDataSource(); | ||
const app = await NestFactory.create(AppModule); | ||
const configService = new ConfigService(); | ||
|
||
const port = configService.get('APP_PORT'); | ||
const logger = new Logger(); | ||
const app = await NestFactory.create(AppModule); | ||
|
||
/** | ||
* Swagger configuration | ||
*/ | ||
const config = new DocumentBuilder() | ||
.setTitle('Brints Estate API') | ||
.setDescription( | ||
'The modern day Real Estate API simplifying the way we buy and sell properties', | ||
) | ||
.addServer('http://localhost:3001', 'Development Server') | ||
.addServer('https://brints-estate-api.herokuapp.com', 'Production Server') | ||
.setTermsOfService('http://localhost:3001/terms') | ||
.setLicense( | ||
'MIT', | ||
'https://github.com/Brints/nestjs-brints-group-estate/blob/main/LICENSE', | ||
) | ||
.setVersion('1.0') | ||
.build(); | ||
const document = SwaggerModule.createDocument(app, config); | ||
SwaggerModule.setup('api-docs', app, document); | ||
swaggerInitializer(app); | ||
|
||
await app.listen(3001); | ||
logger.log('Application started on http://localhost:3001'); | ||
await app.listen(port); | ||
logger.log(`Application is running on ${await app.getUrl()}`); | ||
} | ||
bootstrap(); |
Oops, something went wrong.