Binato/Binato.ts

61 lines
2 KiB
TypeScript
Raw Normal View History

2022-11-19 01:06:03 +00:00
console.clear();
import { ConsoleHelper } from "./ConsoleHelper";
import { readFileSync, existsSync } from "fs";
if (!existsSync("./config.json")) {
ConsoleHelper.printError("You must have a config file in the root of Binato's folder structure.");
ConsoleHelper.printError("Check the GitHub for an example file");
process.exit(1);
}
2023-09-10 12:59:22 +01:00
import ChatHistory from "./server/ChatHistory";
import Config from "./server/interfaces/Config";
import HandleRequest from "./server/BanchoServer";
2022-11-16 11:59:23 +00:00
import { Registry, collectDefaultMetrics } from "prom-client";
2023-09-10 12:59:22 +01:00
import http from "http";
2022-11-27 17:36:55 +00:00
const config:Config = JSON.parse(readFileSync(__dirname + "/config.json").toString()) as Config;
2022-11-16 11:59:23 +00:00
if (config["prometheus"]["enabled"]) {
const register:Registry = new Registry();
register.setDefaultLabels({ app: "nodejs_binato" });
collectDefaultMetrics({ register });
2023-09-10 12:59:22 +01:00
const prometheusServer = http.createServer(async (req, res) => {
if (req.method === "GET") {
res.end(await register.metrics());
}
2022-11-16 11:59:23 +00:00
});
2023-09-10 12:59:22 +01:00
prometheusServer.listen(config["prometheus"]["port"], () => ConsoleHelper.printInfo(`Prometheus metrics listening at port ${config["prometheus"]["port"]}`));
2022-11-16 11:59:23 +00:00
} else {
ConsoleHelper.printWarn("Prometheus is disabled!");
}
2022-11-16 15:25:46 +00:00
const INDEX_PAGE:string = readFileSync("./web/serverPage.html").toString();
2023-09-10 12:59:22 +01:00
const binatoServer = http.createServer((req, res) => {
2022-11-16 11:59:23 +00:00
let packet:Buffer = Buffer.alloc(0);
req.on("data", (chunk:Buffer) => packet = Buffer.concat([packet, chunk], packet.length + chunk.length));
req.on("end", () => {
switch (req.method) {
case "GET":
if (req.url == "/" || req.url == "/index.html" || req.url == "/index") {
2023-09-10 12:59:22 +01:00
res.end(INDEX_PAGE);
2022-11-16 11:59:23 +00:00
} else if (req.url == "/chat") {
2023-08-20 13:03:01 +01:00
// I don't think this works??
2023-09-10 12:59:22 +01:00
res.end(ChatHistory.GenerateForWeb());
2022-11-16 11:59:23 +00:00
}
2023-09-10 12:59:22 +01:00
break;
2022-11-16 11:59:23 +00:00
case "POST":
2022-11-17 00:29:07 +00:00
HandleRequest(req, res, packet);
2023-09-10 12:59:22 +01:00
break;
2022-11-16 11:59:23 +00:00
default:
2023-09-10 12:59:22 +01:00
res.writeHead(405);
res.end("Method not allowed");
break;
2022-11-16 11:59:23 +00:00
}
});
2022-11-17 00:29:07 +00:00
});
2023-09-10 12:59:22 +01:00
binatoServer.listen(config.http.port, () => ConsoleHelper.printInfo(`Binato is up! Listening at port ${config.http.port}`));