PERF: Validações de variaveis são realizadas antes da inicação da aplicação. Removido arquivo inutilizado

This commit is contained in:
Rafael Alves Lopes 2026-05-28 16:03:49 -03:00
parent ee966d3306
commit e000d5dc9c
5 changed files with 54 additions and 10 deletions

View File

@ -14,6 +14,7 @@ DB_NAME=omnichannel
FRONTEND_URL=http://localhost:3000
JWT_SECRET=change-this-long-random-secret
JWT_EXPIRES_IN=8h
LOG_LEVEL=info
# Auth providers: ldap,microsoft or only one of them
AUTH_PROVIDERS=ldap,microsoft

49
src/infra/config/env.ts Normal file
View File

@ -0,0 +1,49 @@
export interface DatabaseConfig {
host: string;
port: number;
user: string;
password: string;
database: string;
}
const REQUIRED_ENV = ['DB_HOST', 'DB_PORT', 'DB_USER', 'DB_PASSWORD', 'DB_NAME', 'JWT_SECRET'];
export function validateAppEnv() {
const missing = REQUIRED_ENV.filter((name) => !String(process.env[name] || '').trim());
if (missing.length) {
throw new Error(`Variaveis de ambiente obrigatorias ausentes: ${missing.join(', ')}`);
}
getNumberEnv('DB_PORT');
}
export function getDatabaseConfig(): DatabaseConfig {
return {
host: getRequiredEnv('DB_HOST'),
port: getNumberEnv('DB_PORT'),
user: getRequiredEnv('DB_USER'),
password: getRequiredEnv('DB_PASSWORD'),
database: getRequiredEnv('DB_NAME'),
};
}
function getRequiredEnv(name: string) {
const value = String(process.env[name] || '').trim();
if (!value) {
throw new Error(`Variavel de ambiente obrigatoria ausente: ${name}`);
}
return value;
}
function getNumberEnv(name: string) {
const value = Number(getRequiredEnv(name));
if (!Number.isFinite(value)) {
throw new Error(`Variavel de ambiente invalida: ${name} deve ser numerica`);
}
return value;
}

View File

@ -1,15 +1,10 @@
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { Pool, PoolClient, QueryResultRow } from 'pg';
import { getDatabaseConfig } from '../config/env';
@Injectable()
export class DatabaseService implements OnModuleDestroy {
private readonly pool = new Pool({
host: process.env.DB_HOST || 'localhost',
port: Number(process.env.DB_PORT || 5432),
user: process.env.DB_USER || process.env.POSTGRES_USER,
password: process.env.DB_PASSWORD || process.env.POSTGRES_PASSWORD,
database: process.env.DB_NAME || process.env.POSTGRES_DB,
});
private readonly pool = new Pool(getDatabaseConfig());
query<T extends QueryResultRow = QueryResultRow>(text: string, params?: unknown[]) {
return this.pool.query<T>(text, params);

View File

@ -1,3 +0,0 @@
import { Logger } from '@nestjs/common';
export const appLogger = new Logger('Application');

View File

@ -4,10 +4,12 @@ import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { json, urlencoded } from 'express';
import { AppModule } from './app.module';
import { validateAppEnv } from './infra/config/env';
import { loadEnv } from './infra/config/load-env';
async function bootstrap() {
loadEnv();
validateAppEnv();
const app = await NestFactory.create(AppModule, { bodyParser: false });
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3000';