-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (40 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require('express');
const { Pool } = require('pg');
const app = express();
const port = process.env.PORT || 3000;
// Configuração para PostgreSQL
const pgPool = new Pool({
connectionString: process.env.PG_CONNECTION_STRING,
});
// Middleware para ajustar a base path
const basePath = process.env.BASE_PATH || '';
app.use((req, res, next) => {
if (req.originalUrl.startsWith(basePath)) {
req.url = req.originalUrl.substring(basePath.length);
}
next();
});
app.get('/', (req, res) => {
res.status(200).send('Hello');
});
app.get('/pg-check', async (req, res) => {
try {
const client = await pgPool.connect();
// Query para obter o nome do banco de dados e o endereço do servidor
const serverInfoQuery = `
SELECT
inet_server_addr() AS server_ip,
current_database() AS database_name,
now() AS current_time
`;
const result = await client.query(serverInfoQuery);
client.release();
// Retornar as informações
res.send(`Connected to database: ${result.rows[0].database_name} on server: ${result.rows[0].server_ip} at ${result.rows[0].current_time}`);
} catch (err) {
res.status(500).send(`PostgreSQL connection error: ${err.message}`);
}
});
app.listen(port, () => {
console.log(`App running on port ${port}`);
});