diff --git a/back/index.js b/back/index.js index 6d2e2ed..66451c2 100644 --- a/back/index.js +++ b/back/index.js @@ -2,11 +2,12 @@ const express = require("express"); const http = require("http"); const mqtt = require("mqtt"); const mysql = require("mysql"); +const path = require("node:path"); const app = express(); app.use(express.json()); -const client = mqtt.connect("mqtt://localhost/", { +const client = mqtt.connect("mqtt://mqtt/", { port: 1883, protocol: "mqtt", clientId: "unidite_traitement", @@ -14,7 +15,7 @@ const client = mqtt.connect("mqtt://localhost/", { password: "ensibs" }); -client.on("error", (err) => { +client.once("error", (err) => { console.error("MQTT connection error:", err); }); @@ -37,7 +38,7 @@ app.post("/api/alert", (req, res) => { const { sensor_id, level, text } = req.body; const connection = mysql.createConnection({ - host: "localhost", + host: "mysql", user: "root", password: "IloveSachAwAmama69", database: "sachamama" @@ -76,4 +77,6 @@ app.post("/api/alert", (req, res) => { res.status(200).json({ message: "Alert sent successfully" }); }); +app.use(express.static("public")); + app.listen(3000, () => console.log("Server running on http://localhost:3000")); diff --git a/docker-compose.yml b/docker-compose.yml index a0e32f1..d1c97ac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,3 +32,12 @@ services: volumes: - ./mosquitto/config:/mosquitto/config + web: + build: + context: . + network: host + restart: always + container_name: web + ports: + - 8081:3000 + diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..f685c11 --- /dev/null +++ b/dockerfile @@ -0,0 +1,12 @@ +FROM node:alpine AS build +WORKDIR /app +COPY front . +RUN npm install && npm run build + +FROM node:alpine +WORKDIR /app +COPY back . +RUN npm install +COPY --from=build /app/dist /app/public +EXPOSE 3000 +CMD ["node", "index.js"] \ No newline at end of file