Skip to content
Snippets Groups Projects
main.ts 1.71 KiB
Newer Older
/**
 * This is not a production server yet!
 * This is only a minimal backend to get started.
 */

import { Logger } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";

import { AppModule } from "./app/app.module";

import { MicroserviceOptions, Transport } from "@nestjs/microservices";
import { ConfigService } from "@nestjs/config";
import { IGateway } from "@ocm-engine/config";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { WsAdapter } from "@nestjs/platform-ws";
import * as fs from "fs";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useWebSocketAdapter(new WsAdapter(app));

  const configService = app.get(ConfigService);
  const gatewayConfig = configService.get<IGateway>("gateway")!;

  const globalPrefix = "api";
  app.setGlobalPrefix(globalPrefix);
  app.enableShutdownHooks();

  app.connectMicroservice<MicroserviceOptions>({
    transport: Transport.TCP,
    options: {
      host: gatewayConfig.host,
      port: gatewayConfig.tcpPort,
    },
  });

  await app.startAllMicroservices();

  app.enableShutdownHooks();

  const config = new DocumentBuilder()
    .setTitle("OCM Gateway")
    .setDescription("OCM ENGINE GATEWAY API")
    .setVersion("1.0")
    .addServer(`http://${gatewayConfig.host}:${gatewayConfig.httpPort}`)
    .build();

  const document = SwaggerModule.createDocument(app, config);
  fs.writeFileSync("./gateway-swagger.json", JSON.stringify(document));
  SwaggerModule.setup("api", app, document);

  const port = gatewayConfig.httpPort || 3000;

  await app.listen(port, gatewayConfig.host);

  Logger.log(
    `🚀 Application is running on: http://${gatewayConfig.host}:${port}/${globalPrefix}`,
  );
}

bootstrap();