-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
97 lines (85 loc) · 2.88 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Built-in Node.js modules
const http = require("http");
const path = require("path");
const fs = require("fs");
const EventEmitter = require("events");
const { exec } = require('child_process');
// Third-party modules
const express = require("express");
require("dotenv").config();
// Local modules
const config = require("./config.json");
const { log } = require("./modules/utils.js");
// Express app and server setup
const app = express();
const httpServer = http.createServer(app);
const appJsPath = path.join(__dirname, 'routing', 'client', 'bundle.js');
if (!fs.existsSync(appJsPath)) {
console.log('Building project as bundle.js does not exist...');
exec('npm run build', (error, stdout, stderr) => {
if (error) {
console.error(`Error during build: ${error.message}`);
return;
}
if (stderr) {
console.error(`Build stderr: ${stderr}`);
return;
}
console.log(`Build stdout: ${stdout}`);
});
}
/**
* Global server object that holds various server configurations and states.
* @global
* @type {Object}
* @property {Array} worlds - An array to store world instances.
* @property {Array} plugins - An array to store plugin instances.
* @property {Object} config - Server configuration loaded from a JSON file.
* @property {Object} env - Environment variables from the process environment.
* @property {EventEmitter} events - EventEmitter instance to emit server events.
*/
global.server = {
worlds: [],
plugins: [],
config,
env: process.env,
events: new EventEmitter()
}
require("./modules/io.js")(httpServer);
require("./modules/plugins.js");
const files = [];
const getFilesRecursively = function(directory) {
const filesInDirectory = fs.readdirSync(directory);
for(let i = 0; i < filesInDirectory.length; i++) {
const file = filesInDirectory[i];
let absolute = path.join(directory, file);
if(fs.statSync(absolute).isDirectory()) {
getFilesRecursively(absolute);
} else {
files.push(absolute);
let routePath = '/' + path.relative("routing/client/", absolute).split(path.sep).join('/');
app.get(routePath, function(req, res) {
return res.sendFile(absolute, {
root: '.'
});
});
}
}
}
getFilesRecursively("./routing/client/");
// Route shared documents with the client
{
const srcPath = path.join(__dirname, "modules", "shared", "ranks.json");
const destPath = path.join(__dirname, 'client-src', 'shared', "ranks.json");
fs.copyFile(srcPath, destPath, (err) => {
if(err) throw err;
});
}
app.get("/:worldName?", (req, res) => {
return res.sendFile("./routing/client/index.html", {
root: '.'
});
});
httpServer.listen(config.port, () => {
log("INFO", `Server is running at *:${config.port}`);
});