diff --git a/boltic.yaml b/boltic.yaml index 82a73bc..1709ce7 100644 --- a/boltic.yaml +++ b/boltic.yaml @@ -7,4 +7,5 @@ build: ignorefile: .gitignore env: - PORT: '8080' \ No newline at end of file + PORT: '8080' + \ No newline at end of file diff --git a/index.js b/index.js index 2660e17..dd3fc5e 100644 --- a/index.js +++ b/index.js @@ -1,271 +1,78 @@ const express = require("express"); -const path = require("path"); -const fs = require("fs"); +const bodyParser = require("body-parser"); const app = express(); +const PORT = 8080; -app.use(express.static("public")); +// Middleware to parse JSON +app.use(bodyParser.json()); -// Set up EJS as the templating engine -app.set("view engine", "ejs"); -app.set("views", path.join(__dirname, "views")); +// In-memory data store +let items = []; +let currentId = 1; -// Utility to get directories sorted by datetime -function getSortedReportDirectories(reportDir, env) { - const dirents = fs - .readdirSync(reportDir, { withFileTypes: true }) - .filter((dirent) => dirent.isDirectory()) - .map((dirent) => { - const folderName = dirent.name; - const parts = folderName.split("-"); - if (parts.length >= 6) { - const metadata = extractMetadata(folderName); - const datetime = new Date(`${metadata.date} ${metadata.time}`); - const link = path.join(env, folderName, "index.html"); - return { - folderName, - date: metadata.date, - time: metadata.time, - datetime, - buildId: metadata.buildId, - link, - }; - } - return null; - }) +// Routes - .filter(Boolean) - .sort((a, b) => b.datetime - a.datetime); // Sort in descending order of datetime +// CREATE - Add a new item +app.post("/items", (req, res) => { + const { name, description } = req.body; - return dirents; -} - - -// Utility to extract metadata from folder names -function extractMetadata(str) { - const regex = - /^([a-z]+)-([a-z]+-[a-z0-9]+)-(\d+)-(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})$/; - const match = str.match(regex); - - if (!match) { - return "Invalid string format"; + if (!name) { + return res.status(400).json({ error: "Name is required" }); } - const [_, env, region, buildId, year, month, day, hours, minutes, seconds] = - match; - - // Create a date object in UTC - const utcDate = new Date( - Date.UTC(year, month - 1, day, hours, minutes, seconds) - ); - - // Convert to IST using toLocaleString with the appropriate timezone - const istDate = utcDate.toLocaleString("en-IN", { timeZone: "Asia/Kolkata" }); - - // Separate the date and time from the IST conversion - const [istDateOnly, istTimeOnly] = istDate.split(", "); - - return { - env, - region, - buildId: parseInt(buildId), - date: istDateOnly, - time: istTimeOnly, - datetime: utcDate, - }; -} - -// Route to serve the main index.html -app.get("/", (req, res) => { - res.sendFile(path.join(__dirname, "public", "index.html"), (err) => { - if (err) { - res.status(err.status).end(); - } - }); + const newItem = { id: currentId++, name, description }; + items.push(newItem); + res.status(201).json(newItem); }); -// Dynamic route to serve specific folder's index.html for PROD-ASIA-SOUTH1 -app.get("/uat-asia-south1/:folderId/index.html", async (req, res) => { - const folderId = req.params.folderId; - const reportDir = path.join( - __dirname, - "public", - "uat-asia-south1", - folderId, - "index.html" - ); - - // Serve the index.html from the specific folder - fs.readFile(reportDir, "utf8", (err, data) => { - if (err) { - return res - .status(404) - .send("Index file not found for the requested folder."); - } - res.send(data); - }); +// READ - Get all items +app.get("/items", (req, res) => { + res.json(items); }); -// Dynamic route to serve specific folder's index.html for PROD-US-CENTRAL1 -app.get("/uat-us-central1/:folderId/index.html", (req, res) => { - const folderId = req.params.folderId; - const reportDir = path.join( - __dirname, - "public", - "uat-us-central1", - folderId, - "index.html" - ); +// READ - Get item by ID +app.get("/items/:id", (req, res) => { + const { id } = req.params; + const item = items.find((i) => i.id === parseInt(id)); - // Serve the index.html from the specific folder - fs.readFile(reportDir, "utf8", (err, data) => { - if (err) { - return res - .status(404) - .send("Index file not found for the requested folder."); - } - res.send(data); - }); -}); -// Dynamic route to serve specific folder's index.html for PROD-ASIA-SOUTH1 -app.get("/prod-asia-south1/:folderId/index.html", async (req, res) => { - const folderId = req.params.folderId; - const reportDir = path.join( - __dirname, - "public", - "prod-asia-south1", - folderId, - "index.html" - ); + if (!item) { + return res.status(404).json({ error: "Item not found" }); + } - // Serve the index.html from the specific folder - fs.readFile(reportDir, "utf8", (err, data) => { - if (err) { - return res - .status(404) - .send("Index file not found for the requested folder."); - } - res.send(data); - }); + res.json(item); }); -// Dynamic route to serve specific folder's index.html for PROD-US-CENTRAL1 -app.get("/prod-us-central1/:folderId/index.html", (req, res) => { - const folderId = req.params.folderId; - const reportDir = path.join( - __dirname, - "public", - "prod-us-central1", - folderId, - "index.html" - ); +// UPDATE - Update item by ID +app.put("/items/:id", (req, res) => { + const { id } = req.params; + const { name, description } = req.body; - // Serve the index.html from the specific folder - fs.readFile(reportDir, "utf8", (err, data) => { - if (err) { - return res - .status(404) - .send("Index file not found for the requested folder."); - } - res.send(data); - }); + const item = items.find((i) => i.id === parseInt(id)); + if (!item) { + return res.status(404).json({ error: "Item not found" }); + } + + if (name) item.name = name; + if (description) item.description = description; + + res.json(item); }); -// Dynamic route to serve specific folder's index.html for DEV-ASIA-SOUTH1 -app.get("/dev-asia-south1/:folderId/index.html", (req, res) => { - const folderId = req.params.folderId; - const reportDir = path.join( - __dirname, - "public", - "dev-asia-south1", - folderId, - "index.html" - ); +// DELETE - Remove item by ID +app.delete("/items/:id", (req, res) => { + const { id } = req.params; + const itemIndex = items.findIndex((i) => i.id === parseInt(id)); - // Serve the index.html from the specific folder - fs.readFile(reportDir, "utf8", (err, data) => { - if (err) { - return res - .status(404) - .send("Index file not found for the requested folder."); - } - res.send(data); - }); + if (itemIndex === -1) { + return res.status(404).json({ error: "Item not found" }); + } + + items.splice(itemIndex, 1); + res.status(204).send(); }); -// Dynamic route to serve specific folder's index.html for DEV-US-CENTRAL1 -app.get("/dev-us-central1/:folderId/index.html", (req, res) => { - const folderId = req.params.folderId; - const reportDir = path.join( - __dirname, - "public", - "dev-us-central1", - folderId, - "index.html" - ); - - // Serve the index.html from the specific folder - fs.readFile(reportDir, "utf8", (err, data) => { - if (err) { - return res - .status(404) - .send("Index file not found for the requested folder."); - } - res.send(data); - }); -}); - -// Dynamic route for UAT-ASIA-SOUTH1 -app.get("/uat-asia", (req, res) => { - console.log("came here"); - - const reportDir = path.join(__dirname, "public", "uat-asia-south1"); - const reportDirs = getSortedReportDirectories(reportDir, "uat-asia-south1"); - res.render("reportPage", { reportDirs }); -}); - -// Dynamic route for PROD-US-CENTRAL1 (for all subfolders) -app.get("/uat-us", (req, res) => { - const reportDir = path.join(__dirname, "public", "uat-us-central1"); - const reportDirs = getSortedReportDirectories(reportDir, "uat-us-central1"); - res.render("reportPage", { reportDirs }); -}); -// Dynamic route for PROD-ASIA-SOUTH1 -app.get("/prod-asia", (req, res) => { - console.log("came here"); - - const reportDir = path.join(__dirname, "public", "prod-asia-south1"); - const reportDirs = getSortedReportDirectories(reportDir, "prod-asia-south1"); - res.render("reportPage", { reportDirs }); -}); - -// Dynamic route for PROD-US-CENTRAL1 (for all subfolders) -app.get("/prod-us", (req, res) => { - const reportDir = path.join(__dirname, "public", "prod-us-central1"); - const reportDirs = getSortedReportDirectories(reportDir, "prod-us-central1"); - res.render("reportPage", { reportDirs }); -}); - -// Dynamic route for DEV-ASIA-SOUTH1 -app.get("/dev-asia", (req, res) => { - const reportDir = path.join(__dirname, "public", "dev-asia-south1"); - const reportDirs = getSortedReportDirectories(reportDir, "dev-asia-south1"); - res.render("reportPage", { reportDirs }); -}); - -// Dynamic route for DEV-US-CENTRAL1 -app.get("/dev-us", (req, res) => { - const reportDir = path.join(__dirname, "public", "dev-us-central1"); - const reportDirs = getSortedReportDirectories(reportDir, "dev-us-central1"); - res.render("reportPage", { reportDirs }); -}); - -// Handle 404 errors -app.use((req, res) => { - res.status(404).send("404 Not Found"); -}); - -const PORT = process.env.PORT || 8080; +// Start server app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); }); diff --git a/public/dev-asia-south1/index.html b/public/dev-asia-south1/index.html deleted file mode 100644 index be506e8..0000000 --- a/public/dev-asia-south1/index.html +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - DEV-ASIA-SOUTH1 Automation Reports - - - - -
-

Automation Reports - DEV-ASIA-SOUTH1

-
- -
- -
- - - - - - - - diff --git a/public/dev-us-central1/index.html b/public/dev-us-central1/index.html deleted file mode 100644 index e8f8336..0000000 --- a/public/dev-us-central1/index.html +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - DEV-US-CENTRAL1 Automation Reports - - - - -
-

Automation Reports - DEV-US-CENTRAL1

-
- -
- -
- - - - - - - - diff --git a/public/index.html b/public/index.html deleted file mode 100644 index 4bc4fa3..0000000 --- a/public/index.html +++ /dev/null @@ -1,142 +0,0 @@ - - - - - - - Boltic Automation Reports - - - - -
-
- Boltic Logo -
-

Automation Reports

-
- -
- - -
- - - - - \ No newline at end of file diff --git a/public/prod-asia-south1/index.html b/public/prod-asia-south1/index.html deleted file mode 100644 index 21d7d9c..0000000 --- a/public/prod-asia-south1/index.html +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - PROD-ASIA-SOUTH1 Automation Reports - - - - -
-

Automation Reports - PROD-ASIA-SOUTH1

-
- -
- -
- - - - - - - - diff --git a/public/prod-us-central1/index.html b/public/prod-us-central1/index.html deleted file mode 100644 index f6f4355..0000000 --- a/public/prod-us-central1/index.html +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - PROD-US-CENTRAL1 Automation Reports - - - - -
-

Automation Reports - PROD-US-CENTRAL1

-
- -
- -
- - - - - - - - diff --git a/public/uat-asia-south1/index.html b/public/uat-asia-south1/index.html deleted file mode 100644 index 5c0f3f9..0000000 --- a/public/uat-asia-south1/index.html +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - UAT-ASIA-SOUTH1 Automation Reports - - - - -
-

Automation Reports - UAT-ASIA-SOUTH1

-
- -
- -
- - - - - - - - diff --git a/public/uat-us-central1/index.html b/public/uat-us-central1/index.html deleted file mode 100644 index 5c0f3f9..0000000 --- a/public/uat-us-central1/index.html +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - UAT-ASIA-SOUTH1 Automation Reports - - - - -
-

Automation Reports - UAT-ASIA-SOUTH1

-
- -
- -
- - - - - - - - diff --git a/views/reportPage.ejs b/views/reportPage.ejs deleted file mode 100644 index ff97ff5..0000000 --- a/views/reportPage.ejs +++ /dev/null @@ -1,108 +0,0 @@ - - - - - - Automation Reports - - - - -
-

Automation Reports

-
- -
- - - - - - - - - - - - <% reportDirs.forEach(function(report) { %> - - - - - - - - <% }) %> - -
Folder NameBuild IDDateTimeLink
<%= report.folderName %><%= report.buildId %><%= report.date %><%= report.time %>View Report
-
- - - - -