From 09a6eb5d0d34024ce89c75f777c9d3291a535e1b Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Fri, 26 Jan 2024 03:56:15 -0500 Subject: [PATCH] Move readline to commands and add something like jinja --- bun.lockb | Bin 65630 -> 67487 bytes package.json | 2 + public | 2 +- src/util/motd.ts | 3 ++ src/util/{readline.ts => readline/Command.ts} | 38 ++---------------- src/util/readline/commands.ts | 27 +++++++++++++ src/util/readline/index.ts | 24 +++++++++++ src/util/readline/logger.ts | 4 ++ src/ws/server.ts | 22 +++++++--- test/index.test.ts | 0 10 files changed, 81 insertions(+), 41 deletions(-) create mode 100644 src/util/motd.ts rename src/util/{readline.ts => readline/Command.ts} (59%) create mode 100644 src/util/readline/commands.ts create mode 100644 src/util/readline/index.ts create mode 100644 src/util/readline/logger.ts delete mode 100644 test/index.test.ts diff --git a/bun.lockb b/bun.lockb index 429a487a801ca02c2fd15e403ce955ef51de7a90..dcce92a7d04dea966baf744c3a10612bda0e3ca0 100755 GIT binary patch delta 12209 zcmeHNd0Z4%wyr8^q1kbP23rM{s3==2v`W(8zLcVdIvD^UeFc|K1P$mp%1qU8Vfp^;My!lc0fu;4G>r%CE?p<7fcagIgWe5?1{jSNMa^v1Mgh=LkZJ zRKLAT?a4N3>n>b=9@7be7kDR^%y&dqTMG*WYgulI zwaj62M>}8d<}L_+kcGvC7)TI)YAXny;P)Zfnp==OP79>BAk;dKxfISq@&H-Zf^2KK zP0-9=1w;74)A)xAoJS#-^z&$wi?OO^+y1{ByT5idc=2WS0}E|~%u z44!SXl|-^ureCEyOh(@;W59+Z~a)1;v@d z%8si3CA4#B?YTw8Wi~-L4bJ1TlC{7g971+5KkU-Wu@`06Mpl#vHE@$JEYW0SL2(w= zpd_-uUT7~DN(^fME4UE0I7PU@2A0_i%d9yFHCusl0PWmgT$F|B1z~QPwWOq45bQ-c z1@_#$@*U{sdFI%vE4km$3GwHKoC0fkxh-30#02<5$Scb2u-N7(t+16<3!{zdgf<7# z5koJ75`yDAg?9G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{(SGZ!+vTv?Gtvga+)(@zJ zb~ec$uW5Z59k6O~Bd~k;Q{YjnwBDb_!EE!VB{0wX(_WZ;{&Wqd!=HM+VNKSL^OSuv z^6uo~N5a(UlR0hl(-ZxrPv>QR@kNDabX@t`pO(L2GV4c;A{M2$R`p6hf)4RT1}Y;nhj6aKo3$3{wX;QIyAcW+qD`thXwX}iuT zLdRdwApzPkc3L219J5O4f#g1BlGN@db6TMGpr$+>yD9QmFxF{hd|V>0lhz?wKg~4j z@E3+1pWGg*j+YRJ_ss5K?Mz2@=DU;U{`u5(^@Juy-m9iH2=DR-(Qj?m(P(?+!Rq*dA)Mn_JXB&__E zlNLXVSv$gW#7zEgn#}r%q=gS|YOH+cBldvzDR_Xp$G%x04GE`~H#3s;14Mo|v-^$S zz4lx5<7v=OKQc6B_#4lKo<7zKR}vu-R}S^3m`YtJkVM>preVryLKax5-Goxj71(Qm%#;|{G?DX7s b@_T`ny_4Sl?mO9H`-Jxr "); -rl.prompt(); - -class Command { +export class Command { public static commands: Command[] = []; public static async handleCommand(line: string) { const args = line.split(" "); @@ -57,25 +46,4 @@ class Command { ) {} } -Command.addCommand( - new Command(["memory", "mem"], "mem", msg => { - const mem = process.memoryUsage(); - return `Memory: ${(mem.heapUsed / 1000 / 1000).toFixed(2)} MB used / ${( - mem.heapTotal / - 1000 / - 1000 - ).toFixed(2)} MB total`; - }) -); - -rl.on("line", async line => { - const out = await Command.handleCommand(line); - logger.info(out); - rl.prompt(); -}); - -rl.on("SIGINT", () => { - process.exit(); -}); - -(globalThis as unknown as any).rl = rl; +export default Command; diff --git a/src/util/readline/commands.ts b/src/util/readline/commands.ts new file mode 100644 index 0000000..cfda747 --- /dev/null +++ b/src/util/readline/commands.ts @@ -0,0 +1,27 @@ +import Command from "./Command"; + +Command.addCommand( + new Command(["help", "h", "commands", "cmds"], "help", msg => { + return ( + "Commands: " + + Command.commands.map(cmd => cmd.aliases[0]).join(" | ") + ); + }) +); + +Command.addCommand( + new Command(["memory", "mem"], "mem", msg => { + const mem = process.memoryUsage(); + return `Memory: ${(mem.heapUsed / 1000 / 1000).toFixed(2)} MB used / ${( + mem.heapTotal / + 1000 / + 1000 + ).toFixed(2)} MB total`; + }) +); + +Command.addCommand( + new Command(["stop", "exit"], "stop", msg => { + process.exit(); + }) +); diff --git a/src/util/readline/index.ts b/src/util/readline/index.ts new file mode 100644 index 0000000..9911ad8 --- /dev/null +++ b/src/util/readline/index.ts @@ -0,0 +1,24 @@ +import readline from "readline"; +import logger from "./logger"; +import Command from "./Command"; +import "./commands"; + +export const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +rl.setPrompt("mpps> "); +rl.prompt(); + +rl.on("line", async line => { + const out = await Command.handleCommand(line); + logger.info(out); + rl.prompt(); +}); + +rl.on("SIGINT", () => { + process.exit(); +}); + +(globalThis as unknown as any).rl = rl; diff --git a/src/util/readline/logger.ts b/src/util/readline/logger.ts new file mode 100644 index 0000000..2ee09c5 --- /dev/null +++ b/src/util/readline/logger.ts @@ -0,0 +1,4 @@ +import { Logger } from "../Logger"; + +export const logger = new Logger("CLI"); +export default logger; diff --git a/src/ws/server.ts b/src/ws/server.ts index f564c9b..9282ff0 100644 --- a/src/ws/server.ts +++ b/src/ws/server.ts @@ -3,13 +3,25 @@ import { createSocketID, createUserID } from "../util/id"; import fs from "fs"; import path from "path"; import { handleMessage } from "./message"; -import { decoder } from "../util/helpers"; import { Socket, socketsBySocketID } from "./Socket"; -import { serve, file } from "bun"; import env from "../util/env"; +import { getMOTD } from "../util/motd"; +import nunjucks from "nunjucks"; const logger = new Logger("WebSocket Server"); +async function getIndex() { + const index = Bun.file("./public/index.html"); + + const rendered = nunjucks.renderString(await index.text(), { + motd: getMOTD() + }); + + const response = new Response(rendered); + response.headers.set("Content-Type", "text/html"); + return response; +} + export const app = Bun.serve({ port: env.PORT, hostname: "0.0.0.0", @@ -30,13 +42,13 @@ export const app = Bun.serve({ if (data) { return new Response(data); } else { - return new Response(Bun.file("./public/index.html")); + return getIndex(); } } else { - return new Response(Bun.file("./public/index.html")); + return getIndex(); } } catch (err) { - return new Response(Bun.file("./public/index.html")); + return getIndex(); } } }, diff --git a/test/index.test.ts b/test/index.test.ts deleted file mode 100644 index e69de29..0000000