summary refs log tree commit diff
path: root/bundle/src
diff options
context:
space:
mode:
Diffstat (limited to 'bundle/src')
-rw-r--r--bundle/src/Server.ts33
-rw-r--r--bundle/src/start.ts74
2 files changed, 107 insertions, 0 deletions
diff --git a/bundle/src/Server.ts b/bundle/src/Server.ts
new file mode 100644
index 00000000..14abc128
--- /dev/null
+++ b/bundle/src/Server.ts
@@ -0,0 +1,33 @@
+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 api.start();
+	await cdn.start();
+	await gateway.start();
+
+	if (!Config.get().gateway.endpoint) await Config.set({ gateway: { endpoint: `ws://localhost:${port}` } });
+	if (!Config.get().cdn.endpoint) await Config.set({ cdn: { endpoint: `http://localhost:${port}` } });
+}
+
+main().catch(console.error);
diff --git a/bundle/src/start.ts b/bundle/src/start.ts
new file mode 100644
index 00000000..2ae2ce87
--- /dev/null
+++ b/bundle/src/start.ts
@@ -0,0 +1,74 @@
+import fs from "fs";
+import { MongoMemoryServer } from "mongodb-memory-server-global-4.4";
+import path from "path";
+import cluster from "cluster";
+import os from "os";
+import osu from "node-os-utils";
+import exitHook from "async-exit-hook";
+
+const cores = Number(process.env.threads) || 1 || 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}`);
+
+		// Fork workers.
+		for (let i = 0; i < cores; i++) {
+			cluster.fork();
+		}
+
+		cluster.on("exit", (worker: any, code: any, signal: any) => {
+			console.log(`[Worker] died with pid: ${worker.process.pid} , restarting ...`);
+			cluster.fork();
+		});
+	})();
+} else {
+	require("./Server.js");
+}