import { ConfigService } from '@nestjs/config';

export type ConfigurableHttpServer = {
  requestTimeout: number;
  headersTimeout: number;
  keepAliveTimeout: number;
  maxRequestsPerSocket: number;
};

export const configureHttpServer = (
  server: ConfigurableHttpServer,
  configService: ConfigService,
): void => {
  server.requestTimeout =
    configService.get<number>('reliability.serverRequestTimeoutMs', { infer: true }) ?? 120_000;
  server.headersTimeout =
    configService.get<number>('reliability.headersTimeoutMs', { infer: true }) ?? 15_000;
  server.keepAliveTimeout =
    configService.get<number>('reliability.keepAliveTimeoutMs', { infer: true }) ?? 5_000;
  server.maxRequestsPerSocket =
    configService.get<number>('reliability.maxRequestsPerSocket', { infer: true }) ?? 1_000;
};
