import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { join } from 'path';

import { NestExpressApplication } from '@nestjs/platform-express';
import { Transport } from '@nestjs/microservices';
import { json, urlencoded } from 'express';

export let app: any
const app_port = process.env.QueryModuleUrlPort;
async function bootstrap() {
  const fs = require('fs');
  //const keyFile = fs.readFileSync('src/SSL/localhost.decrypted.key');
  //const certFile = fs.readFileSync('src/SSL/localhost.crt');

  app = await NestFactory.create<NestExpressApplication>(AppModule);
  // app = await NestFactory.create<NestExpressApplication>(AppModule, {
  //   httpsOptions: {
  //     key: keyFile,
  //     cert: certFile,
  //   }
  // });

  app.connectMicroservice({
    transport: Transport.TCP,
    options: {
      port: process.env.QueryModuleInternalUrlPort
    },
  });
  await app.startAllMicroservices();


  app.useStaticAssets(join(__dirname, '..', 'public'));
  app.setBaseViewsDir(join(__dirname, '..', 'views'));
  app.setViewEngine('jade');
  app.use(json({ limit: '50mb' }));
  app.use(urlencoded({ extended: true, limit: '50mb' }));

  await app.listen(app_port);
  process.env.LOCALHOST = await app.getUrl();
  //"http://client.localhost1.com:3091";
  //await app.getUrl();
  console.log(`🚀 Check Login page at ${process.env.LOCALHOST}/graphqlapi`);

  console.log(`Server ${process.env.SERVERNAME} started`);
  console.log(`Act as supergraph: ${process.env.ACT_AS_SUPERGRAPH}`);
}
bootstrap();
