Files
bworld/server/main.ts
T
2026-09-26 13:25:34 -03:00

42 lines
1020 B
TypeScript

// a dedicated server: the host from server/host.ts on a port, saving the world when it's stopped
import { type Host, start_host } from "./host.ts";
const PORT = Number(Deno.env.get("PORT") ?? 8000);
let host: Host;
try {
host = start_host({
build_dir: Deno.env.get("BUILD_DIR") ?? "build",
server_mods_dir: Deno.env.get("SERVER_MODS_DIR") ?? "server_mods",
root: ".",
world_file: Deno.env.get("WORLD_FILE") ?? "world.json",
seed: Deno.env.get("SEED"),
on_fatal(message) {
console.error(message);
Deno.exit(1);
},
});
} catch (e) {
console.error((e as Error).message);
Deno.exit(1);
}
async function shutdown() {
try {
await host.shutdown();
Deno.exit(0);
} catch (e) {
console.error((e as Error).message);
Deno.exit(1);
}
}
Deno.addSignalListener("SIGINT", shutdown);
if (Deno.build.os !== "windows") {
Deno.addSignalListener("SIGTERM", shutdown);
}
Deno.serve(
{ port: PORT, onListen: ({ port }) => console.log(`bworld server on http://localhost:${port}/`) },
host.handle,
);