import express from "express"; import cors from "cors"; import cookieParser from "cookie-parser"; // Import API endpoints import { getTimetable, getSubstitutions, getHistory, getClasses, putTimetable, getInfo, putKey, deleteKey, } from "./api/index.js"; import auth from "./api/auth.js"; import { Parser } from "./parser/index.js"; import { BolleClient } from "./parser/bolle.js"; import { parseSubstitutionPlan } from "./parser/untis.js"; import { registerAdmin } from "./api/admin.js"; import { checkAdmin } from "./api/permission.js"; // Check that credentials are supplied if ( !process.env.BOLLE_URL || !process.env.BOLLE_USER || !process.env.BOLLE_KEY ) { console.error("Error: Bolle Auth environment variables missing!"); process.exit(1); } // Create the Webserver const app = express(); const port = process.env.PORT || 3000; app.use(cors()); app.use(cookieParser()); app.use(express.urlencoded({ extended: true })); app.use(express.json()); // Initialize the Parser and set it to update the // substitution plan at the specified update interval new Parser( new BolleClient( process.env.BOLLE_URL, process.env.BOLLE_USER, process.env.BOLLE_KEY, ), parseSubstitutionPlan, process.env.UPDATE_INTERVAL || 1 * 60 * 1000, // Default to 1 minute ); // Create new Auth class to store sessions app.post("/auth/login", auth.login); app.get("/auth/logout", auth.logout); // Check login for every API request app.use("/api", auth.checkLogin); // Provide check endpoint so the frontend // can check if the user is logged in app.get("/api/check", (_req, res) => { res.sendStatus(200); }); // Register API endpoints app.get("/api/info", getInfo); app.put("/api/key", putKey); app.delete("/api/key", deleteKey); app.get("/api/timetable", getTimetable); app.put("/api/timetable", putTimetable); app.get("/api/substitutions", getSubstitutions); app.get("/api/history", getHistory); app.get("/api/classes", getClasses); app.post("/api/token", auth.token); // Register Admin endpoints app.use("/api/admin", checkAdmin); registerAdmin(app); // Respond with 400 for non-existent endpoints app.get("/api/*", (_req, res) => { res.sendStatus(400); }); // Supply frontend files if any other url // is requested to make vue router work app.use("/", express.static("../dist")); app.use("/*", express.static("../dist")); app.listen(port, () => { console.log(`Server listening on http://localhost:${port}`); });