summary refs log tree commit diff
path: root/bundle/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-14 23:15:19 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-14 23:15:19 +0200
commitb9e545a445dfef8cf42d837df8e653cba7c1a736 (patch)
tree93fb093189bd663b18f35cd91e3a08c63db42411 /bundle/src
parentmove devDependencies to dependencies (diff)
downloadserver-b9e545a445dfef8cf42d837df8e653cba7c1a736.tar.xz
:art: clean up server bundle
Diffstat (limited to 'bundle/src')
-rw-r--r--bundle/src/BundledServer.ts41
-rw-r--r--bundle/src/Database.ts43
-rw-r--r--bundle/src/Server.ts22
-rw-r--r--bundle/src/root.ts33
-rw-r--r--bundle/src/start.ts57
-rw-r--r--bundle/src/stats.ts22
6 files changed, 119 insertions, 99 deletions
diff --git a/bundle/src/BundledServer.ts b/bundle/src/BundledServer.ts
new file mode 100644
index 00000000..6951c1c4
--- /dev/null
+++ b/bundle/src/BundledServer.ts
@@ -0,0 +1,41 @@
+process.on("unhandledRejection", console.error);
+process.on("uncaughtException", console.error);
+
+import http from "http";
+import { FosscordServer as APIServer } from "@fosscord/api";
+import { Server as GatewayServer } from "@fosscord/gateway";
+import { CDNServer } from "@fosscord/cdn/";
+import express from "express";
+import { Config } from "../../util/dist";
+
+const app = express();
+const server = http.createServer();
+const port = Number(process.env.PORT) || 8080;
+const production = true;
+server.on("request", app);
+
+// @ts-ignore
+const api = new APIServer({ server, port, production, app });
+// @ts-ignore
+const cdn = new CDNServer({ server, port, production, app });
+// @ts-ignore
+const gateway = new GatewayServer({ server, port, production });
+
+async function main() {
+	await Config.set({
+		cdn: {
+			endpointClient: "${location.host}",
+			endpoint: `http://localhost:${port}`,
+		},
+		gateway: {
+			endpointClient: '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}',
+			endpoint: `ws://localhost:${port}`,
+		},
+	});
+
+	await api.start();
+	await cdn.start();
+	await gateway.start();
+}
+
+main().catch(console.error);
diff --git a/bundle/src/Database.ts b/bundle/src/Database.ts
new file mode 100644
index 00000000..0efd2471
--- /dev/null
+++ b/bundle/src/Database.ts
@@ -0,0 +1,43 @@
+import fs from "fs";
+import { MongoMemoryServer } from "mongodb-memory-server";
+import path from "path";
+import exitHook from "async-exit-hook";
+console.log(process.arch, process.platform);
+if (process.arch == "ia32") {
+	Object.defineProperty(process, "arch", {
+		value: "x64",
+	});
+}
+
+export async function setupDatabase() {
+	const dbPath = path.join(__dirname, "..", "..", "db");
+	const dbName = "fosscord";
+	const storageEngine = "wiredTiger";
+	const port = 27020;
+	const ip = "127.0.0.1";
+	var mongod: MongoMemoryServer;
+	fs.mkdirSync(dbPath, { recursive: true });
+
+	exitHook((callback: any) => {
+		(async () => {
+			console.log(`Stopping MongoDB ...`);
+			await mongod.stop();
+			console.log(`Stopped MongoDB`);
+			callback();
+		})();
+	});
+
+	console.log(`[Database] starting ...`);
+	mongod = new MongoMemoryServer({
+		instance: {
+			port,
+			ip,
+			dbName,
+			dbPath,
+			storageEngine,
+			auth: false, // by default `mongod` is started with '--noauth', start `mongod` with '--auth'
+		},
+	});
+	await mongod.start();
+	process.env.MONGO_URL = mongod.getUri(dbName);
+}
diff --git a/bundle/src/Server.ts b/bundle/src/Server.ts
index 6951c1c4..42a3f3c3 100644
--- a/bundle/src/Server.ts
+++ b/bundle/src/Server.ts
@@ -1,35 +1,27 @@
 process.on("unhandledRejection", console.error);
 process.on("uncaughtException", console.error);
 
-import http from "http";
 import { FosscordServer as APIServer } from "@fosscord/api";
 import { Server as GatewayServer } from "@fosscord/gateway";
 import { CDNServer } from "@fosscord/cdn/";
-import express from "express";
 import { Config } from "../../util/dist";
 
-const app = express();
-const server = http.createServer();
-const port = Number(process.env.PORT) || 8080;
 const production = true;
-server.on("request", app);
 
-// @ts-ignore
-const api = new APIServer({ server, port, production, app });
-// @ts-ignore
-const cdn = new CDNServer({ server, port, production, app });
-// @ts-ignore
-const gateway = new GatewayServer({ server, port, production });
+const api = new APIServer({ production, port: Number(process.env.API_PORT) || 3001 });
+const gateway = new GatewayServer({ port: Number(process.env.GATEWAY_PORT) || 3002 });
+const cdn = new CDNServer({ production, port: Number(process.env.CDN_PORT) || 3003 });
 
 async function main() {
 	await Config.set({
 		cdn: {
 			endpointClient: "${location.host}",
-			endpoint: `http://localhost:${port}`,
+			endpoint: `http://localhost:${cdn.options.port}`,
 		},
 		gateway: {
-			endpointClient: '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}',
-			endpoint: `ws://localhost:${port}`,
+			endpointClient:
+				'${location.protocol === "https:" ? "wss://" : "ws://"}${location.hostname}:' + gateway.port,
+			endpoint: `ws://localhost:${gateway.port}`,
 		},
 	});
 
diff --git a/bundle/src/root.ts b/bundle/src/root.ts
deleted file mode 100644
index 42a3f3c3..00000000
--- a/bundle/src/root.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-process.on("unhandledRejection", console.error);
-process.on("uncaughtException", console.error);
-
-import { FosscordServer as APIServer } from "@fosscord/api";
-import { Server as GatewayServer } from "@fosscord/gateway";
-import { CDNServer } from "@fosscord/cdn/";
-import { Config } from "../../util/dist";
-
-const production = true;
-
-const api = new APIServer({ production, port: Number(process.env.API_PORT) || 3001 });
-const gateway = new GatewayServer({ port: Number(process.env.GATEWAY_PORT) || 3002 });
-const cdn = new CDNServer({ production, port: Number(process.env.CDN_PORT) || 3003 });
-
-async function main() {
-	await Config.set({
-		cdn: {
-			endpointClient: "${location.host}",
-			endpoint: `http://localhost:${cdn.options.port}`,
-		},
-		gateway: {
-			endpointClient:
-				'${location.protocol === "https:" ? "wss://" : "ws://"}${location.hostname}:' + gateway.port,
-			endpoint: `ws://localhost:${gateway.port}`,
-		},
-	});
-
-	await api.start();
-	await cdn.start();
-	await gateway.start();
-}
-
-main().catch(console.error);
diff --git a/bundle/src/start.ts b/bundle/src/start.ts
index ee012c15..581c3d9a 100644
--- a/bundle/src/start.ts
+++ b/bundle/src/start.ts
@@ -1,64 +1,19 @@
-import fs from "fs";
-import { MongoMemoryServer } from "mongodb-memory-server-global-4.4";
-import path from "path";
+process.env.MONGOMS_DEBUG = "true";
+
 import cluster from "cluster";
 import os from "os";
-import osu from "node-os-utils";
-import exitHook from "async-exit-hook";
+import { setupDatabase } from "./Database";
+import { initStats } from "./stats";
 
 // TODO: add tcp socket event transmission
 const cores = 1 || Number(process.env.threads) || os.cpus().length;
 
 if (cluster.isMaster && !process.env.masterStarted) {
-	const dbPath = path.join(__dirname, "..", "..", "db");
-	const dbName = "fosscord";
-	const storageEngine = "wiredTiger";
-	const port = 27020;
-	const ip = "127.0.0.1";
-	var mongod: MongoMemoryServer;
-	fs.mkdirSync(dbPath, { recursive: true });
-
-	exitHook((callback: any) => {
-		(async () => {
-			console.log(`Stopping MongoDB ...`);
-			await mongod.stop();
-			console.log(`Stopped MongoDB`);
-			callback();
-		})();
-	});
-
 	process.env.masterStarted = "true";
 
-	setInterval(async () => {
-		const [cpuUsed, memory, network] = await Promise.all([osu.cpu.usage(), osu.mem.info(), osu.netstat.inOut()]);
-		if (typeof network === "object") {
-			console.log(`Network: in ${network.total.inputMb}mb | out ${network.total.outputMb}mb`);
-		}
-
-		console.log(
-			`[CPU] ${cpuUsed.toFixed(2)}% | [Memory] ${memory.usedMemMb.toFixed(0)}mb/${memory.totalMemMb.toFixed(0)}mb`
-		);
-	}, 1000 * 60);
-
 	(async () => {
-		console.log(`[Database] starting ...`);
-		mongod = new MongoMemoryServer({
-			instance: {
-				port,
-				ip,
-				dbName,
-				dbPath,
-				storageEngine,
-				auth: false, // by default `mongod` is started with '--noauth', start `mongod` with '--auth'
-			},
-		});
-		await mongod.start();
-		process.env.MONGO_URL = mongod.getUri(dbName);
-
-		console.log(`[CPU] ${osu.cpu.model()} Cores x${osu.cpu.count()}`);
-		console.log(`[System] ${await osu.os.oos()} ${os.arch()}`);
-		console.log(`[Database] started`);
-		console.log(`[Process] running with pid: ${process.pid}`);
+		initStats();
+		await setupDatabase();
 
 		if (cores === 1) {
 			require("./Server.js");
diff --git a/bundle/src/stats.ts b/bundle/src/stats.ts
new file mode 100644
index 00000000..c621ed75
--- /dev/null
+++ b/bundle/src/stats.ts
@@ -0,0 +1,22 @@
+import os from "os";
+import osu from "node-os-utils";
+
+export function initStats() {
+	console.log(`[CPU] ${osu.cpu.model()} Cores x${osu.cpu.count()}`);
+	console.log(`[System] ${os.platform()} ${os.arch()}`);
+	console.log(`[Database] started`);
+	console.log(`[Process] running with pid: ${process.pid}`);
+
+	setInterval(async () => {
+		const [cpuUsed, memory, network] = await Promise.all([osu.cpu.usage(), osu.mem.info(), osu.netstat.inOut()]);
+		if (typeof network === "object") {
+			console.log(`[Network]: in ${network.total.inputMb}mb | out ${network.total.outputMb}mb`);
+		}
+
+		console.log(
+			`[CPU] ${cpuUsed.toFixed(2)}% | [Memory] ${Math.round(
+				process.memoryUsage().rss / 1024 / 1024
+			)}mb/${memory.totalMemMb.toFixed(0)}mb`
+		);
+	}, 1000 * 60);
+}