From a2f9e06d096684fb739b54ff0fa420b393c9a5e7 Mon Sep 17 00:00:00 2001 From: cowrie Date: Fri, 30 Jan 2026 12:39:40 +0800 Subject: [PATCH] first commit --- api/Dockerfile | 12 +++++++++ api/data.json | 7 ++++++ api/server.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 21 ++++++++++++++++ nginx/default.conf | 17 +++++++++++++ readme.md | 2 ++ site/index.html | 51 +++++++++++++++++++++++++++++++++++++ 7 files changed, 173 insertions(+) create mode 100644 api/Dockerfile create mode 100644 api/data.json create mode 100644 api/server.js create mode 100644 docker-compose.yml create mode 100644 nginx/default.conf create mode 100644 readme.md create mode 100644 site/index.html diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 0000000..a9a8f5a --- /dev/null +++ b/api/Dockerfile @@ -0,0 +1,12 @@ +FROM node:20-alpine + +WORKDIR /app + +RUN npm init -y && npm install express + +COPY server.js . +COPY data.json . + +EXPOSE 3000 + +CMD ["node", "server.js"] diff --git a/api/data.json b/api/data.json new file mode 100644 index 0000000..0e3657f --- /dev/null +++ b/api/data.json @@ -0,0 +1,7 @@ +{ + "CO2": 2.43, + "CO": 0.56, + "BZ": 3.11, + "AQ": 100, + "updated_at": "2026-01-30T03:48:16.033Z" +} \ No newline at end of file diff --git a/api/server.js b/api/server.js new file mode 100644 index 0000000..8e430ba --- /dev/null +++ b/api/server.js @@ -0,0 +1,63 @@ +const express = require("express"); +const fs = require("fs"); + +const app = express(); +const PORT = 3000; +const DATA_FILE = "./data.json"; + +// Helper: read data +function readData() { + return JSON.parse(fs.readFileSync(DATA_FILE, "utf8")); +} + +// Helper: write data +function writeData(data) { + fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2)); +} + +// ============================ +// GET /api/data → read values +// ============================ +app.get("/data", (req, res) => { + try { + const data = readData(); + res.json(data); + } catch (err) { + res.status(500).json({ error: "Gagal membaca data" }); + } +}); + +// =========================================== +// GET /api/update?co2=&co=&bz=&aq= +// =========================================== +app.get("/update", (req, res) => { + const { co2, co, bz, aq } = req.query; + + if (!co2 || !co || !bz || !aq) { + return res.status(400).json({ + error: "Parameter wajib: co2, co, bz, aq" + }); + } + + const newData = { + CO2: Number(co2), + CO: Number(co), + BZ: Number(bz), + AQ: Number(aq), + updated_at: new Date().toISOString() + }; + + try { + writeData(newData); + res.json({ + message: "Data berhasil diperbarui", + data: newData + }); + } catch (err) { + res.status(500).json({ error: "Gagal menyimpan data" }); + } +}); + +app.listen(PORT, () => { + console.log(`API berjalan di port ${PORT}`); +}); diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6bf9b8d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,21 @@ +version: "3.8" + +services: + web: + image: nginx:alpine + container_name: air-quality-web + ports: + - "8080:80" + volumes: + - ./site:/usr/share/nginx/html:ro + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro + depends_on: + - api + restart: unless-stopped + + api: + build: ./api + container_name: air-quality-api + volumes: + - ./api/data.json:/app/data.json + restart: unless-stopped diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 0000000..9503b37 --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,17 @@ +server { + listen 80; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + location / { + try_files $uri $uri/ =404; + } + + location /api/ { + proxy_pass http://api:3000/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5ef9718 --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +## Air quality webserver using docker +### just run "docker compose up -d --build" \ No newline at end of file diff --git a/site/index.html b/site/index.html new file mode 100644 index 0000000..605e330 --- /dev/null +++ b/site/index.html @@ -0,0 +1,51 @@ + + + + + Sensor Kualitas Udara + + + + +

🌱 Sensor Kualitas Udara

+ +
COâ‚‚: -
+
CO: -
+
BZ: -
+
AQ: -
+ + + + +