the-tip-top-backend/index.js

55 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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());
// --- Vérification connexion DB ---
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 });
}
});
// --- Route santé ---
app.get("/health", (req, res) => {
res.status(200).json({ status: "ok" });
});
// --- Route daccueil ---
app.get("/", (req, res) => {
res.json({ message: "✅ API Thé Tip Top en ligne et opérationnelle g1001001 !" });
});
// --- Monitoring 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 du serveur ---
const PORT = process.env.PORT || 4000;
app.listen(PORT, "0.0.0.0", () => {
console.log(`🚀 Backend lancé sur 0.0.0.0:${PORT}`);
});