♻️ Refactor backend and add comments
This commit is contained in:
@ -1,4 +1,8 @@
|
||||
import express from "express";
|
||||
import cors from "cors";
|
||||
import cookieParser from "cookie-parser";
|
||||
|
||||
// Import API endpoints
|
||||
import {
|
||||
getTimetable,
|
||||
getSubstitutions,
|
||||
@ -7,41 +11,51 @@ import {
|
||||
} from "./api/index.js";
|
||||
import { Auth } from "./api/auth.js";
|
||||
import { Parser } from "./parser/index.js";
|
||||
import cors from "cors";
|
||||
import cookieParser from "cookie-parser";
|
||||
|
||||
// Check DSB Mobile credentials are supplied
|
||||
if (!process.env.DSB_USER || !process.env.DSB_PASSWORD) {
|
||||
console.error("Error: DSB 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 }));
|
||||
|
||||
const auth = new Auth();
|
||||
|
||||
if (!process.env.DSB_USER || !process.env.DSB_PASSWORD) {
|
||||
console.error("Error: DSB Auth environment variables missing!");
|
||||
process.exit(1);
|
||||
}
|
||||
const parser = new Parser(
|
||||
// Initialize the Parser and set it to update the
|
||||
// substitution plan at the specified update interval
|
||||
new Parser(
|
||||
process.env.DSB_USER,
|
||||
process.env.DSB_PASSWORD,
|
||||
process.env.UPDATE_INTERVAL || 1 * 60 * 1000
|
||||
process.env.UPDATE_INTERVAL || 1 * 60 * 1000 // Default to 1 minute
|
||||
);
|
||||
|
||||
// Create new Auth class to store sessions
|
||||
const auth = new Auth();
|
||||
app.post("/login", auth.login);
|
||||
|
||||
// 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/timetable", getTimetable);
|
||||
app.get("/api/substitutions", getSubstitutions);
|
||||
app.get("/api/history", getHistory);
|
||||
app.get("/api/classes", getClasses);
|
||||
// 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"));
|
||||
|
||||
|
Reference in New Issue
Block a user