🎨 clean up server bundle

This commit is contained in:
Flam3rboy 2021-08-14 23:15:19 +02:00
parent 2ca550d0c9
commit 8cde32ec1a
8 changed files with 121 additions and 103 deletions

BIN
bundle/package-lock.json generated

Binary file not shown.

View File

@ -10,8 +10,7 @@
"build:api": "cd ../api/ && npm run build",
"build:cdn": "cd ../cdn/ && npm run build",
"build:gateway": "cd ../gateway/ && npm run build",
"bundle": "npm run build && node dist/start.js",
"start": "npm run build && node dist/root.js",
"start": "npm run build && node dist/start.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
@ -36,8 +35,7 @@
"@types/node-os-utils": "^1.2.0",
"async-exit-hook": "^2.0.1",
"express": "^4.17.1",
"link": "^0.1.5",
"mongodb-memory-server-global-4.4": "^7.3.6",
"mongodb-memory-server": "^7.3.6",
"node-os-utils": "^1.3.5",
"typescript": "^4.3.5"
}

View File

@ -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);

43
bundle/src/Database.ts Normal file
View File

@ -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);
}

View File

@ -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}`,
},
});

View File

@ -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);

View File

@ -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");

22
bundle/src/stats.ts Normal file
View File

@ -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);
}