All files / src main.ts

0% Statements 0/26
100% Branches 0/0
0% Functions 0/4
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                                                                                                               
import { AppModule } from "@/app.module";
import fastifyCookie from "@fastify/cookie";
import fastifyHelmet from "@fastify/helmet";
import { Logger, ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { FastifyAdapter, NestFastifyApplication } from "@nestjs/platform-fastify";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { name } from "../package.json";
import { AxiosInterceptor } from "./common/interceptors/Axios.interceptor";
import { CommandInterceptor } from "./common/interceptors/Command.interceptor";
import { ShardingManager, TopGGAutoPoster } from "./lib";
import { Config } from "./modules/shared/config/types";
 
async function bootstrap() {
	const logger = new Logger("Main");
	const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
	const configService = app.get<ConfigService>(ConfigService);
	const Port = configService.get<number>("PORT");
 
	const fastifyInstance = app.getHttpAdapter().getInstance();
	fastifyInstance
		.addHook("onRequest", async (req, res) => {
			req.socket["encrypted"] = process.env.NODE_ENV === "production";
		})
		.decorateReply("setHeader", function (name: string, value: unknown) {
			this.header(name, value);
		})
		.decorateReply("end", function () {
			this.send("");
		});
 
	app.register(fastifyHelmet);
	app.register(fastifyCookie, {
		secret: configService.getOrThrow<Config["API"]>("API").CookieSecret,
		hook: "onRequest",
	});
 
	app.useGlobalInterceptors(new CommandInterceptor(), new AxiosInterceptor());
	app.setGlobalPrefix("api");
	app.useGlobalPipes(
		new ValidationPipe({
			always: true,
			forbidNonWhitelisted: true,
			transform: true,
		}),
	);
 
	const ShardManager = new ShardingManager(configService);
	const TopGGPoster = new TopGGAutoPoster(configService.getOrThrow<Config["TopGGToken"]>("TopGGToken"), ShardManager);
 
	const SwaggerConfig = new DocumentBuilder()
		.setTitle("N-D-B API Docs")
		.setDescription("View and test all routes and functions of N-D-B API")
		.setVersion("1.0")
		.build();
 
	const SwaggerDocument = SwaggerModule.createDocument(app, SwaggerConfig);
	SwaggerModule.setup("docs", app, SwaggerDocument);
 
	try {
		await app.listen(Port, "0.0.0.0");
		await ShardManager.init();
		await TopGGPoster.init();
 
		logger.log(`${name} Running on Port: ${Port} in ${process.env.ENVIRONMENT} mode | URL: ${await app.getUrl()}`);
	} catch (error) {
		logger.error("An error occurred when starting: ", (error as Error).message);
	}
}
bootstrap();