Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Dec 24, 2024
1 parent bcf1457 commit 8181045
Showing 1 changed file with 14 additions and 43 deletions.
57 changes: 14 additions & 43 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,54 +914,25 @@ async function testDatabaseConnection() {
}

// Wrap the server startup in an async function
async function startServer() {
await testDatabaseConnection();

return new Promise((resolve, reject) => {
console.log(`Attempting to bind to port ${PORT}...`);
await testDatabaseConnection();

try {
// Add error event listener before calling listen
server.on("error", (err) => {
console.error("Server error:", {
code: err.code,
message: err.message,
stack: err.stack,
});
reject(err);
});

server.on("listening", () => {
const address = server.address();
console.log("Server listening event triggered", address);
});

console.log("Calling server.listen...");
server.listen(PORT, "0.0.0.0", () => {
const address = server.address();
console.log("Listen callback triggered");

if (!address) {
console.error("Server failed to bind to an address");
process.exit(1);
}

console.log(`SSH server running on port ${PORT}`);
console.log(`Full address info:`, address);
resolve(true);
});
} catch (error) {
console.error("Caught error during server start:", error);
reject(error);
}
// Add error event listener before calling listen
server.on("error", (err) => {
console.error("Server error:", {
code: err.code,
message: err.message,
stack: err.stack,
});
}
// Call the startServer function
startServer().catch((error) => {
console.error("Failed to start server:", error);
process.exit(1);
});

server.on("listening", () => {
const address = server.address();
console.log("Server listening event triggered", address);
});

server.listen(PORT);

process.on("SIGINT", () => {
console.log("\nShutting down server...");
server.close();
Expand Down

0 comments on commit 8181045

Please sign in to comment.