Skip to content
Snippets Groups Projects
app.controller.ts 3.19 KiB
Newer Older
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
import { Body, Controller, Get, Header, Logger, Param, Post, Res } from '@nestjs/common';
import { version } from '../package.json';
import { Response } from 'express';
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
import { generatePDF, renderPage } from './shared/utils';
import { GetPdfDto } from './users/dto/get-pdf.dto';
import { ConfigurationService } from './configuration/configuration.service';
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
import { ApiParam } from '@nestjs/swagger';
Hugo SUBTIL's avatar
Hugo SUBTIL committed
@Controller()
export class AppController {
Bastien DUMONT's avatar
Bastien DUMONT committed
  private start = Date.now();
  private logger = new Logger(AppController.name);

  constructor(private configurationService: ConfigurationService) {}
Hugo SUBTIL's avatar
Hugo SUBTIL committed

  @Get('healthcheck')
Bastien DUMONT's avatar
Bastien DUMONT committed
  healthcheck() {
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    const now = Date.now();
    return {
      status: 'API Online',
      uptime: Number((now - this.start) / 1000).toFixed(0),
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    };
Hugo SUBTIL's avatar
Hugo SUBTIL committed
  }

  @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,
    };
  }
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
  /** 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;
    }
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
    return appUrl;
  }
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
  @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);
  }
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed

Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
  @Get('/render/*')
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
  @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')
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
  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);
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed

Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
    const appUrl = this.getAppUrlToBeCalled();
    const url = `${appUrl}${page}`;
    this.logger.log('url=' + url);
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed

Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
    const html = await renderPage(url);
Etienne LOUPIAS's avatar
Etienne LOUPIAS committed
    res.send(html);
  }
Hugo SUBTIL's avatar
Hugo SUBTIL committed
}