This commit is contained in:
parent
c2513c9e96
commit
2b7fbf6173
2
.env
2
.env
|
|
@ -1,5 +1,5 @@
|
||||||
PORT=4000
|
|
||||||
DB_HOST=the-tip-top-db
|
DB_HOST=the-tip-top-db
|
||||||
DB_USER=postgres
|
DB_USER=postgres
|
||||||
DB_PASS=postgres
|
DB_PASS=postgres
|
||||||
DB_NAME=thetiptop
|
DB_NAME=thetiptop
|
||||||
|
PORT=4000
|
||||||
|
|
|
||||||
31
index.js
31
index.js
|
|
@ -7,24 +7,29 @@ dotenv.config();
|
||||||
const { Pool } = pkg;
|
const { Pool } = pkg;
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
// --- CORS sécurisé ---
|
||||||
app.use(
|
app.use(
|
||||||
cors({
|
cors({
|
||||||
origin: ["http://localhost:5173", "https://dsp5-archi-o24a-15m-g3.fr"],
|
origin: [
|
||||||
|
"http://localhost:5173", // pour tests locaux
|
||||||
|
"https://dsp5-archi-o24a-15m-g3.fr" // ton frontend en production
|
||||||
|
],
|
||||||
credentials: true,
|
credentials: true,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// --- Connexion PostgreSQL ---
|
// --- Connexion PostgreSQL ---
|
||||||
const pool = new Pool({
|
const pool = new Pool({
|
||||||
host: process.env.DB_HOST || "the-tip-top-db",
|
host: process.env.DB_HOST,
|
||||||
user: process.env.DB_USER || "postgres",
|
user: process.env.DB_USER,
|
||||||
password: process.env.DB_PASS || "postgres",
|
password: process.env.DB_PASS,
|
||||||
database: process.env.DB_NAME || "thetiptop",
|
database: process.env.DB_NAME,
|
||||||
port: 5432,
|
port: 5432,
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- Test de connexion à la base ---
|
// --- Vérification de la base ---
|
||||||
app.get("/db-check", async (req, res) => {
|
app.get("/db-check", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const result = await pool.query("SELECT NOW()");
|
const result = await pool.query("SELECT NOW()");
|
||||||
|
|
@ -38,23 +43,13 @@ app.get("/db-check", async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- Route test API ---
|
// --- Route de test ---
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
res.json({ message: "The Tip Top API is running 🚀" });
|
res.json({ message: "The Tip Top API is running 🚀" });
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- Exemple de route API utilisant la base ---
|
|
||||||
app.get("/users", async (req, res) => {
|
|
||||||
try {
|
|
||||||
const result = await pool.query("SELECT * FROM users LIMIT 10");
|
|
||||||
res.json(result.rows);
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// --- Lancement du serveur ---
|
// --- Lancement du serveur ---
|
||||||
const PORT = process.env.PORT || 4000;
|
const PORT = process.env.PORT || 4000;
|
||||||
app.listen(PORT, "0.0.0.0", () => {
|
app.listen(PORT, "0.0.0.0", () => {
|
||||||
console.log(`🚀 Backend The Tip Top lancé sur le port ${PORT}`);
|
console.log(`🚀 Backend lancé sur le port ${PORT}`);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user