minecluster/lib/Minecluster.js

52 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

// Imports
import fig from "figlet";
import http from "http";
import express from "express";
import { INFO, OK, logInfo } from "./util/logging.js";
// Import Core Modules
import buildRoutes from "./server/router.js";
import injectSockets from "./server/sockets.js";
import pg from "./database/postgres.js";
// Constants
const title = "MCL";
const port = process.env.MCL_DEV_PORT ?? 52000;
// Class
export default class Minecluster {
constructor(options = {}) {
for (var k in options) this[k] = options[k];
this.port = options.port ?? port;
}
async _preinitialize() {
logInfo(fig.textSync(title, "Larry 3D"));
INFO("INIT", "Initializing...");
this.app = express();
this.pg = pg;
this.server = http.createServer(this.app);
this.sockets = injectSockets(this.server, this.jobs);
this.routes = buildRoutes(this.sockets);
this.app.use(this.routes);
OK("INIT", "Initialized!");
}
async _connect() {
await this.pg.connect();
}
start() {
const mcl = this;
return new Promise(async function init(res) {
mcl._preinitialize();
await mcl._connect();
mcl.server.listen(mcl.port, function onStart() {
OK("SERVER", `Running on ${mcl.port}`);
res();
});
});
}
}