Remove database
Some checks failed
Build and Push to Registry / build-and-push (push) Failing after 40s
Some checks failed
Build and Push to Registry / build-and-push (push) Failing after 40s
This commit is contained in:
parent
cfa9514be9
commit
b04679ba34
@ -1,7 +1,6 @@
|
|||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { Database } from '../database';
|
import { SignerImprovedService } from '../services/signer';
|
||||||
import { SignerImprovedService } from '../services/signer-improved';
|
|
||||||
import { SessionManager } from '../utils/session-manager';
|
import { SessionManager } from '../utils/session-manager';
|
||||||
import { authTokens } from '../utils/auth-tokens';
|
import { authTokens } from '../utils/auth-tokens';
|
||||||
import { ProcessInfo, ProcessData, ProcessRoles, EOfficeStatus } from '../types';
|
import { ProcessInfo, ProcessData, ProcessRoles, EOfficeStatus } from '../types';
|
||||||
@ -54,22 +53,23 @@ export class ProcessController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Get UUID from database
|
// Get UUID from database
|
||||||
let uuid: string;
|
let uuid: string = uuidv4();
|
||||||
try {
|
// TODO this should be moved to signer probably
|
||||||
const result = await Database.query('SELECT uid FROM users WHERE "idNot" = $1', [userAuth.idNotUser.idNot]);
|
// try {
|
||||||
uuid = result.rows.length > 0 ? result.rows[0].uid : null;
|
// const result = await Database.query('SELECT uid FROM users WHERE "idNot" = $1', [userAuth.idNotUser.idNot]);
|
||||||
} catch (error) {
|
// uuid = result.rows.length > 0 ? result.rows[0].uid : null;
|
||||||
Logger.error('Error fetching UUID by idNot', {
|
// } catch (error) {
|
||||||
requestId,
|
// Logger.error('Error fetching UUID by idNot', {
|
||||||
error: error instanceof Error ? error.message : 'Unknown error'
|
// requestId,
|
||||||
});
|
// error: error instanceof Error ? error.message : 'Unknown error'
|
||||||
uuid = '';
|
// });
|
||||||
}
|
// uuid = '';
|
||||||
|
// }
|
||||||
|
|
||||||
if (!uuid) {
|
// if (!uuid) {
|
||||||
Logger.info('No existing UUID found in db, generating new one', { requestId });
|
// Logger.info('No existing UUID found in db, generating new one', { requestId });
|
||||||
uuid = uuidv4();
|
// uuid = uuidv4();
|
||||||
}
|
// }
|
||||||
|
|
||||||
const processData: ProcessData = {
|
const processData: ProcessData = {
|
||||||
uid: uuid,
|
uid: uuid,
|
||||||
@ -282,22 +282,22 @@ export class ProcessController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get UUID from database
|
// Get UUID from database
|
||||||
let uuid: string;
|
let uuid: string = uuidv4();
|
||||||
try {
|
// try {
|
||||||
const result = await Database.query('SELECT uid FROM offices WHERE "idNot" = $1', [userAuth.idNotUser.office.idNot]);
|
// const result = await Database.query('SELECT uid FROM offices WHERE "idNot" = $1', [userAuth.idNotUser.office.idNot]);
|
||||||
uuid = result.rows.length > 0 ? result.rows[0].uid : null;
|
// uuid = result.rows.length > 0 ? result.rows[0].uid : null;
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
Logger.error('Error fetching office UUID by idNot', {
|
// Logger.error('Error fetching office UUID by idNot', {
|
||||||
requestId,
|
// requestId,
|
||||||
error: error instanceof Error ? error.message : 'Unknown error'
|
// error: error instanceof Error ? error.message : 'Unknown error'
|
||||||
});
|
// });
|
||||||
uuid = '';
|
// uuid = '';
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (!uuid) {
|
// if (!uuid) {
|
||||||
Logger.info('No existing office UUID found in db, generating new one', { requestId });
|
// Logger.info('No existing office UUID found in db, generating new one', { requestId });
|
||||||
uuid = uuidv4();
|
// uuid = uuidv4();
|
||||||
}
|
// }
|
||||||
|
|
||||||
const processData: ProcessData = {
|
const processData: ProcessData = {
|
||||||
uid: uuid,
|
uid: uuid,
|
||||||
|
103
src/database.ts
103
src/database.ts
@ -1,103 +0,0 @@
|
|||||||
import { Pool, QueryResult, PoolConfig } from 'pg';
|
|
||||||
import * as dotenv from 'dotenv';
|
|
||||||
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration de la base de données PostgreSQL
|
|
||||||
*/
|
|
||||||
const dbConfig: PoolConfig = {
|
|
||||||
user: process.env.DB_USER || 'postgres',
|
|
||||||
host: process.env.DB_HOST || 'localhost',
|
|
||||||
database: process.env.DB_NAME || 'prd',
|
|
||||||
password: process.env.DB_PASSWORD || 'admin',
|
|
||||||
port: parseInt(process.env.DB_PORT || '5432'),
|
|
||||||
|
|
||||||
// Configuration du pool de connexions
|
|
||||||
max: parseInt(process.env.DB_POOL_MAX || '20'), // Nombre maximum de connexions dans le pool
|
|
||||||
min: parseInt(process.env.DB_POOL_MIN || '2'), // Nombre minimum de connexions maintenues
|
|
||||||
idleTimeoutMillis: parseInt(process.env.DB_IDLE_TIMEOUT || '30000'), // Temps d'inactivité avant fermeture
|
|
||||||
connectionTimeoutMillis: parseInt(process.env.DB_CONNECTION_TIMEOUT || '2000'), // Timeout pour établir une connexion
|
|
||||||
// acquireTimeoutMillis: parseInt(process.env.DB_ACQUIRE_TIMEOUT || '60000'), // Timeout pour acquérir une connexion
|
|
||||||
|
|
||||||
// Configuration SSL si nécessaire
|
|
||||||
ssl: process.env.DB_SSL === 'true' ? {
|
|
||||||
rejectUnauthorized: process.env.DB_SSL_REJECT_UNAUTHORIZED !== 'false'
|
|
||||||
} : false
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pool de connexions PostgreSQL
|
|
||||||
*/
|
|
||||||
const pool = new Pool(dbConfig);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gestionnaire d'erreur pour le pool
|
|
||||||
*/
|
|
||||||
pool.on('error', (err: Error) => {
|
|
||||||
console.error('PostgreSQL Error:', err);
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Classe pour gérer les opérations de base de données
|
|
||||||
*/
|
|
||||||
export class Database {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exécute une requête SQL avec des paramètres
|
|
||||||
* @param text - La requête SQL
|
|
||||||
* @param params - Les paramètres de la requête
|
|
||||||
* @returns Promise<QueryResult> - Le résultat de la requête
|
|
||||||
*/
|
|
||||||
static async query(text: string, params?: any[]): Promise<QueryResult> {
|
|
||||||
try {
|
|
||||||
return await pool.query(text, params);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error executing query:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Teste la connexion à la base de données
|
|
||||||
* @returns Promise<boolean> - True si la connexion est réussie
|
|
||||||
*/
|
|
||||||
static async testConnection(): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
const result = await this.query('SELECT NOW() as current_time');
|
|
||||||
console.log('Database connection successful:', result.rows[0].current_time);
|
|
||||||
return true;
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error('Database connection failed:', error.message);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ferme toutes les connexions du pool
|
|
||||||
* @returns Promise<void>
|
|
||||||
*/
|
|
||||||
static async close(): Promise<void> {
|
|
||||||
try {
|
|
||||||
await pool.end();
|
|
||||||
console.log('PostgreSQL connection pool closed');
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error closing connection pool:', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gestion propre de l'arrêt de l'application
|
|
||||||
*/
|
|
||||||
process.on('SIGINT', async () => {
|
|
||||||
console.log('SIGINT signal received, closing connections...');
|
|
||||||
await Database.close();
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
|
||||||
console.log('SIGTERM signal received, closing connections...');
|
|
||||||
await Database.close();
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
@ -1,6 +1,5 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import { Database } from './database';
|
|
||||||
import { config } from './config';
|
import { config } from './config';
|
||||||
import { routes } from './routes';
|
import { routes } from './routes';
|
||||||
import { SignerImprovedService } from './services/signer-improved';
|
import { SignerImprovedService } from './services/signer-improved';
|
||||||
@ -96,18 +95,7 @@ setInterval(() => {
|
|||||||
|
|
||||||
// Initialisation et démarrage du serveur
|
// Initialisation et démarrage du serveur
|
||||||
async function startServer(): Promise<void> {
|
async function startServer(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// Test de la connexion à la base de données au démarrage
|
|
||||||
console.log('Initializing database connection...');
|
|
||||||
const isDbConnected = await Database.testConnection();
|
|
||||||
|
|
||||||
if (!isDbConnected) {
|
|
||||||
console.error('Database connection failed. Server cannot start.');
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Database connection established successfully');
|
|
||||||
|
|
||||||
// Démarrage du serveur
|
// Démarrage du serveur
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server started on port ${PORT}`);
|
console.log(`Server started on port ${PORT}`);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user