feat: initialize microservice architecture with auth, api, realtime, copernicus, ml, and console modules
This commit is contained in:
105
auth/src/storage/database.js
Normal file
105
auth/src/storage/database.js
Normal file
@@ -0,0 +1,105 @@
|
||||
const { Pool } = require('pg');
|
||||
|
||||
const config = {
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT,
|
||||
max: 10,
|
||||
idleTimeoutMillis: 30000,
|
||||
connectionTimeoutMillis: 5000
|
||||
}
|
||||
|
||||
const pool = new Pool({ ...config, database: process.env.DB_NAME });
|
||||
|
||||
pool.on('error', (err) => {
|
||||
console.error('Error in database', err);
|
||||
});
|
||||
|
||||
/**
|
||||
* Execute a query with parameters
|
||||
* @param {string} text - SQL query
|
||||
* @param {Array} params - Query parameters
|
||||
* @returns {Promise<Object>} Query result
|
||||
*/
|
||||
async function query(text, params) {
|
||||
const start = Date.now();
|
||||
const result = await pool.query(text, params);
|
||||
const duration = Date.now() - start;
|
||||
|
||||
if (duration > 100) {
|
||||
console.warn(`[DB] Slow query (${duration}ms):`, text.substring(0, 80));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a client from pool for transactions
|
||||
* @returns {Promise<Object>} Pool client
|
||||
*/
|
||||
async function getClient() {
|
||||
return await pool.connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize database and ensure tables exist
|
||||
*/
|
||||
async function initDb() {
|
||||
// Test connection
|
||||
await pool.query('SELECT NOW()');
|
||||
// Ensure pgcrypto extension (provides gen_random_uuid)
|
||||
// Note: creating extensions requires proper DB permissions (usually superuser in PG)
|
||||
try {
|
||||
await pool.query(`CREATE EXTENSION IF NOT EXISTS pgcrypto;`);
|
||||
} catch (err) {
|
||||
console.warn('[DB] Could not create pgcrypto extension (may require superuser):', err.message);
|
||||
}
|
||||
|
||||
// Ensure tables exist (UUID default generated by DB)
|
||||
await pool.query(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
username VARCHAR(50) UNIQUE NOT NULL,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
telegram_id VARCHAR(50) UNIQUE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_telegram_id ON users(telegram_id);
|
||||
`);
|
||||
|
||||
await pool.query(`
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
session_code VARCHAR(64) NOT NULL,
|
||||
encoded_username TEXT NOT NULL,
|
||||
ip_address INET,
|
||||
user_agent TEXT,
|
||||
browser VARCHAR(100),
|
||||
os VARCHAR(100),
|
||||
device_type VARCHAR(50),
|
||||
location_country VARCHAR(100),
|
||||
location_city VARCHAR(100),
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
last_active TIMESTAMP DEFAULT NOW(),
|
||||
is_revoked BOOLEAN DEFAULT FALSE
|
||||
);
|
||||
|
||||
-- Altera colonna in base al nuovo standard token 32 byte - 64 url chars
|
||||
ALTER TABLE sessions ALTER COLUMN session_code TYPE VARCHAR(64);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_code ON sessions(session_code);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
|
||||
`);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
pool,
|
||||
query,
|
||||
getClient,
|
||||
initDb
|
||||
};
|
||||
36
auth/src/storage/redis.js
Normal file
36
auth/src/storage/redis.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const Redis = require('ioredis');
|
||||
|
||||
const redis = new Redis({
|
||||
host: process.env.REDIS_HOST,
|
||||
port: parseInt(process.env.REDIS_PORT),
|
||||
maxRetriesPerRequest: 3,
|
||||
lazyConnect: true,
|
||||
retryStrategy(times) {
|
||||
const delay = Math.min(times * 50, 2000);
|
||||
return delay;
|
||||
}
|
||||
})
|
||||
|
||||
redis.on('error', (error) => {
|
||||
console.error('redis error', error);
|
||||
})
|
||||
|
||||
redis.on('connect', () => {})
|
||||
redis.on('reconnecting', () => {
|
||||
console.log('reconnecting to redis')
|
||||
})
|
||||
|
||||
async function configure() {
|
||||
try {
|
||||
await redis.connect();
|
||||
await redis.ping();
|
||||
} catch (err) {
|
||||
console.error('Redis error', err);
|
||||
}
|
||||
}
|
||||
|
||||
function connected() {
|
||||
return redis.status === 'ready';
|
||||
}
|
||||
|
||||
module.exports = { redis, configure, connected };
|
||||
Reference in New Issue
Block a user