Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
added api controller
modified data source for cloud based postgres
added ssl config to data source
ssl defaults to false
cleaned up health controller
fixed lint errors
  • Loading branch information
Prudent Bird committed Jul 24, 2024
1 parent 85af481 commit f3eaabf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
17 changes: 17 additions & 0 deletions src/api.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller, Get } from '@nestjs/common';
import { skipAuth } from './helpers/skipAuth';

@Controller()
export default class ApiController {
@skipAuth()
@Get('api')
public home() {
return { status_code: 200, message: 'Welcome to NestJs Backend Endpoint' };
}

@skipAuth()
@Get('api/v1')
public v1() {
return { status_code: 200, message: 'Welcome to version 1 of NestJS Backend Endpoint' };
}
}
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import dataSource from './database/data-source';
import { SeedingModule } from './database/seeding/seeding.module';
import HealthController from './health.controller';
import ApiController from './api.controller';
import { AuthModule } from './modules/auth/auth.module';
import { UserModule } from './modules/user/user.module';
import authConfig from '../config/auth.config';
Expand Down Expand Up @@ -65,6 +66,6 @@ import { AuthGuard } from './guards/auth.guard';
UserModule,
OrganisationsModule,
],
controllers: [HealthController],
controllers: [HealthController, ApiController],
})
export class AppModule {}
1 change: 1 addition & 0 deletions src/database/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const dataSource = new DataSource({
migrations: [process.env.DB_MIGRATIONS],
synchronize: isDevelopment,
migrationsTableName: 'migrations',
ssl: process.env.DB_SSL === 'true',
});

export async function initializeDataSource() {
Expand Down
4 changes: 2 additions & 2 deletions src/health.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export default class HealthController {
@skipAuth()
@Get('/')
public home() {
return { status_code: 200, message: 'Welcome to NestJs Backend Endpoint ......' };
return { status_code: 200, message: 'Welcome to NestJs Backend Endpoint' };
}

@skipAuth()
@Get('health')
public health() {
return 'healthy endpoint';
return { status_code: 200, message: 'This is a healthy endpoint' };
}
}
66 changes: 37 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,50 @@ import dataSource, { initializeDataSource } from './database/data-source';
import { SeedingService } from './database/seeding/seeding.service';

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, { bufferLogs: true });
try {
const app = await NestFactory.create<NestExpressApplication>(AppModule, { bufferLogs: true });

const logger = app.get(Logger);
const logger = app.get(Logger);

const dataSource = app.get(DataSource);
const dataSource = app.get(DataSource);

try {
await initializeDataSource();
console.log('Data Source has been initialized!');
} catch (err) {
console.error('Error during Data Source initialization', err);
process.exit(1);
}
try {
await initializeDataSource();
console.log('Data Source has been initialized!');
} catch (err) {
console.error('Error during Data Source initialization', err);
process.exit(1);
}

const seedingService = app.get(SeedingService);
await seedingService.seedDatabase();
const seedingService = app.get(SeedingService);
await seedingService.seedDatabase();

app.enable('trust proxy');
app.useLogger(logger);
app.enableCors();
app.setGlobalPrefix('api/v1', { exclude: ["/", "health"] });
app.enable('trust proxy');
app.useLogger(logger);
app.enableCors();
app.setGlobalPrefix('api/v1', { exclude: ['/', 'health', 'api', 'api/v1'] });

// TODO: set options for swagger docs
const options = new DocumentBuilder()
.setTitle('<project-title-here>')
.setDescription('<project-description-here>')
.setVersion('1.0')
.addBearerAuth()
.build();
// TODO: set options for swagger docs
const options = new DocumentBuilder()
.setTitle('<project-title-here>')
.setDescription('<project-description-here>')
.setVersion('1.0')
.addBearerAuth()
.build();

const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api/docs', app, document);

const port = app.get<ConfigService>(ConfigService).get<number>('server.port');
await app.listen(port);
const port = app.get<ConfigService>(ConfigService).get<number>('server.port');
await app.listen(port);

logger.log({ message: 'server started 🚀', port, url: `http://localhost:${port}/api` });
logger.log({ message: 'server started 🚀', port, url: `http://localhost:${port}/api/v1` });
} catch (err) {
console.error('Error during bootstrap', err);
process.exit(1);
}
}
bootstrap();
bootstrap().catch(err => {
console.error('Error during bootstrap', err);
process.exit(1);
});

0 comments on commit f3eaabf

Please sign in to comment.