2023-08-26 00:50:19 +01:00
|
|
|
// OwlBoard - © Fred Boniface 2022-2023 - Licensed under GPLv3 (or later)
|
2023-02-09 20:34:53 +00:00
|
|
|
|
2023-08-26 00:50:19 +01:00
|
|
|
// Please see the included LICENSE file
|
2023-02-09 20:34:53 +00:00
|
|
|
|
2023-08-26 00:38:26 +01:00
|
|
|
const mode = process.env.NODE_ENV || "development";
|
2023-02-16 10:39:19 +00:00
|
|
|
|
2023-09-24 21:35:36 +01:00
|
|
|
// Logging
|
|
|
|
const logger = require("./src/utils/logger.utils");
|
|
|
|
logger.logger.info("Logger Initialised");
|
|
|
|
|
2023-02-09 20:34:53 +00:00
|
|
|
// External Requires
|
2023-08-26 00:38:26 +01:00
|
|
|
const express = require("express");
|
|
|
|
const app = express();
|
2023-05-06 21:54:49 +01:00
|
|
|
|
|
|
|
// Middleware
|
2023-08-26 00:38:26 +01:00
|
|
|
const compression = require("compression");
|
|
|
|
const rateLimit = require("express-rate-limit");
|
|
|
|
const cors = require("cors");
|
|
|
|
const authenticate = require("./src/middlewares/auth.middlewares");
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Internal Requires
|
2023-08-26 00:38:26 +01:00
|
|
|
const version = require("./src/configs/version.configs");
|
|
|
|
const listRtr = require("./src/routes/list.routes");
|
|
|
|
const ldbRtr = require("./src/routes/ldb.routes");
|
|
|
|
const ldbsRtr = require("./src/routes/ldbs.routes");
|
|
|
|
const kubeRtr = require("./src/routes/kube.routes");
|
|
|
|
const findRtr = require("./src/routes/find.routes");
|
|
|
|
const issueRtr = require("./src/routes/issue.routes");
|
|
|
|
const statRtr = require("./src/routes/stats.routes");
|
|
|
|
const regRtr = require("./src/routes/registration.routes");
|
|
|
|
const pisRtr = require("./src/routes/pis.routes");
|
|
|
|
const trainRtr = require("./src/routes/train.routes");
|
|
|
|
const pis2Rtr = require("./src/routes/pis2.routes"); // API Version 2 Routes
|
|
|
|
const ref2Rtr = require("./src/routes/ref2.routes"); // API Version 2 Routes
|
|
|
|
const live2Rtr = require("./src/routes/live2.routes"); // API Version 2 Routes
|
|
|
|
const tt2Rtr = require("./src/routes/timetable2.routes"); // API Version 2
|
|
|
|
const user2Rtr = require("./src/routes/user2.routes"); // API Version 2 Routes
|
|
|
|
const miscRtr = require("./src/routes/misc.routes"); // Non-Public API Routes
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Set Server Configurations
|
2023-08-26 00:38:26 +01:00
|
|
|
const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0";
|
|
|
|
const srvPort = process.env.OWL_SRV_PORT || 8460;
|
2023-05-06 21:54:49 +01:00
|
|
|
|
|
|
|
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
|
2023-09-10 21:42:20 +01:00
|
|
|
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
|
2023-05-06 21:54:49 +01:00
|
|
|
});
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Print version number:
|
2023-09-24 21:35:36 +01:00
|
|
|
logger.logger.info(`Starting version ${version.app} in ${mode} mode`);
|
2023-02-09 20:34:53 +00:00
|
|
|
|
2023-09-10 21:41:50 +01:00
|
|
|
// Remove X-Powered-By header:
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
2023-02-09 20:34:53 +00:00
|
|
|
// Express Error Handling:
|
2023-07-28 20:51:43 +01:00
|
|
|
app.use((err, req, res, next) => {
|
2023-02-09 20:34:53 +00:00
|
|
|
const statusCode = err.statuscode || 500;
|
2023-09-26 20:36:04 +01:00
|
|
|
logger.logger.error(err, "Express Error")
|
2023-08-26 00:38:26 +01:00
|
|
|
res.status(statusCode).json({ message: err.message });
|
2023-02-09 20:34:53 +00:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
2023-05-06 21:54:49 +01:00
|
|
|
// Global Middleware:
|
2023-08-26 00:38:26 +01:00
|
|
|
app.use(
|
|
|
|
cors({
|
|
|
|
origin: "*", //[/\.owlboard\.info$/, 'localhost:5173', 'localhost:4173']
|
|
|
|
})
|
|
|
|
);
|
2023-02-09 20:34:53 +00:00
|
|
|
app.use(express.json()); //JSON Parsing for POST Requests
|
2023-05-06 21:54:49 +01:00
|
|
|
app.use(compression()); // Compress API Data if supported by client
|
|
|
|
app.use(limiter);
|
2023-07-08 18:39:24 +01:00
|
|
|
app.use(authenticate);
|
2023-02-09 20:34:53 +00:00
|
|
|
|
2023-06-17 22:14:58 +01:00
|
|
|
// 2023 Rationalisation Routes (/api/v2, /misc)
|
2023-08-26 00:38:26 +01:00
|
|
|
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.)
|
2023-06-17 22:14:58 +01:00
|
|
|
|
2023-05-06 21:54:49 +01:00
|
|
|
// Unauthenticated Routes
|
2023-08-26 00:38:26 +01:00
|
|
|
app.use("/api/v1/list", listRtr);
|
|
|
|
app.use("/api/v1/ldb", ldbRtr);
|
|
|
|
app.use("/api/v1/kube", kubeRtr);
|
|
|
|
app.use("/api/v1/find", findRtr);
|
|
|
|
app.use("/api/v1/issue", issueRtr);
|
|
|
|
app.use("/api/v1/stats", statRtr);
|
|
|
|
app.use("/api/v1/register", regRtr);
|
2023-05-06 21:54:49 +01:00
|
|
|
|
|
|
|
// Authented Routes
|
2023-08-26 00:38:26 +01:00
|
|
|
app.use("/api/v1/ldbs", authenticate, ldbsRtr);
|
|
|
|
app.use("/api/v1/pis", authenticate, pisRtr);
|
|
|
|
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.
|
|
|
|
app.use("/api/v1/train", authenticate, trainRtr);
|
2023-05-06 21:54:49 +01:00
|
|
|
|
|
|
|
// Number of proxies:
|
2023-08-26 00:38:26 +01:00
|
|
|
app.set("trust proxy", 4);
|
|
|
|
mode === "development"
|
|
|
|
? app.get("/api/v1/ip", (req, res) => res.send(req.ip))
|
2023-05-06 21:54:49 +01:00
|
|
|
: null;
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Start Express
|
2023-08-26 00:38:26 +01:00
|
|
|
app.listen(srvPort, srvListen, (error) => {
|
|
|
|
if (!error) {
|
2023-09-24 21:35:36 +01:00
|
|
|
logger.logger.info(`Listening on http://${srvListen}:${srvPort}`);
|
2023-02-09 20:34:53 +00:00
|
|
|
} else {
|
2023-09-24 21:35:36 +01:00
|
|
|
logger.logger.error(error, `Error starting server`);
|
2023-02-09 20:34:53 +00:00
|
|
|
}
|
2023-08-26 00:38:26 +01:00
|
|
|
});
|