diff --git a/bun.lockb b/bun.lockb index 429a487..dcce92a 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 5a1c2fd..eba0420 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "events": "^3.3.0", "fancy-text-converter": "^1.0.9", "keccak": "^2.1.0", + "nunjucks": "^3.2.4", "unique-names-generator": "^4.7.1", "yaml": "^2.3.2", "zod": "^3.22.2" @@ -22,6 +23,7 @@ "devDependencies": { "@types/bun": "latest", "@types/node": "^20.5.9", + "@types/nunjucks": "^3.2.6", "@typescript-eslint/eslint-plugin": "^6.19.1", "@typescript-eslint/parser": "^6.19.1", "eslint": "^8.56.0", diff --git a/public b/public index af660e9..0f21e39 160000 --- a/public +++ b/public @@ -1 +1 @@ -Subproject commit af660e9834493b178876dd959d5e3fc97e96bbc2 +Subproject commit 0f21e390667589f7acfef4d82a8f270a830822ee diff --git a/src/util/motd.ts b/src/util/motd.ts new file mode 100644 index 0000000..a47fe74 --- /dev/null +++ b/src/util/motd.ts @@ -0,0 +1,3 @@ +export function getMOTD() { + return "This site is loud!"; +} diff --git a/src/util/readline.ts b/src/util/readline/Command.ts similarity index 59% rename from src/util/readline.ts rename to src/util/readline/Command.ts index 246f3e9..b7e706f 100644 --- a/src/util/readline.ts +++ b/src/util/readline/Command.ts @@ -1,17 +1,6 @@ -import readline from "readline"; -import { Logger } from "./Logger"; +import logger from "./logger"; -export const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout -}); - -const logger = new Logger("CLI"); - -rl.setPrompt("mpps> "); -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