Skip to content

Commit 25db5da

Browse files
committed
deployment failed issues solved
1 parent 03ad0f2 commit 25db5da

3 files changed

Lines changed: 84 additions & 36 deletions

File tree

lib/backend/server.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ app.use(express.json());
3333
app.use('/api/posts', PostRoutes);
3434
// Basic health check route
3535
app.get('/', (req, res) => {
36-
res.status(200).json({ message: 'Backend server is running!', status: 'OK' });
36+
res.status(200).json({
37+
message: 'SkillSocket Backend is running!',
38+
status: 'OK',
39+
timestamp: new Date().toISOString(),
40+
port: process.env.PORT || 3000
41+
});
42+
});
43+
44+
// Simple ping endpoint for Render health checks
45+
app.get('/ping', (req, res) => {
46+
res.status(200).send('pong');
3747
});
3848

3949
// Health check endpoint for Docker and monitoring
@@ -100,10 +110,23 @@ app.use('/api/events', eventRoutes);
100110
// Export io for use in other modules
101111
module.exports = { io };
102112

103-
const MONGODB_URI = process.env.MONGODB_URI ;
104-
mongoose.connect(MONGODB_URI)
105-
.then(() => console.log("MongoDB connected"))
106-
.catch(err => console.error("MongoDB connection error:", err));
113+
const MONGODB_URI = process.env.MONGODB_URI;
114+
115+
console.log('📊 Connecting to MongoDB...');
116+
console.log('🔗 MongoDB URI exists:', !!MONGODB_URI);
117+
118+
mongoose.connect(MONGODB_URI, {
119+
serverSelectionTimeoutMS: 5000, // Timeout after 5s instead of 30s
120+
socketTimeoutMS: 45000, // Close sockets after 45s of inactivity
121+
})
122+
.then(() => {
123+
console.log("✅ MongoDB connected successfully");
124+
console.log("📊 Database ready state:", mongoose.connection.readyState);
125+
})
126+
.catch(err => {
127+
console.error("❌ MongoDB connection error:", err.message);
128+
process.exit(1);
129+
});
107130

108131
const onlineUsers = new Map();
109132

@@ -197,10 +220,12 @@ io.on('connection', (socket) => {
197220
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
198221

199222
function startServer(port = DEFAULT_PORT) {
200-
server.listen(port, () => {
223+
server.listen(port, '0.0.0.0', () => {
201224
console.log(`🚀 Server running on port ${port}`);
225+
console.log(`🌍 Server bound to 0.0.0.0:${port}`);
202226
console.log(`📋 Public upload endpoint: /api/user/upload-logo-public`);
203227
console.log(`🔒 Authenticated upload endpoint: /api/user/upload-logo`);
228+
console.log(`✅ Server ready to accept connections`);
204229
});
205230

206231
// Handle server errors (for example, EADDRINUSE)
@@ -223,5 +248,39 @@ function startServer(port = DEFAULT_PORT) {
223248
});
224249
}
225250

251+
// Graceful shutdown handlers
252+
process.on('SIGTERM', () => {
253+
console.log('🔄 SIGTERM received, shutting down gracefully...');
254+
server.close(() => {
255+
console.log('✅ Server closed');
256+
mongoose.connection.close(false, () => {
257+
console.log('✅ MongoDB connection closed');
258+
process.exit(0);
259+
});
260+
});
261+
});
262+
263+
process.on('SIGINT', () => {
264+
console.log('🔄 SIGINT received, shutting down gracefully...');
265+
server.close(() => {
266+
console.log('✅ Server closed');
267+
mongoose.connection.close(false, () => {
268+
console.log('✅ MongoDB connection closed');
269+
process.exit(0);
270+
});
271+
});
272+
});
273+
274+
// Handle uncaught exceptions
275+
process.on('uncaughtException', (err) => {
276+
console.error('💥 Uncaught Exception:', err);
277+
process.exit(1);
278+
});
279+
280+
process.on('unhandledRejection', (reason, promise) => {
281+
console.error('💥 Unhandled Rejection at:', promise, 'reason:', reason);
282+
process.exit(1);
283+
});
284+
226285
// Start the server
227286
startServer();

lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ class MyApp extends StatelessWidget {
2121
home: const LoadingPage(), // changed from LoginScreen to LoadingPage
2222
);
2323
}
24-
}
24+
}

render.yaml

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,35 @@
1-
# This render.yaml is for reference only
2-
# Since we're using Docker Hub method, create the service manually in Render dashboard
3-
4-
# Manual Render Setup Instructions:
5-
# 1. Go to Render Dashboard → New Web Service
6-
# 2. Choose "Deploy an existing image from a registry"
7-
# 3. Image URL: docker.io/yourusername/skillsocket-backend:latest
8-
# 4. Configure as shown below:
9-
101
services:
112
- type: web
123
name: skillsocket-backend
13-
env: docker
14-
# Image will be pulled from Docker Hub automatically
15-
image:
16-
url: docker.io/yourusername/skillsocket-backend:latest
4+
env: node
5+
region: oregon
176
plan: free
18-
region: oregon # Choose closest to your users
19-
healthCheckPath: /api/health
20-
# Auto-deploy when new image is pushed to Docker Hub
7+
rootDir: lib/backend
8+
buildCommand: npm ci --production
9+
startCommand: npm start
10+
healthCheckPath: /ping
2111
autoDeploy: true
2212
envVars:
2313
- key: NODE_ENV
2414
value: production
2515
- key: PORT
26-
value: 3000
27-
# Add your MongoDB connection string manually in Render dashboard
16+
value: 10000
17+
# Add these manually in Render dashboard - DO NOT commit real values
2818
- key: MONGODB_URI
29-
value: "mongodb+srv://username:password@cluster.mongodb.net/skillsocket"
30-
# Generate a strong JWT secret manually in Render dashboard
19+
sync: false # Set this in Render dashboard manually
3120
- key: JWT_SECRET
32-
value: "your-super-secret-jwt-key-here"
33-
# Optional: Add these if you use Cloudinary
21+
sync: false # Set this in Render dashboard manually
22+
# Optional Cloudinary vars - set in Render dashboard if using
3423
- key: CLOUDINARY_CLOUD_NAME
35-
value: "your-cloudinary-name"
24+
sync: false
3625
- key: CLOUDINARY_API_KEY
37-
value: "your-cloudinary-key"
26+
sync: false
3827
- key: CLOUDINARY_API_SECRET
39-
value: "your-cloudinary-secret"
40-
# Optional: Add these if you use Firebase
28+
sync: false
29+
# Optional Firebase vars - set in Render dashboard if using
4130
- key: FIREBASE_PROJECT_ID
42-
value: "your-firebase-project-id"
31+
sync: false
4332
- key: FIREBASE_CLIENT_EMAIL
44-
value: "your-firebase-client-email"
33+
sync: false
4534
- key: FIREBASE_PRIVATE_KEY
46-
value: "your-firebase-private-key"
35+
sync: false

0 commit comments

Comments
 (0)