refactor: Rewrite to typescript
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
This commit is contained in:
135
server.ts
Normal file
135
server.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Jakub Žitník
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
import express, { Request, Response } from "express";
|
||||
import path from "path";
|
||||
const app = express();
|
||||
import fs from "fs/promises";
|
||||
import { getCurrentInterval } from "./scheduleRules.js";
|
||||
import bodyParser from "body-parser";
|
||||
import cors from "cors";
|
||||
|
||||
const VERSIONS = ["v1", "v2", "v3"];
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
// @ts-ignore
|
||||
globalThis.File = class File {};
|
||||
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.use(cors({
|
||||
origin: "*",
|
||||
methods: ["GET", "POST", "OPTIONS"],
|
||||
allowedHeaders: ["Content-Type"],
|
||||
}));
|
||||
|
||||
app.get('/', async (req: Request, res: Response) => {
|
||||
const userAgent = req.headers['user-agent'] || '';
|
||||
const isBrowser = /Mozilla|Chrome|Firefox|Safari|Edg/.test(userAgent);
|
||||
|
||||
if (isBrowser) {
|
||||
res.sendFile(path.join(process.cwd(), "web", "public", "index.html"));
|
||||
} else {
|
||||
const dataStr = await fs.readFile(path.join(process.cwd(), "db", "v1.json"), "utf8");
|
||||
const data = JSON.parse(dataStr);
|
||||
|
||||
data["status"]["currentUpdateSchedule"] = getCurrentInterval();
|
||||
|
||||
res.json(data);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/versioned/v1', async (_: Request, res: Response) => {
|
||||
const dataStr = await fs.readFile(path.join(process.cwd(), "db", "v1.json"), "utf8");
|
||||
const data = JSON.parse(dataStr);
|
||||
|
||||
data["status"]["currentUpdateSchedule"] = getCurrentInterval();
|
||||
|
||||
res.json(data);
|
||||
});
|
||||
|
||||
VERSIONS.forEach((version) => {
|
||||
app.get(`/versioned/${version}`, async (_: Request, res: Response) => {
|
||||
try {
|
||||
const filePath = path.join(process.cwd(), "db", `${version}.json`);
|
||||
const dataStr = await fs.readFile(filePath, "utf8");
|
||||
const data = JSON.parse(dataStr);
|
||||
|
||||
data.status.currentUpdateSchedule = getCurrentInterval();
|
||||
|
||||
res.json(data);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Failed to load version data" });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/status", async (_: Request, res: Response) => {
|
||||
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 })
|
||||
}
|
||||
})
|
||||
|
||||
app.post("/report", async (req: Request, res: Response): Promise<any> => {
|
||||
const { class: className, location, content } = req.body;
|
||||
if (!className || !location || !content) {
|
||||
return res.status(400).json({ error: "Missing required fields." });
|
||||
}
|
||||
if (!["TIMETABLE", "ABSENCES", "ABSENCE", "TAKES_PLACE", "OTHER"].includes(location)) {
|
||||
return res.status(400).json({ error: "Invalid location value." });
|
||||
}
|
||||
|
||||
const url = process.env.REPORT_WEBHOOK_URL;
|
||||
|
||||
if (!url) {
|
||||
console.error("REPORT_WEBHOOK_URL is not set.");
|
||||
return res.status(500).json({ error: "Server configuration error." });
|
||||
}
|
||||
|
||||
let resp;
|
||||
try {
|
||||
resp = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "text/plain",
|
||||
},
|
||||
body: `${content}\n\nClass: ${className}\nLocation: ${location}`,
|
||||
});
|
||||
} catch (err: any) {
|
||||
console.error('Fetch failed:', err.message);
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (!resp.ok) {
|
||||
throw new Error(`Request failed`);
|
||||
}
|
||||
|
||||
res.status(200).json({ message: "Report received successfully." });
|
||||
});
|
||||
|
||||
app.use(express.static(path.join(process.cwd(), 'web/public'), {
|
||||
index: 'index.html',
|
||||
extensions: ['html'],
|
||||
}));
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server is running at http://localhost:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user