-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (55 loc) · 1.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Module Imports */
import express from "express";
import dotenv from "dotenv";
dotenv.config();
/* Import Classes */
import Station from "./modules/classes/Station.js";
import StationType from "./modules/classes/StationType.js";
/* Initialise Variables */
const port = process.env.PORT || 3000;
const server_type = process.env.SERVER_TYPE || "master";
const station_id = process.env.STATION_ID || -1;
/* Initialise Express */
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
/* Import Routers */
const masterRouter = await import("./routers/master.js").then((module) => module.router);
/* Import Custom Modules */
import log from "./modules/utils/log.js";
import db from "./modules/utils/db.js";
/* Run Express Server */
app.listen(port, async () => {
try {
await db.query("SELECT 1");
log("Database connection established.");
} catch (error) {
log("Database connection failed.");
log(error);
}
if(server_type == "master") {
app.use("/", masterRouter);
log("Master server started.");
} else if (server_type == "client") {
if(station_id == -1) {
log("No station ID provided. Exiting.");
process.exit(1);
}
const [stationData] = await db.query("SELECT * FROM stations WHERE id = ?", [station_id]);
if(stationData.length == 0) {
log("Invalid station ID provided. Exiting.");
process.exit(1);
}
const station = new Station(stationData[0].id, stationData[0].name, stationData[0].avgtime, stationData[0].station_type);
log(`Station ID: ${station_id}`);
log(`Station Name: ${await station.getName()}`);
app.use("/", await import("./routers/client.js").then((module) => module.default));
} else {
log("Invalid server type provided. Exiting.");
process.exit(1);
}
log(`Server is running on port ${port}.`)
});
export {
station_id
}