// OwlBoard - © Fred Boniface 2022-2023 - Licensed under GPLv3 (or later) // Please see the included LICENSE file const mode = process.env.NODE_ENV || "development"; // Logging const logger = require("./src/utils/logger.utils"); logger.logger.info("Logger Initialised"); // External Requires const express = require("express"); const app = express(); // Middleware const compression = require("compression"); const rateLimit = require("express-rate-limit"); const cors = require("cors"); const authenticate = require("./src/middlewares/auth.middlewares"); // Internal Requires const version = require("./src/configs/version.configs"); const pis2Rtr = require("./src/routes/pis.routes"); // API Version 2 Routes const ref2Rtr = require("./src/routes/ref.routes"); // API Version 2 Routes const live2Rtr = require("./src/routes/live.routes"); // API Version 2 Routes const tt2Rtr = require("./src/routes/timetable.routes"); // API Version 2 const user2Rtr = require("./src/routes/user.routes"); // API Version 2 Routes const miscRtr = require("./src/routes/misc.routes"); // Non-Public API Routes // Set Server Configurations const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0"; const srvPort = process.env.OWL_SRV_PORT || 8460; const limiter = rateLimit({ windowMs: 15 * (60 * 1000), // 15 minutes max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes) standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers legacyHeaders: false, // Disable the `X-RateLimit-*` headers }); // Print version number: logger.logger.info(`Starting version ${version.app} in ${mode} mode`); // Remove X-Powered-By header: app.disable("x-powered-by"); // Express Error Handling: app.use((err, req, res, next) => { const statusCode = err.statuscode || 500; logger.logger.error(err, "Express Error"); res.status(statusCode).json({ message: err.message }); return; }); // Global Middleware: app.use( cors({ origin: "*", //[/\.owlboard\.info$/, 'localhost:5173', 'localhost:4173'] }) ); app.use(express.json()); //JSON Parsing for POST Requests app.use(compression()); // Compress API Data if supported by client app.use(limiter); app.use(authenticate); // 2023 Rationalisation Routes (/api/v2, /misc) app.use("/api/v2/pis", pis2Rtr); // API Version 2 app.use("/api/v2/live", live2Rtr); // API Version 2 app.use("/api/v2/ref", ref2Rtr); // API Version 2 app.use("/api/v2/timetable", tt2Rtr); // API Version 2 app.use("/api/v2/user", user2Rtr); // API Version 2 app.use("/misc", miscRtr); // Non public-api endpoints (Stats, Issue, etc.) app.use("/api/v1/auth/test", authenticate, (req, res) => res.status(200).json({ status: "ok", message: "Authentication successful", }) ); // Returns 401 if auth failed, 200 if successful. // Number of proxies: app.set("trust proxy", 4); mode === "development" ? app.get("/api/v1/ip", (req, res) => res.send(req.ip)) : null; // Start Express app.listen(srvPort, srvListen, (error) => { if (!error) { logger.logger.info(`Listening on http://${srvListen}:${srvPort}`); } else { logger.logger.error(error, `Error starting server`); } });