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/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
- function startServer(connectionString, port, sslMode) {
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
- createPool(connectionString, sslMode)
33
- .then(() => {
34
- app.use('/api', apiRoutes);
35
-
36
- const clientPath = path.join(__dirname, '../client');
37
- app.use(express.static(clientPath));
38
-
39
- app.get('*', (_, res) => {
40
- res.sendFile(path.join(clientPath, 'index.html'));
41
- });
42
-
43
- app.listen(port, () => {
44
- console.log(`✓ Server running on http://localhost:${port}`);
45
- console.log(` Open your browser to view your database`);
46
- });
47
-
48
- process.on('SIGINT', () => {
49
- console.log('\nShutting down...');
50
- closePool().then(() => {
51
- process.exit(0);
52
- });
53
- });
54
- })
55
- .catch((error) => {
56
- console.error('Failed to start server:', error.message);
57
- process.exit(1);
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