Skip to content

Commit

Permalink
feat(export): separate running the app from boostrapping it
Browse files Browse the repository at this point in the history
this will allow apps to import achievibit and use it as a sub path
  • Loading branch information
thatkookooguy committed Dec 16, 2019
1 parent a9131e5 commit 5669934
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
50 changes: 50 additions & 0 deletions src/bootstrap-application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { readFileSync } from 'fs-extra';
import nunjucks from 'nunjucks';
import { join } from 'path';

import { AppModule } from '@kb-app';
import { ConfigService } from '@kb-config';
import { PackageDetailsDto } from '@kb-models';

export async function bootstrap(): Promise<NestExpressApplication> {
const config = new ConfigService();

const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useGlobalPipes(new ValidationPipe({
whitelist: true
}));

const packageDetails: PackageDetailsDto =
await app.get('AppService').getPackageDetails();

const options = new DocumentBuilder()
.setTitle(packageDetails.name)
.setDescription('The achievibit API description')
.setVersion(packageDetails.version)
.build();

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

SwaggerModule.setup('api/docs', app, document, {
customSiteTitle: `kibibit - achievibit API documentation`,
customCss: readFileSync(join(__dirname, '../public/swagger.css'), 'utf8')
// customJs: '../swagger-things/swagger.js',
// customfavIcon: '../swagger-things/favicon-32.png'
});

app.useStaticAssets(join(__dirname, '..', 'public'), { prefix: '/public/' });
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('njk');
nunjucks.configure('views', {
autoescape: true,
express: app,
watch: true
});

return app;
}
53 changes: 3 additions & 50 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,4 @@
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { readFileSync } from 'fs-extra';
import nunjucks from 'nunjucks';
import { join } from 'path';
import { bootstrap } from './bootstrap-application';

import { AppModule } from '@kb-app';
import { ConfigService } from '@kb-config';
import { PackageDetailsDto } from '@kb-models';

async function bootstrap() {
const config = new ConfigService();

const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useGlobalPipes(new ValidationPipe({
whitelist: true
}));

const packageDetails: PackageDetailsDto =
await app.get('AppService').getPackageDetails();

const options = new DocumentBuilder()
.setTitle(packageDetails.name)
.setDescription('The achievibit API description')
.setVersion(packageDetails.version)
.build();

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

SwaggerModule.setup('api/docs', app, document, {
customSiteTitle: `kibibit - achievibit API documentation`,
customCss: readFileSync(join(__dirname, '../public/swagger.css'), 'utf8')
// customJs: '../swagger-things/swagger.js',
// customfavIcon: '../swagger-things/favicon-32.png'
});

app.useStaticAssets(join(__dirname, '..', 'public'), { prefix: '/public/' });
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('njk');
nunjucks.configure('views', {
autoescape: true,
express: app,
watch: true
});

await app.listen(config.port);
}
bootstrap();
bootstrap()
.then((app) => app.listen(10101));

0 comments on commit 5669934

Please sign in to comment.