add: added docker file for the backend

This commit is contained in:
Lukian 2025-04-27 14:05:09 +02:00
parent a91cf9d6a5
commit deee8457b7
3 changed files with 27 additions and 3 deletions

View file

@ -2,11 +2,12 @@ const express = require("express");
const http = require("http"); const http = require("http");
const mqtt = require("mqtt"); const mqtt = require("mqtt");
const mysql = require("mysql"); const mysql = require("mysql");
const path = require("node:path");
const app = express(); const app = express();
app.use(express.json()); app.use(express.json());
const client = mqtt.connect("mqtt://localhost/", { const client = mqtt.connect("mqtt://mqtt/", {
port: 1883, port: 1883,
protocol: "mqtt", protocol: "mqtt",
clientId: "unidite_traitement", clientId: "unidite_traitement",
@ -14,7 +15,7 @@ const client = mqtt.connect("mqtt://localhost/", {
password: "ensibs" password: "ensibs"
}); });
client.on("error", (err) => { client.once("error", (err) => {
console.error("MQTT connection 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 { sensor_id, level, text } = req.body;
const connection = mysql.createConnection({ const connection = mysql.createConnection({
host: "localhost", host: "mysql",
user: "root", user: "root",
password: "IloveSachAwAmama69", password: "IloveSachAwAmama69",
database: "sachamama" database: "sachamama"
@ -76,4 +77,6 @@ app.post("/api/alert", (req, res) => {
res.status(200).json({ message: "Alert sent successfully" }); res.status(200).json({ message: "Alert sent successfully" });
}); });
app.use(express.static("public"));
app.listen(3000, () => console.log("Server running on http://localhost:3000")); app.listen(3000, () => console.log("Server running on http://localhost:3000"));

View file

@ -32,3 +32,12 @@ services:
volumes: volumes:
- ./mosquitto/config:/mosquitto/config - ./mosquitto/config:/mosquitto/config
web:
build:
context: .
network: host
restart: always
container_name: web
ports:
- 8081:3000

12
dockerfile Normal file
View file

@ -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"]