Actualiser index.js
All checks were successful
the-tip-top-backend/pipeline/head This commit looks good

This commit is contained in:
wkadmin 2025-10-21 18:07:16 +00:00
parent 77f9a0aecc
commit 40b471e76e

View File

@ -1,24 +1,43 @@
import express from 'express';
import client from 'prom-client';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 4000;
// Crée un registre de métriques Prometheus
const register = new client.Registry();
client.collectDefaultMetrics({ register });
// --- Prometheus setup ---
const collectDefaultMetrics = client.collectDefaultMetrics;
collectDefaultMetrics(); // CPU, mémoire, etc.
// Route API
app.get('/', (req, res) => {
res.send('The Tip Top Backend is running 🚀');
const httpRequestCounter = new client.Counter({
name: 'http_requests_total',
help: 'Nombre total de requêtes HTTP reçues',
labelNames: ['method', 'route', 'status'],
});
// Route /metrics pour Prometheus
app.use((req, res, next) => {
res.on('finish', () => {
httpRequestCounter.inc({
method: req.method,
route: req.route?.path || req.path,
status: res.statusCode,
});
});
next();
});
// --- Ton API existante ---
app.get('/api', (req, res) => {
res.json({ message: 'API The Tip Top 🚀' });
});
// --- Endpoint de métriques ---
app.get('/metrics', async (req, res) => {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
res.set('Content-Type', client.register.contentType);
res.end(await client.register.metrics());
});
app.listen(PORT, () => {
console.log(`✅ Backend running on port ${PORT}`);
console.log(`🚀 Backend The Tip Top lancé sur le port ${PORT}`);
});