Newer
Older
import { Body, Controller, Get, Header, Logger, Param, Post, Res } from '@nestjs/common';
import { version } from '../package.json';
import { Response } from 'express';
import { generatePDF, renderPage } from './shared/utils';
import { GetPdfDto } from './users/dto/get-pdf.dto';
import { ConfigurationService } from './configuration/configuration.service';
private logger = new Logger(AppController.name);
constructor(private configurationService: ConfigurationService) {}
const now = Date.now();
return {
status: 'API Online',
uptime: Number((now - this.start) / 1000).toFixed(0),
@Get('config')
async config() {
// temp IS_OPENSHIFT: on appsoc servers, GHOST_HOST_AND_PORT is local network server name
return {
ghostAdminUrl:
((process.env.IS_OPENSHIFT ? process.env.GHOST_HOST_AND_PORT : process.env.GHOST_URL_APPSOC) ||
'http://localhost:2368') + '/ghost/',
matomoTrackerUrl: process.env.MATOMO_URL,
matomoSiteId: process.env.MATOMO_SITEID,
};
}
/** App URL which can be called from the chrome container (used to get pdf or prerender pages) */
private getAppUrlToBeCalled(): string {
let appUrl;
if (this.configurationService.isLocalConf()) {
// In local dev, use the docker host machine's IP to make the angular app accessible from the chrome container
appUrl = 'http://' + (process.env.DOCKER_HOST_IP || '172.17.0.1') + ':4200';
} else if (process.env.IS_OPENSHIFT !== 'true') {
if (process.env.NODE_ENV === 'rec') {
appUrl = 'http://web-app-rec:8080';
} else {
appUrl = 'http://web-app:8080';
}
} else {
appUrl = this.configurationService.appUrl;
}
@Post('/pdf')
@Header('Content-Type', 'application/pdf')
@Header('Cache-Control', 'no-cache, no-store, must-revalidate')
@Header('Pragma', 'no-cache')
@Header('Expires', '0')
async getPdf(@Body() body: GetPdfDto, @Res() res: Response): Promise<void> {
this.logger.log('getPdf: ' + JSON.stringify(body));
const appUrl = this.getAppUrlToBeCalled();
const url = `${appUrl}/${body.urlPath}`;
this.logger.log('url=' + url);
const buffer = await generatePDF(url);
res.set({
'Content-Disposition': `attachment; filename=${body.fileName}`,
'Content-Length': buffer.length,
});
res.end(buffer);
}
@ApiParam({ name: 'page', type: String, required: true })
@Header('Content-Type', 'text/html')
@Header('Cache-Control', 'no-cache, no-store, must-revalidate')
@Header('Pragma', 'no-cache')
@Header('Expires', '0')
async renderUrl(@Param() params: any, @Res() res: Response): Promise<void> {
// If the page is not starting with a slash, add it
const page = params[0].startsWith('/') ? params[0] : '/' + params[0];
this.logger.log('renderUrl: ' + page);
const appUrl = this.getAppUrlToBeCalled();
const url = `${appUrl}${page}`;
this.logger.log('url=' + url);