Files
UDN/index.ts
T

94 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-07-05 19:53:19 +02:00
#!/usr/bin/env bun
2024-07-05 12:48:42 +02:00
import {
addConnection,
2024-07-05 16:04:12 +02:00
connectServers,
2024-07-05 12:48:42 +02:00
forgetConnection,
2024-07-05 16:49:08 +02:00
getStats,
2024-07-05 12:48:42 +02:00
processMessage,
} from "./websocket-handler";
2024-07-05 16:04:12 +02:00
import { writeInfo, writeStat, writeSuccess } from "./utility";
2024-07-04 17:27:46 +02:00
2024-07-05 12:48:42 +02:00
import Colors from "colors";
2024-07-05 13:17:19 +02:00
import IP from "ip";
2024-07-04 16:11:02 +02:00
import Path from "path";
2024-07-05 19:58:42 +02:00
import { ServerWebSocket } from "bun";
2024-07-05 16:04:12 +02:00
import { getConfig } from "./config-handler";
2024-07-04 16:11:02 +02:00
// CONFIG
2024-07-05 20:01:46 +02:00
const staticDir = Path.join(Path.dirname(import.meta.path), "static/dist");
2024-07-04 16:11:02 +02:00
const defaultPage = "index.html";
2024-07-04 17:27:46 +02:00
// TYPES
2024-07-05 16:04:12 +02:00
export type WS = ServerWebSocket<unknown> | WebSocket;
// MAIN
async function main() {
// CONFIG
const config = await getConfig();
// SERVER
const server = Bun.serve({
port: config.port,
fetch(request, server) {
// allow websockets
server.upgrade(request);
// respond
const requestPath = new URL(request.url).pathname;
switch (requestPath) {
case "/":
return createFileResponse(defaultPage);
2024-07-05 16:49:08 +02:00
case "/stats":
return new Response(JSON.stringify(getStats()));
2024-07-05 16:04:12 +02:00
default:
return createFileResponse(requestPath);
}
2024-07-04 17:27:46 +02:00
},
2024-07-05 16:04:12 +02:00
websocket: {
open() {
addConnection();
},
message(ws, message) {
processMessage(ws, message.toString(), config);
},
close(ws) {
forgetConnection(ws);
},
2024-07-04 16:18:40 +02:00
},
2024-07-05 16:04:12 +02:00
});
2024-07-04 16:11:02 +02:00
2024-07-05 16:04:12 +02:00
function createFileResponse(requestPath: string): Response {
const fullPath = Path.join(staticDir, requestPath);
return new Response(Bun.file(fullPath));
}
// OTHER SERVERS
connectServers(config);
// CLI
writeSuccess(`###\nstarted up ${new Date().toLocaleString()}`);
2024-07-05 20:03:07 +02:00
console.log("relevant output available in the 'logs' file");
2024-07-05 16:04:12 +02:00
function updateCLI() {
console.clear();
console.log(Colors.bold.bgWhite("UNIVERSAL DECENTRALIZED NETWORK"));
console.log();
2024-07-04 16:11:02 +02:00
2024-07-05 16:04:12 +02:00
console.log(new Date().toLocaleString());
console.log();
2024-07-05 13:17:19 +02:00
2024-07-05 16:04:12 +02:00
writeInfo("server url", Colors.bold.green(server.url.toString()));
writeInfo("server ip address", Colors.bold.green(IP.address()));
console.log();
2024-07-05 13:17:19 +02:00
2024-07-05 16:49:08 +02:00
getStats().forEach((entry) => writeStat(...entry));
2024-07-05 16:04:12 +02:00
}
2024-07-05 13:17:19 +02:00
2024-07-05 16:04:12 +02:00
setInterval(updateCLI, 5000);
2024-07-05 12:48:42 +02:00
}
2024-07-05 16:04:12 +02:00
main();