diff --git a/Z2/app/index.html b/Z2/app/index.html
new file mode 100644
index 0000000..ec067af
--- /dev/null
+++ b/Z2/app/index.html
@@ -0,0 +1,137 @@
+
+
+
+ DevOps Dashboard
+
+
+
+
+🚀 DevOps Dashboard
+
+
+
+
⏱ DB Time
+
Loading...
+
+
+
+
+
+
+
+
🔌 Status
+
Checking...
+
+
+
+
+
+
➕ Add Message
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Z2/app/server.js b/Z2/app/server.js
index acaae26..e5b4137 100644
--- a/Z2/app/server.js
+++ b/Z2/app/server.js
@@ -1,8 +1,17 @@
const express = require("express");
const { Pool } = require("pg");
+const os = require("os");
+const path = require("path");
const app = express();
+// middleware
+app.use(express.json());
+app.use(express.static(path.join(__dirname)));
+
+let counter = 0;
+
+// DB connection
const pool = new Pool({
host: "postgres-service",
user: "postgres",
@@ -11,15 +20,93 @@ const pool = new Pool({
port: 5432,
});
-app.get("/", async (req, res) => {
+// 🔥 Wait for DB
+async function waitForDB() {
+ let connected = false;
+ while (!connected) {
+ try {
+ await pool.query("SELECT 1");
+ connected = true;
+ console.log("✅ Database connected");
+ } catch (err) {
+ console.log("⏳ Waiting for database...");
+ await new Promise(res => setTimeout(res, 2000));
+ }
+ }
+}
+
+async function initDB() {
+ try {
+ await pool.query(`
+ CREATE TABLE IF NOT EXISTS messages (
+ id SERIAL PRIMARY KEY,
+ text VARCHAR(255)
+ )
+ `);
+ console.log("✅ Table ready");
+ } catch (err) {
+ console.log("TABLE ERROR:", err.message);
+ }
+}
+
+// 🔹 Status API
+app.get("/api/status", async (req, res) => {
+ counter++;
+
try {
const result = await pool.query("SELECT NOW()");
- res.send("DB Time: " + result.rows[0].now);
+ res.json({
+ time: result.rows[0].now,
+ hostname: os.hostname(),
+ requests: counter,
+ status: "Connected"
+ });
} catch (err) {
- res.send("Database not ready");
+ res.json({
+ time: "N/A",
+ hostname: os.hostname(),
+ requests: counter,
+ status: "Database not ready"
+ });
}
});
-app.listen(3000, () => {
- console.log("Server running on port 3000");
-});
\ No newline at end of file
+// 🔹 Insert data
+app.post("/api/add", async (req, res) => {
+ const { text } = req.body;
+
+ try {
+ await pool.query(
+ "INSERT INTO messages (text) VALUES ($1)",
+ [text]
+ );
+ res.send("Inserted");
+ } catch (err) {
+ console.log("INSERT ERROR:", err);
+ res.status(500).send("Error inserting data");
+ }
+});
+
+// 🔹 Get data
+app.get("/api/messages", async (req, res) => {
+ try {
+ const result = await pool.query(
+ "SELECT * FROM messages ORDER BY id DESC"
+ );
+ res.json(result.rows);
+ } catch (err) {
+ console.log("FETCH ERROR:", err);
+ res.status(500).send("Error fetching data");
+ }
+});
+
+async function startServer() {
+ await waitForDB();
+ await initDB();
+
+ app.listen(3000, () => {
+ console.log("🚀 Server running on port 3000");
+ });
+}
+
+startServer();
\ No newline at end of file
diff --git a/Z2/k8s/service.yaml b/Z2/k8s/service.yaml
index b38a40a..f22a835 100644
--- a/Z2/k8s/service.yaml
+++ b/Z2/k8s/service.yaml
@@ -8,6 +8,6 @@ spec:
selector:
app: web-app
ports:
- - port: 80
+ - port: 60
targetPort: 3000
nodePort: 30007
\ No newline at end of file