Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/controllers/stats.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const getStats = async (req, res, next) => {
res.status(200).json(stats);

} catch (error) {

next(error);
}
}

Expand Down
37 changes: 33 additions & 4 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ app.use('/api/albums', require('./routes/album.route'));
app.use('/api/playlists', require('./routes/playlist.route'));
app.use('/api/stats', require('./routes/stats.route'));

// 5. Health Check & Root
app.get('/api/health', async (req, res) => {
const mongoose = require('mongoose');
const Song = require('./models/song.model');
const Redis = require('./lib/redis');

const health = {
status: 'UP',
timestamp: new Date(),
database: {
status: mongoose.connection.readyState === 1 ? 'CONNECTED' : 'DISCONNECTED',
songCount: await Song.countDocuments().catch(() => 0),
},
redis: {
status: Redis.isOpen ? 'CONNECTED' : 'DISCONNECTED'
},
environment: process.env.NODE_ENV
};
res.status(200).json(health);
});

app.get('/', (req, res) => res.send('API is running...'));

// 5. Sentry Error Handler AFTER routes
Expand Down Expand Up @@ -90,10 +111,18 @@ app.use((err, req, res, next) => {

// 7. Database & Server start
const port = process.env.PORT || 5000;
connectDB();

httpServer.listen(port, () => {
Logger.info(`Server is running on port ${port}`);
});
const startServer = async () => {
try {
await connectDB();
httpServer.listen(port, () => {
Logger.info(`Server is running on port ${port}`);
});
} catch (err) {
Logger.error(`Server failed to start: ${err.message}`);
}
};

startServer();

module.exports = app;
9 changes: 8 additions & 1 deletion backend/src/lib/redis.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const { createClient } = require('redis');

const redisClient = createClient({
url: process.env.REDIS_URL
url: process.env.REDIS_URL,
socket: {
connectTimeout: 5000,
reconnectStrategy: (retries) => {
if (retries > 3) return new Error('Retry limit exceeded');
return Math.min(retries * 50, 500);
}
}
});

redisClient.on('error', (err) => console.log('Redis Client Error', err));
Expand Down
Loading