diff --git a/package-lock.json b/package-lock.json index e6ac3a24..d86b1011 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2570,10 +2570,13 @@ } }, "@types/handlebars": { - "version": "4.0.40", - "resolved": "https://registry.npmjs.org/@types/handlebars/-/handlebars-4.0.40.tgz", - "integrity": "sha512-sGWNtsjNrLOdKha2RV1UeF8+UbQnPSG7qbe5wwbni0mw4h2gHXyPFUMOC+xwGirIiiydM/HSqjDO4rk6NFB18w==", - "dev": true + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@types/handlebars/-/handlebars-4.1.0.tgz", + "integrity": "sha512-gq9YweFKNNB1uFK71eRqsd4niVkXrxHugqWFQkeLRJvGjnxsLr16bYtcsG4tOFwmYi0Bax+wCkbf1reUfdl4kA==", + "dev": true, + "requires": { + "handlebars": "*" + } }, "@types/highlight.js": { "version": "9.12.3", @@ -7736,11 +7739,6 @@ "integrity": "sha1-C+4AUBiusmDQo6865ljdATbsG5k=", "dev": true }, - "foreachasync": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/foreachasync/-/foreachasync-3.0.0.tgz", - "integrity": "sha1-VQKYfchxS+M5IJfzLgBxyd7gfPY=" - }, "forever-agent": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", @@ -8919,28 +8917,6 @@ "minimalistic-assert": "^1.0.1" } }, - "hbs": { - "version": "4.0.4", - "resolved": "https://registry.npmjs.org/hbs/-/hbs-4.0.4.tgz", - "integrity": "sha512-esVlyV/V59mKkwFai5YmPRSNIWZzhqL5YMN0++ueMxyK1cCfPa5f6JiHtapPKAIVAhQR6rpGxow0troav9WMEg==", - "requires": { - "handlebars": "4.0.14", - "walk": "2.3.9" - }, - "dependencies": { - "handlebars": { - "version": "4.0.14", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.0.14.tgz", - "integrity": "sha512-E7tDoyAA8ilZIV3xDJgl18sX3M8xB9/fMw8+mfW4msLW8jlX97bAnWgT3pmaNXuvzIEgSBMnAHfuXsB2hdzfow==", - "requires": { - "async": "^2.5.0", - "optimist": "^0.6.1", - "source-map": "^0.6.1", - "uglify-js": "^3.1.4" - } - } - } - }, "he": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", @@ -15684,6 +15660,7 @@ "version": "0.6.1", "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz", "integrity": "sha1-2j6nRob6IaGaERwybpDrFaAZZoY=", + "dev": true, "requires": { "minimist": "~0.0.1", "wordwrap": "~0.0.2" @@ -15692,7 +15669,8 @@ "minimist": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", - "integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8=" + "integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8=", + "dev": true } } }, @@ -18328,7 +18306,8 @@ "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true }, "source-map-resolve": { "version": "0.5.2", @@ -19530,6 +19509,7 @@ "version": "3.4.9", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.4.9.tgz", "integrity": "sha512-8CJsbKOtEbnJsTyv6LE6m6ZKniqMiFWmm9sRbopbkGs3gMPPfd3Fh8iIA4Ykv5MgaTbqHr4BaoGLJLZNhsrW1Q==", + "dev": true, "requires": { "commander": "~2.17.1", "source-map": "~0.6.1" @@ -20139,14 +20119,6 @@ "browser-process-hrtime": "^0.1.2" } }, - "walk": { - "version": "2.3.9", - "resolved": "https://registry.npmjs.org/walk/-/walk-2.3.9.tgz", - "integrity": "sha1-MbTbZnjyrgHDnqn7hyWpAx5Vins=", - "requires": { - "foreachasync": "^3.0.0" - } - }, "walker": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/walker/-/walker-1.0.7.tgz", @@ -20573,7 +20545,8 @@ "wordwrap": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz", - "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc=" + "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc=", + "dev": true }, "worker-farm": { "version": "1.7.0", diff --git a/package.json b/package.json index 54ddc1bc..e907e289 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,6 @@ "fs-extra": "8.1.0", "github-release-notes": "0.17.0", "googleapis": "27.0.0", - "hbs": "4.0.4", "js-yaml": "3.13.1", "prom-client": "11.5.3", "reflect-metadata": "0.1.13", @@ -70,6 +69,7 @@ "@types/node": "11.13.17", "@types/supertest": "2.0.8", "@types/universal-analytics": "0.4.2", + "@types/handlebars": "4.1.0", "coveralls": "3.0.4", "cpx": "1.5.0", "husky": "3.0.0", diff --git a/src/controllers/application.controller.e2e.spec.ts b/src/controllers/application.controller.e2e.spec.ts index aa0e14dc..bc0dd9b3 100644 --- a/src/controllers/application.controller.e2e.spec.ts +++ b/src/controllers/application.controller.e2e.spec.ts @@ -20,8 +20,6 @@ describe('ApplicationController (e2e)', () => { }).compile(); app = moduleFixture.createNestApplication(); - app.setBaseViewsDir(join(__dirname, '..', 'views')); - app.setViewEngine('hbs'); await app.init(); }); diff --git a/src/controllers/application.controller.ts b/src/controllers/application.controller.ts index 5f457427..face3b7b 100644 --- a/src/controllers/application.controller.ts +++ b/src/controllers/application.controller.ts @@ -1,13 +1,16 @@ -import { Controller, Get, Res } from '@nestjs/common'; +import { Controller, Get, Res, Header, HttpStatus } from '@nestjs/common'; import { PrometheusService } from '../logger/prometheus.service'; +import { logger } from '../logger/logger.service'; +import { join } from 'path'; +import { Utils } from '../utils/utils'; @Controller() export class ApplicationController { constructor(private readonly prometheus: PrometheusService) {} @Get('/') - welcome(@Res() response): string { - return response.render('homepage.hbs'); + async welcome(@Res() response): Promise { + return response.status(HttpStatus.OK).send(Utils.renderHbs('homepage')); } @Get('/metrics') diff --git a/src/exceptions/allExceptionFilter.ts b/src/exceptions/allExceptionFilter.ts index 3a8b68a0..f77267de 100644 --- a/src/exceptions/allExceptionFilter.ts +++ b/src/exceptions/allExceptionFilter.ts @@ -4,6 +4,7 @@ import { HttpException, HttpStatus, } from '@nestjs/common'; +import { Utils } from '../utils/utils'; /** * Handle all ExceptionFilter @@ -21,7 +22,9 @@ export class AllExceptionsFilter implements ExceptionFilter { : HttpStatus.INTERNAL_SERVER_ERROR; if (status === HttpStatus.NOT_FOUND) { - return response.status(status).render('404.hbs', { path: request.url }); + return response + .status(status) + .send(Utils.renderHbs('404', { path: request.url })); } return response.status(status).json({ diff --git a/src/main.ts b/src/main.ts index 135ed421..24a2dfc4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,10 +15,6 @@ async function bootstrap() { // Handle every exceptions app.useGlobalFilters(new AllExceptionsFilter()); - // Set View Engine - app.setBaseViewsDir(join(__dirname, 'views')); - app.setViewEngine('hbs'); - const port = process.env.PORT || 3000; app.enableCors({ diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 8df62cc2..9f0a9fd1 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -6,6 +6,7 @@ import { logger } from '../logger/logger.service'; import * as Handlebars from 'handlebars'; import { PreconditionException } from '../exceptions/precondition.exception'; +import { join } from 'path'; Handlebars.registerHelper('foreach', (items, options) => { return items.map(item => options.fn(item)).join(','); }); @@ -189,4 +190,13 @@ export class Utils { const CryptoJS = require('crypto-js'); return CryptoJS.AES.encrypt(str, process.env.ENCRYPTION_KEY).toString(); } + + static renderHbs(filePath: string, dataSource: any = {}): string { + const fs = require('fs-extra'); + const path = join(__dirname, '../views/', `${filePath}.hbs`); + let template = fs.readFileSync(path); + template = template.toString(); + + return Utils.render(template, dataSource); + } }