Procedimiento de activacion

This commit is contained in:
2026-03-06 12:11:17 +01:00
parent 37e41a0130
commit 221abe0d33
16 changed files with 375 additions and 4 deletions

View File

@@ -0,0 +1,28 @@
import axios, { type AxiosInstance } from "axios"
export class HttpClient {
public client: AxiosInstance
constructor(args: {
baseURL: string,
headers: Object,
}) {
this.client = axios.create({
...args
})
this.client.interceptors.request.use(
// Plantilla
)
this.client.interceptors.response.use(
// Plantilla
)
}
}

View File

@@ -0,0 +1,41 @@
import { Pool, type QueryResult, type QueryResultRow } from "pg";
export class PgClient {
private pgPool: Pool;
constructor(args: {
pool: Pool
}) {
this.pgPool = args.pool
}
public connect() {
return this.pgPool.connect()
}
/**
* Wrapper para ejecutar consultas con tipado fuerte.
* T es el formato de la respusta.
* @param text - La consulta SQL (ej. 'SELECT * FROM users WHERE id = $1')
* @param params - Los valores para los placeholders $1, $2, etc.
*/
public async query<T extends QueryResultRow = any>(
text: string,
params?: any[]
): Promise<QueryResult<T>> {
return await this.pgPool.query(text, params);
};
/**
* Función para validar la conexión al inicio.
*/
public async checkDatabaseConnection(): Promise<void> {
const client = await this.pgPool.connect();
const res = await client.query('SELECT NOW()');
console.log(`[o] Database connected successfully at: ${res.rows[0].now}`);
client.release(); // Liberamos el cliente de vuelta al pool
return;
// Si algo falla se tiene que propagar
};
}

View File

View File

@@ -0,0 +1,9 @@
import type { ServerContext } from "domain/ServerContext.js";
export class NfcUsecases {
constructor(
serverContext: ServerContext
) {
}
}

View File

@@ -6,7 +6,7 @@ export const env = {
HOST: process.env.HOST,
POSTGRES_HOST: process.env.POSTGRES_HOST,
POSTGRES_PORT: process.env.POSTGRES_PORT,
POSTGRES_PORT: Number(process.env.POSTGRES_PORT),
POSTGRES_USER: process.env.POSTGRES_USER,
POSTGRES_PASSWORD: process.env.POSTGRES_PASSWORD
}

View File

@@ -0,0 +1,3 @@
import { HttpClient } from "adapters/HTTPClient.adapter.js";
export const httpclient = new HttpClient({ baseURL: "", headers: {} })

View File

@@ -0,0 +1,20 @@
import { Pool } from "pg";
import { PgClient } from "adapters/PGClient.adapter.js";
import { env } from "./env.config.js";
import assert from "node:assert";
const { POSTGRES_HOST, POSTGRES_PORT, POSTGRES_USER, POSTGRES_PASSWORD } = env
assert(POSTGRES_HOST != undefined)
assert(Number.isInteger(POSTGRES_PORT))
const pool = new Pool({
host: POSTGRES_HOST,
port: POSTGRES_PORT,
user: POSTGRES_USER,
password: POSTGRES_PASSWORD
})
export const pgClient = new PgClient({
pool
})

View File

@@ -0,0 +1,7 @@
import type { HttpClient } from "adapters/HTTPClient.adapter.js"
import type { PgClient } from "adapters/PGClient.adapter.js"
export type ServerContext = {
PostgresClient: PgClient,
HttpClient: HttpClient
}

View File

View File

@@ -1,13 +1,22 @@
import assert from 'assert';
import express, { Router, type Request, type Response } from 'express';
import { errorHandler } from './aplication/middleware.js';
import { env } from './config/env.config.js';
import assert from 'assert';
import type { ServerContext } from 'domain/ServerContext.js';
import { httpclient } from 'config/httpclient.config.js';
import { pgClient } from 'config/pgclient.config.js';
const PORT = env.PORT
const HOSTNAME = env.HOST
assert(HOSTNAME != undefined)
const serverContext: ServerContext = {
HttpClient: httpclient,
PostgresClient: pgClient
}
const router = Router();
router.get("/health", (req: Request, res: Response) => {