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';

process.argv.forEach(function (val, index, array) {
  console.log(index + ': ' + val);
});
console.log(process.argv[2].split('=')[1])

export let app: any
const app_port = process.env.QueryModuleUrlPort;
async function bootstrap() {
  app = await NestFactory.create<NestExpressApplication>(AppModule);

  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();
  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();
