2019-11-03 11:33:59 +00:00
|
|
|
var fuzIsOpen = false;
|
2019-11-03 11:45:01 +00:00
|
|
|
var lastSeen = new Date("1970-01-01");
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const db = "./.data/data.json";
|
|
|
|
|
|
|
|
try {
|
|
|
|
var content = fs.readFileSync(db, "utf8");
|
2019-11-03 11:56:59 +00:00
|
|
|
fuzIsOpen = JSON.parse(content)["fuzIsOpen"];
|
|
|
|
lastSeen = new Date(JSON.parse(content)["lastSeen"]);
|
2019-11-03 11:45:01 +00:00
|
|
|
} catch (err) {}
|
2019-11-03 11:33:59 +00:00
|
|
|
|
2019-09-30 21:05:06 +00:00
|
|
|
const express = require("express");
|
2018-10-15 15:28:02 +00:00
|
|
|
const app = express();
|
2018-03-13 20:25:39 +00:00
|
|
|
|
|
|
|
// http://expressjs.com/en/starter/static-files.html
|
2019-09-30 21:05:06 +00:00
|
|
|
app.use(express.static("public"));
|
2018-03-13 20:25:39 +00:00
|
|
|
|
|
|
|
// http://expressjs.com/en/starter/basic-routing.html
|
2019-11-03 11:33:59 +00:00
|
|
|
app.get("/", (req, res) => {
|
|
|
|
res.sendFile(__dirname + "/views/index.html");
|
|
|
|
});
|
|
|
|
|
|
|
|
// http://expressjs.com/en/starter/basic-routing.html
|
|
|
|
app.get("/img", (req, res) => {
|
2019-11-03 14:49:00 +00:00
|
|
|
if (fuzIsOpen && Date() - (2 * 60 * 1000) < lastSeen) {
|
|
|
|
return res.sendFile(__dirname + "/views/open.svg");
|
2019-11-03 11:45:01 +00:00
|
|
|
}
|
2019-11-03 14:49:00 +00:00
|
|
|
res.sendFile(__dirname + "/views/closed.svg");
|
2018-03-13 20:25:39 +00:00
|
|
|
});
|
2019-11-03 11:56:59 +00:00
|
|
|
app.get("/api", (req, res) => {
|
2019-11-03 14:49:00 +00:00
|
|
|
res.send(fuzIsOpen && new Date() - (2 * 60 * 1000) < lastSeen);
|
2019-11-03 11:56:59 +00:00
|
|
|
});
|
2018-03-13 20:25:39 +00:00
|
|
|
|
2019-11-03 11:22:00 +00:00
|
|
|
// http://expressjs.com/en/starter/basic-routing.html
|
2019-11-03 11:33:59 +00:00
|
|
|
app.get("/status", (req, res) => {
|
|
|
|
if (req.query.password !== process.env.PASSWORD) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
2019-11-03 12:08:59 +00:00
|
|
|
fuzIsOpen = req.query.fuzisopen == "1";
|
2019-11-03 14:37:00 +00:00
|
|
|
lastSeen = new Date();
|
2019-11-03 11:56:59 +00:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(db, JSON.stringify({ fuzIsOpen, lastSeen }));
|
|
|
|
} catch (err) {}
|
|
|
|
|
2019-11-03 11:33:59 +00:00
|
|
|
res.sendStatus(200);
|
2019-11-03 11:22:00 +00:00
|
|
|
});
|
|
|
|
|
2018-10-15 15:38:54 +00:00
|
|
|
const listener = app.listen(process.env.PORT, function() {
|
2019-09-30 21:05:06 +00:00
|
|
|
console.log("Your app is listening on port " + listener.address().port);
|
2018-03-13 20:25:39 +00:00
|
|
|
});
|
2019-11-03 11:22:00 +00:00
|
|
|
|
2019-11-03 11:45:01 +00:00
|
|
|
process.on("SIGTERM", function() {
|
2019-11-03 11:22:00 +00:00
|
|
|
console.log("SIGTERM received, sending SOS to Resurrect...");
|
2019-11-03 11:45:01 +00:00
|
|
|
require("https").get(
|
|
|
|
"https://resurrect.glitch.me/" + process.env.PROJECT_DOMAIN + "",
|
|
|
|
process.exit
|
|
|
|
);
|
|
|
|
});
|