Procedimiento de activacion
This commit is contained in:
28
src/adapters/HTTPClient.adapter.ts
Normal file
28
src/adapters/HTTPClient.adapter.ts
Normal 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
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
41
src/adapters/PGClient.adapter.ts
Normal file
41
src/adapters/PGClient.adapter.ts
Normal 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
|
||||
};
|
||||
}
|
||||
0
src/aplication/Nfc.controller.ts
Normal file
0
src/aplication/Nfc.controller.ts
Normal file
9
src/aplication/Nfc.usecases.ts
Normal file
9
src/aplication/Nfc.usecases.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { ServerContext } from "domain/ServerContext.js";
|
||||
|
||||
export class NfcUsecases {
|
||||
constructor(
|
||||
serverContext: ServerContext
|
||||
) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
3
src/config/httpclient.config.ts
Normal file
3
src/config/httpclient.config.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { HttpClient } from "adapters/HTTPClient.adapter.js";
|
||||
|
||||
export const httpclient = new HttpClient({ baseURL: "", headers: {} })
|
||||
20
src/config/pgclient.config.ts
Normal file
20
src/config/pgclient.config.ts
Normal 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
|
||||
})
|
||||
7
src/domain/ServerContext.ts
Normal file
7
src/domain/ServerContext.ts
Normal 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
|
||||
}
|
||||
0
src/infrastructure/Nfc.repository.ts
Normal file
0
src/infrastructure/Nfc.repository.ts
Normal file
11
src/main.ts
11
src/main.ts
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user