1
0
Files
jecnarozvrh/server.js
jzitnik-dev 9b56bff8a8
All checks were successful
Remote Deploy / deploy (push) Successful in 4s
feat: Add login mechanism to API endpoint
2025-08-07 10:54:27 +02:00

52 lines
1.3 KiB
JavaScript

const express = require("express");
const path = require("path");
const app = express();
const fs = require("fs/promises");
const cheerio = require("cheerio");
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
const url = 'https://www.spsejecna.cz/';
const cookies = `JSESSIONID=${req.query.session}; WTDGUID=10`;
fetch(url, {
method: 'GET',
headers: {
'Cookie': cookies
}
})
.then(res => res.text())
.then(data => {
const $ = cheerio.load(data);
const firstLabelText = $('.label.label-icon').first().text().trim();
if (firstLabelText == "Přihlášení") {
return res.status(403).send("Not logged in!");
}
res.sendFile(path.join(__dirname, "db", "current.json"));
})
.catch(err => {
console.error('Fetch error:', err);
res.status(500).send();
});
});
app.get("/status", async (_, res) => {
const dataStr = await fs.readFile(path.resolve("./volume/customState.json"), {encoding: "utf8"});
const data = JSON.parse(dataStr);
if (data.working) {
res.json({working: true})
} else {
res.json({working: data.working, message: data.message})
}
})
// TODO: Reporting errors
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});