Move readline to commands and add something like jinja
This commit is contained in:
parent
541fd4930b
commit
09a6eb5d0d
|
@ -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",
|
||||
|
|
2
public
2
public
|
@ -1 +1 @@
|
|||
Subproject commit af660e9834493b178876dd959d5e3fc97e96bbc2
|
||||
Subproject commit 0f21e390667589f7acfef4d82a8f270a830822ee
|
|
@ -0,0 +1,3 @@
|
|||
export function getMOTD() {
|
||||
return "This site is loud!";
|
||||
}
|
|
@ -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;
|
|
@ -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();
|
||||
})
|
||||
);
|
|
@ -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;
|
|
@ -0,0 +1,4 @@
|
|||
import { Logger } from "../Logger";
|
||||
|
||||
export const logger = new Logger("CLI");
|
||||
export default logger;
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue