116 lines
2.9 KiB
JavaScript
116 lines
2.9 KiB
JavaScript
const express = require("express");
|
|
const bodyParser = require("body-parser");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
require("dotenv").config();
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
const DATA_PATH = path.join(__dirname, "data", "packages.json");
|
|
const PASSWORD = process.env.ADMIN_PASSWORD;
|
|
|
|
app.set("view engine", "ejs");
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
app.use(express.static("public"));
|
|
app.use((req, res, next) => {
|
|
console.log(`[${new Date().toLocaleString()}] ${req.method} ${req.url}`);
|
|
next();
|
|
});
|
|
|
|
|
|
// Load packages from JSON
|
|
function loadPackages() {
|
|
if (!fs.existsSync(DATA_PATH)) return [];
|
|
return JSON.parse(fs.readFileSync(DATA_PATH));
|
|
}
|
|
|
|
// Save packages to JSON
|
|
function savePackages(data) {
|
|
fs.writeFileSync(DATA_PATH, JSON.stringify(data, null, 2));
|
|
}
|
|
|
|
// Format local date/time to "YYYY-MM-DD HH:mm"
|
|
function getLocalTimeString() {
|
|
const now = new Date();
|
|
return now.getFullYear() + "-" +
|
|
String(now.getMonth() + 1).padStart(2, '0') + "-" +
|
|
String(now.getDate()).padStart(2, '0') + " " +
|
|
String(now.getHours()).padStart(2, '0') + ":" +
|
|
String(now.getMinutes()).padStart(2, '0');
|
|
}
|
|
|
|
// Home page
|
|
app.get("/", (req, res) => {
|
|
res.sendFile(path.join(__dirname, "public", "index.html"));
|
|
});
|
|
|
|
// Track package
|
|
app.post("/track", (req, res) => {
|
|
const id = req.body.id?.trim();
|
|
if (!id) return res.send("Invalid tracking ID");
|
|
|
|
const packages = loadPackages();
|
|
const pack = packages.find(p => p.id === id);
|
|
|
|
if (!pack) return res.send(`<p>ID not found</p><a href="/">Back</a>`);
|
|
|
|
const latest = pack.updates[pack.updates.length - 1];
|
|
const history = [...pack.updates].reverse();
|
|
|
|
res.render("track", {
|
|
id: pack.id,
|
|
latest,
|
|
history,
|
|
eta: pack.eta || "Not set"
|
|
});
|
|
});
|
|
|
|
// Admin panel
|
|
app.get("/admin", (req, res) => {
|
|
res.render("admin", { packages: null, error: null });
|
|
});
|
|
|
|
// Admin login
|
|
app.post("/admin", (req, res) => {
|
|
const { password } = req.body;
|
|
if (password !== PASSWORD) {
|
|
return res.render("admin", { packages: null, error: "Wrong password" });
|
|
}
|
|
const packages = loadPackages();
|
|
res.render("admin", { packages, error: null });
|
|
});
|
|
|
|
// Handle update
|
|
app.post("/update", (req, res) => {
|
|
const { id, progress, status, eta, password } = req.body;
|
|
if (password !== PASSWORD) return res.send("Invalid password");
|
|
|
|
const update = {
|
|
progress: parseInt(progress),
|
|
status,
|
|
time: getLocalTimeString()
|
|
};
|
|
|
|
const packages = loadPackages();
|
|
const pack = packages.find(p => p.id === id);
|
|
|
|
if (pack) {
|
|
pack.updates.push(update);
|
|
if (eta) pack.eta = eta;
|
|
} else {
|
|
const newPackage = {
|
|
id,
|
|
updates: [update],
|
|
eta: eta || ""
|
|
};
|
|
packages.push(newPackage);
|
|
}
|
|
|
|
savePackages(packages);
|
|
res.redirect("/admin");
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`🚀 Courier tracker running at http://localhost:${PORT}`);
|
|
});
|