Skip to content

Commit 020ef6a

Browse files
committed
feat: add try-catch block around server startup to handle port conflicts gracefully
1 parent 063e16b commit 020ef6a

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/index.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -284,36 +284,38 @@ app.get('*', async (c) => {
284284
// Start the server
285285
const port = process.env.PORT || 3000;
286286
const startServer = (portToUse) => {
287-
serve({
288-
fetch: app.fetch,
289-
port: portToUse
290-
}, (info) => {
291-
console.log(`Server running at http://localhost:${info.port}`);
292-
console.log('Available endpoints:');
293-
console.log(`- WebSocket: http://localhost:${info.port}/api/1/ws`);
294-
console.log(`- Chat API: http://localhost:${info.port}/api/1/chat`);
295-
console.log(`- Chat UI: http://localhost:${info.port}/chat`);
296-
console.log(`- Subscription: http://localhost:${info.port}/api/1/subscription`);
297-
console.log(`- Subscription Status: http://localhost:${info.port}/api/1/subscription-status`);
298-
console.log(`- Stripe Checkout: http://localhost:${info.port}/api/1/payments/stripe/create-checkout-session`);
299-
console.log(`- Stripe Webhook: http://localhost:${info.port}/api/1/payments/stripe/webhook`);
300-
console.log(`- Stripe Subscription: http://localhost:${info.port}/api/1/payments/stripe/subscription`);
301-
console.log(`- Stripe Cancel: http://localhost:${info.port}/api/1/payments/stripe/cancel-subscription`);
302-
console.log(`- Payment Callback: http://localhost:${info.port}/api/1/payments/cryptapi/callback`);
303-
console.log(`- Payment Logs: http://localhost:${info.port}/api/1/payments/cryptapi/logs`);
304-
console.log(`- Web interface: http://localhost:${info.port}`);
305-
306-
// Create a separate WebSocket server on a different port
307-
const wsPort = parseInt(portToUse) + 1;
308-
startWebSocketServer(wsPort);
309-
}).catch(err => {
287+
try {
288+
serve({
289+
fetch: app.fetch,
290+
port: portToUse
291+
}, (info) => {
292+
console.log(`Server running at http://localhost:${info.port}`);
293+
console.log('Available endpoints:');
294+
console.log(`- WebSocket: http://localhost:${info.port}/api/1/ws`);
295+
console.log(`- Chat API: http://localhost:${info.port}/api/1/chat`);
296+
console.log(`- Chat UI: http://localhost:${info.port}/chat`);
297+
console.log(`- Subscription: http://localhost:${info.port}/api/1/subscription`);
298+
console.log(`- Subscription Status: http://localhost:${info.port}/api/1/subscription-status`);
299+
console.log(`- Stripe Checkout: http://localhost:${info.port}/api/1/payments/stripe/create-checkout-session`);
300+
console.log(`- Stripe Webhook: http://localhost:${info.port}/api/1/payments/stripe/webhook`);
301+
console.log(`- Stripe Subscription: http://localhost:${info.port}/api/1/payments/stripe/subscription`);
302+
console.log(`- Stripe Cancel: http://localhost:${info.port}/api/1/payments/stripe/cancel-subscription`);
303+
console.log(`- Payment Callback: http://localhost:${info.port}/api/1/payments/cryptapi/callback`);
304+
console.log(`- Payment Logs: http://localhost:${info.port}/api/1/payments/cryptapi/logs`);
305+
console.log(`- Web interface: http://localhost:${info.port}`);
306+
307+
// Create a separate WebSocket server on a different port
308+
const wsPort = parseInt(portToUse) + 1;
309+
startWebSocketServer(wsPort);
310+
});
311+
} catch (err) {
310312
if (err.code === 'EADDRINUSE') {
311313
console.log(`Port ${portToUse} is already in use, trying ${portToUse + 2}...`);
312314
startServer(portToUse + 2);
313315
} else {
314316
console.error('Server error:', err);
315317
}
316-
});
318+
}
317319
};
318320

319321
// Try to start the server on the initial port

0 commit comments

Comments
 (0)