pglens 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -7
- package/README.md +38 -145
- package/bin/pglens +143 -7
- package/client/app.js +1141 -79
- package/client/index.html +133 -35
- package/client/styles.css +980 -49
- package/package.json +1 -1
- package/src/db/connection.js +163 -27
- package/src/routes/api.js +288 -9
- package/src/server.js +72 -30
- package/pglens-1.0.0.tgz +0 -0
package/src/server.js
CHANGED
|
@@ -19,43 +19,85 @@ const apiRoutes = require('./routes/api');
|
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Start the Express server.
|
|
22
|
-
* @param {string} connectionString - PostgreSQL connection string
|
|
23
|
-
* @param {number} port - Port number to listen on
|
|
24
|
-
* @param {string} sslMode - SSL mode for database connection
|
|
25
22
|
*/
|
|
26
|
-
|
|
23
|
+
const fs = require('fs');
|
|
24
|
+
const os = require('os');
|
|
25
|
+
const net = require('net');
|
|
26
|
+
|
|
27
|
+
const PORT_FILE = path.join(os.homedir(), '.pglens.port');
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Check if a port is in use.
|
|
31
|
+
* @param {number} port
|
|
32
|
+
* @returns {Promise<boolean>}
|
|
33
|
+
*/
|
|
34
|
+
function isPortInUse(port) {
|
|
35
|
+
return new Promise((resolve) => {
|
|
36
|
+
const server = net.createServer();
|
|
37
|
+
server.once('error', (err) => {
|
|
38
|
+
if (err.code === 'EADDRINUSE') {
|
|
39
|
+
resolve(true);
|
|
40
|
+
} else {
|
|
41
|
+
resolve(false);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
server.once('listening', () => {
|
|
45
|
+
server.close();
|
|
46
|
+
resolve(false);
|
|
47
|
+
});
|
|
48
|
+
server.listen(port);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Start the Express server.
|
|
54
|
+
*/
|
|
55
|
+
async function startServer() {
|
|
27
56
|
const app = express();
|
|
57
|
+
let port = 54321;
|
|
58
|
+
|
|
59
|
+
// Try to find an available port starting from 54321
|
|
60
|
+
if (await isPortInUse(port)) {
|
|
61
|
+
port = 0; // Let OS choose a random available port
|
|
62
|
+
}
|
|
28
63
|
|
|
29
64
|
app.use(cors());
|
|
30
65
|
app.use(express.json());
|
|
31
66
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
67
|
+
app.use('/api', apiRoutes);
|
|
68
|
+
|
|
69
|
+
const clientPath = path.join(__dirname, '../client');
|
|
70
|
+
app.use(express.static(clientPath));
|
|
71
|
+
|
|
72
|
+
app.get('*', (_, res) => {
|
|
73
|
+
res.sendFile(path.join(clientPath, 'index.html'));
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const server = app.listen(port, () => {
|
|
77
|
+
const actualPort = server.address().port;
|
|
78
|
+
console.log(`✓ Server running on http://localhost:${actualPort}`);
|
|
79
|
+
console.log(` Open your browser to view your database`);
|
|
80
|
+
|
|
81
|
+
// Write port to file for CLI to read
|
|
82
|
+
fs.writeFileSync(PORT_FILE, actualPort.toString());
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const shutdown = () => {
|
|
86
|
+
console.log('\nShutting down...');
|
|
87
|
+
if (fs.existsSync(PORT_FILE)) {
|
|
88
|
+
try {
|
|
89
|
+
fs.unlinkSync(PORT_FILE);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
// Ignore removal errors
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
closePool().then(() => {
|
|
95
|
+
process.exit(0);
|
|
58
96
|
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
process.on('SIGINT', shutdown);
|
|
100
|
+
process.on('SIGTERM', shutdown);
|
|
59
101
|
}
|
|
60
102
|
|
|
61
103
|
module.exports = { startServer };
|
package/pglens-1.0.0.tgz
DELETED
|
Binary file
|