55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
import express from "express";
|
|
import cors from "cors";
|
|
import dotenv from "dotenv";
|
|
import helmet from "helmet";
|
|
import morgan from "morgan";
|
|
import client from "prom-client";
|
|
import { pool } from "./db.js";
|
|
|
|
dotenv.config();
|
|
const app = express();
|
|
|
|
app.use(
|
|
cors({
|
|
origin: ["http://localhost:3000", "https://dsp5-archi-o24a-15m-g3.fr"],
|
|
credentials: true,
|
|
})
|
|
);
|
|
app.use(helmet());
|
|
app.use(morgan("tiny"));
|
|
app.use(express.json());
|
|
|
|
// ✅ Nouvelle route /health pour Jenkins
|
|
app.get("/health", (req, res) => {
|
|
res.status(200).json({ status: "ok" });
|
|
});
|
|
|
|
// ✅ Route racine (pour éviter le 404 sur /)
|
|
app.get("/", (req, res) => {
|
|
res.json({ message: "✅ API The Tip Top en ligne !" });
|
|
});
|
|
|
|
// Vérif base de données
|
|
app.get("/db-check", async (req, res) => {
|
|
try {
|
|
const result = await pool.query("SELECT NOW()");
|
|
res.json({ message: "✅ DB connectée", time: result.rows[0].now });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// Prometheus
|
|
const collectDefaultMetrics = client.collectDefaultMetrics;
|
|
collectDefaultMetrics();
|
|
app.get("/metrics", async (req, res) => {
|
|
res.set("Content-Type", client.register.contentType);
|
|
res.end(await client.register.metrics());
|
|
});
|
|
|
|
// Lancement serveur
|
|
const PORT = process.env.PORT || 4000;
|
|
app.listen(PORT, "0.0.0.0", () => {
|
|
console.log(`🚀 Backend lancé sur 0.0.0.0:${PORT} ✅`);
|
|
});
|