Расширенная диагностика PG: /v2/db-status — DNS, коннект, таблицы, пул. Версия 0.1.19
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ipwhitelist",
|
"name": "ipwhitelist",
|
||||||
"version": "0.1.18",
|
"version": "0.1.19",
|
||||||
"description": "IP WhiteList microservice for cloud provider",
|
"description": "IP WhiteList microservice for cloud provider",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
+105
-9
@@ -65,20 +65,75 @@ function createV2Router({ generateCsrfToken, doubleCsrfProtection } = {}) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── GET /v2/db-status — проверка подключения к PG (для отладки) ─────
|
// ── GET /v2/db-status — диагностика подключения к PG ─────────────
|
||||||
router.get('/db-status', async (req, res) => {
|
router.get('/db-status', async (req, res) => {
|
||||||
try {
|
|
||||||
const { pool } = require('./src/db');
|
|
||||||
const dns = require('dns').promises;
|
const dns = require('dns').promises;
|
||||||
|
const { pool } = require('./src/db');
|
||||||
const host = process.env.DB_HOST || 'localhost';
|
const host = process.env.DB_HOST || 'localhost';
|
||||||
const ips = await dns.resolve4(host).catch(() => ['dns-failed']);
|
const port = process.env.DB_PORT || 5432;
|
||||||
const start = Date.now();
|
const db = process.env.DB_NAME || 'ipwhitelist';
|
||||||
await pool.query('SELECT 1 AS ok');
|
const user = process.env.DB_USER || 'super';
|
||||||
const result = { ok: true, db_host: host, resolved_ips: ips, query_ms: Date.now() - start };
|
|
||||||
return res.json(result);
|
const info = {
|
||||||
|
env: {
|
||||||
|
DB_HOST: host,
|
||||||
|
DB_PORT: port,
|
||||||
|
DB_NAME: db,
|
||||||
|
DB_USER: user,
|
||||||
|
DB_PASS: process.env.DB_PASS ? '***' : 'NOT SET',
|
||||||
|
APP_ENV: process.env.APP_ENV || 'production',
|
||||||
|
},
|
||||||
|
dns: { host, ok: false, ips: [] },
|
||||||
|
connect: { ok: false, ms: 0, error: null },
|
||||||
|
query: { ok: false, ms: 0, error: null },
|
||||||
|
pool: { total: pool.totalCount, idle: pool.idleCount, waiting: pool.waitingCount },
|
||||||
|
pg_version: null,
|
||||||
|
tables: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
// DNS
|
||||||
|
try {
|
||||||
|
info.dns.ips = await dns.resolve4(host, { ttl: true });
|
||||||
|
info.dns.ok = info.dns.ips.length > 0;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return res.json({ ok: false, error: e.message, db_host: process.env.DB_HOST || 'unknown' });
|
info.dns.error = e.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect + query
|
||||||
|
try {
|
||||||
|
const start = Date.now();
|
||||||
|
const r = await pool.query('SELECT version() AS v, current_timestamp AS ts');
|
||||||
|
info.connect = { ok: true, ms: Date.now() - start };
|
||||||
|
info.pg_version = r.rows[0].v;
|
||||||
|
info.server_time = r.rows[0].ts;
|
||||||
|
} catch (e) {
|
||||||
|
info.connect = { ok: false, ms: Date.now() - start, error: e.message };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tables
|
||||||
|
if (info.connect.ok) {
|
||||||
|
try {
|
||||||
|
const tables = await pool.query(`
|
||||||
|
SELECT table_name,
|
||||||
|
(SELECT COUNT(*) FROM information_schema.columns WHERE table_name = t.table_name) AS cols
|
||||||
|
FROM information_schema.tables t
|
||||||
|
WHERE table_schema='public' AND table_type='BASE TABLE'
|
||||||
|
ORDER BY table_name
|
||||||
|
`);
|
||||||
|
for (const t of tables.rows) {
|
||||||
|
const cnt = await pool.query('SELECT COUNT(*)::int AS c FROM ' + t.table_name);
|
||||||
|
info.tables.push({ table: t.table_name, columns: t.cols, rows: cnt.rows[0].c });
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
info.tables = [{ error: e.message }];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pool stats
|
||||||
|
info.pool = { total: pool.totalCount, idle: pool.idleCount, waiting: pool.waitingCount };
|
||||||
|
|
||||||
|
res.set('Content-Type', 'text/html; charset=utf-8');
|
||||||
|
res.send(renderDbStatus(info));
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── GET /v2/iam-gateway-test — сравнение API (только админ) ────────
|
// ── GET /v2/iam-gateway-test — сравнение API (только админ) ────────
|
||||||
@@ -288,4 +343,45 @@ function iamComparePage(results) {
|
|||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function renderDbStatus(info) {
|
||||||
|
const ok = info.connect.ok;
|
||||||
|
return '<!DOCTYPE html><html><head><meta charset="utf-8"><title>PG Status</title>'
|
||||||
|
+ '<style>body{font-family:monospace;background:#111;color:#eee;padding:20px}'
|
||||||
|
+ 'h2{color:#4fc3f7}h3{color:#81c784;margin:15px 0 5px}'
|
||||||
|
+ '.ok{color:#81c784}.err{color:#ef5350;word-break:break-all}'
|
||||||
|
+ 'pre{background:#1a1a2e;padding:10px;border-radius:6px;overflow-x:auto;font-size:12px}'
|
||||||
|
+ 'table{border-collapse:collapse;margin:10px 0;font-size:12px}'
|
||||||
|
+ 'td,th{border:1px solid #333;padding:4px 10px;text-align:left}'
|
||||||
|
+ 'th{color:#888}.label{color:#888}</style></head><body>'
|
||||||
|
+ '<h2>⚡ PG Diagnostic <span class="' + (ok ? 'ok' : 'err') + '">' + (ok ? 'OK' : 'FAIL') + '</span></h2>'
|
||||||
|
|
||||||
|
// Status
|
||||||
|
+ '<h3>🔌 Connection</h3>'
|
||||||
|
+ '<p><span class="label">Host:</span> ' + info.env.DB_HOST + ':' + info.env.DB_PORT + '</p>'
|
||||||
|
+ '<p><span class="label">DB:</span> ' + info.env.DB_NAME + ' / ' + info.env.DB_USER + '</p>'
|
||||||
|
+ '<p><span class="label">APP_ENV:</span> ' + info.env.APP_ENV + '</p>'
|
||||||
|
+ '<p><span class="' + (ok ? 'ok' : 'err') + '">' + (ok ? '✅ Connected (' + info.connect.ms + 'ms)' : '❌ ' + info.connect.error) + '</span></p>'
|
||||||
|
|
||||||
|
// DNS
|
||||||
|
+ '<h3>🌐 DNS</h3>'
|
||||||
|
+ '<p>' + info.dns.ips.map(ip => typeof ip === 'object' ? ip.address : ip).join(', ') + (info.dns.error ? ' <span class="err">' + info.dns.error + '</span>' : '') + '</p>'
|
||||||
|
|
||||||
|
// Pool
|
||||||
|
+ '<h3>🏊 Pool</h3>'
|
||||||
|
+ '<p>total=' + info.pool.total + ' idle=' + info.pool.idle + ' waiting=' + info.pool.waiting + '</p>'
|
||||||
|
|
||||||
|
// PG Info
|
||||||
|
+ (info.pg_version ? '<h3>🐘 PostgreSQL</h3><p>' + info.pg_version + '</p><p>Server time: ' + info.server_time + '</p>' : '')
|
||||||
|
|
||||||
|
// Tables
|
||||||
|
+ '<h3>📊 Tables</h3>'
|
||||||
|
+ (info.tables.length > 0 && !info.tables[0].error
|
||||||
|
? '<table><tr><th>Table</th><th>Cols</th><th>Rows</th></tr>'
|
||||||
|
+ info.tables.map(t => '<tr><td>' + t.table + '</td><td>' + t.columns + '</td><td>' + t.rows + '</td></tr>').join('')
|
||||||
|
+ '</table>'
|
||||||
|
: '<p class="err">' + (info.tables[0] && info.tables[0].error || 'no data') + '</p>')
|
||||||
|
|
||||||
|
+ '</body></html>';
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = { createV2Router };
|
module.exports = { createV2Router };
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
// ═══════════════════════════════════════════════════════════════════════════════
|
// ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
version: '0.1.18',
|
version: '0.1.19',
|
||||||
|
|
||||||
// ── Среда ─────────────────────────────────────────────────────────────────
|
// ── Среда ─────────────────────────────────────────────────────────────────
|
||||||
// APP_ENV=test → тестовый стенд (жёлтый баннер в UI, можно включать тестовый API)
|
// APP_ENV=test → тестовый стенд (жёлтый баннер в UI, можно включать тестовый API)
|
||||||
|
|||||||
Reference in New Issue
Block a user