|
| 1 | +import { createRouter } from "next-connect"; |
1 | 2 | import database from "infra/database"; |
2 | | -import { InternalServerError } from "infra/errors"; |
3 | | - |
4 | | -async function status(request, response) { |
5 | | - try { |
6 | | - const updatedAt = new Date().toISOString(); |
7 | | - |
8 | | - const databaseVersionResult = await database.query("SHOW server_version;"); |
9 | | - const databaseVersionValue = databaseVersionResult.rows[0].server_version; |
10 | | - |
11 | | - const databaseMaxConnectionsResult = await database.query( |
12 | | - "SHOW max_connections;" |
13 | | - ); |
14 | | - const databaseMaxConnectionsValue = |
15 | | - databaseMaxConnectionsResult.rows[0].max_connections; |
16 | | - |
17 | | - const databaseName = process.env.POSTGRES_DB; |
18 | | - const databaseOpenedConnectionsResult = await database.query({ |
19 | | - text: "SELECT count(*)::int FROM pg_stat_activity WHERE datname = $1;", |
20 | | - values: [databaseName], |
21 | | - }); |
22 | | - const databaseOpenedConnectionsValue = |
23 | | - databaseOpenedConnectionsResult.rows[0].count; |
24 | | - |
25 | | - response.status(200).json({ |
26 | | - updated_at: updatedAt, |
27 | | - dependencies: { |
28 | | - database: { |
29 | | - version: databaseVersionValue, |
30 | | - max_connections: parseInt(databaseMaxConnectionsValue), |
31 | | - opened_connections: databaseOpenedConnectionsValue, |
32 | | - }, |
33 | | - }, |
34 | | - }); |
35 | | - } catch (error) { |
36 | | - const publicErrorObject = new InternalServerError({ |
37 | | - cause: error, |
38 | | - }); |
| 3 | +import controller from "infra/controller.js"; |
39 | 4 |
|
40 | | - console.log("\n Erro dentro do catch do controller:"); |
41 | | - console.error(publicErrorObject); |
| 5 | +const router = createRouter(); |
42 | 6 |
|
43 | | - response.status(500).json(publicErrorObject); |
44 | | - } |
45 | | -} |
| 7 | +router.get(getHandler); |
| 8 | + |
| 9 | +export default router.handler(controller.errorHandlers); |
| 10 | + |
| 11 | +async function getHandler(request, response) { |
| 12 | + const updatedAt = new Date().toISOString(); |
| 13 | + |
| 14 | + const databaseVersionResult = await database.query("SHOW server_version;"); |
| 15 | + const databaseVersionValue = databaseVersionResult.rows[0].server_version; |
46 | 16 |
|
47 | | -export default status; |
| 17 | + const databaseMaxConnectionsResult = await database.query( |
| 18 | + "SHOW max_connections;" |
| 19 | + ); |
| 20 | + const databaseMaxConnectionsValue = |
| 21 | + databaseMaxConnectionsResult.rows[0].max_connections; |
| 22 | + |
| 23 | + const databaseName = process.env.POSTGRES_DB; |
| 24 | + const databaseOpenedConnectionsResult = await database.query({ |
| 25 | + text: "SELECT count(*)::int FROM pg_stat_activity WHERE datname = $1;", |
| 26 | + values: [databaseName], |
| 27 | + }); |
| 28 | + const databaseOpenedConnectionsValue = |
| 29 | + databaseOpenedConnectionsResult.rows[0].count; |
| 30 | + |
| 31 | + response.status(200).json({ |
| 32 | + updated_at: updatedAt, |
| 33 | + dependencies: { |
| 34 | + database: { |
| 35 | + version: databaseVersionValue, |
| 36 | + max_connections: parseInt(databaseMaxConnectionsValue), |
| 37 | + opened_connections: databaseOpenedConnectionsValue, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }); |
| 41 | +} |
0 commit comments