summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorApexo2026-03-29 14:09:51 +0200
committerApexo2026-03-29 14:10:20 +0200
commitdf5d6c3bcf50af615684189988d3d9cfa9da1c0f (patch)
tree3d6f92bf3e6c9d9965f4f56b7c0c2a73a37703a4
parentfix vite build (diff)
downloadanduril-sim-df5d6c3bcf50af615684189988d3d9cfa9da1c0f.tar.gz
anduril-sim-df5d6c3bcf50af615684189988d3d9cfa9da1c0f.tar.bz2
anduril-sim-df5d6c3bcf50af615684189988d3d9cfa9da1c0f.zip
simple server
-rw-r--r--package.json1
-rw-r--r--server.mjs49
2 files changed, 50 insertions, 0 deletions
diff --git a/package.json b/package.json
index 3e87f53..bdc519a 100644
--- a/package.json
+++ b/package.json
@@ -4,6 +4,7 @@
"type": "module",
"scripts": {
"build": "(cd anduril && [ -d arch/dfp ] || make dfp && make hank-emisar-d3aa) && vite build && cp -t dist index.html anduril/hex/anduril.hank-emisar-d3aa.hex",
+ "serve": "npm run build && node server.mjs",
"dev": "vite build --watch"
},
"dependencies": {
diff --git a/server.mjs b/server.mjs
new file mode 100644
index 0000000..2b1334d
--- /dev/null
+++ b/server.mjs
@@ -0,0 +1,49 @@
+import { createServer } from 'http';
+import { open } from "fs/promises";
+import path from 'path';
+import { fileURLToPath } from 'url';
+
+const MIME = {
+ '.html': 'text/html',
+ '.js': 'application/javascript',
+ '.map': 'application/json',
+ '.hex': 'text/plain',
+};
+const PORT = 8000;
+const ROOT = path.dirname(fileURLToPath(import.meta.url)) + "/dist";
+
+const notFound = (res) => {
+ res.writeHead(404);
+ res.end("Not found");
+}
+
+const error = (res) => {
+ res.writeHead(500);
+ res.end("Internal error");
+}
+
+createServer(async (req, res) => {
+ const urlPath = req.url.split('?')[0];
+ const file = path.join(ROOT, urlPath === '/' ? '/index.html' : urlPath);
+ if (!file.startsWith(ROOT + "/")) return notFound(res);
+
+ try {
+ await using fd = await open(file, "r");
+ res.writeHead(200, {
+ "Content-Type": MIME[path.extname(file)] ?? "text/plain"
+ });
+
+ for await (const chunk of fd.createReadStream()) {
+ res.write(chunk);
+ }
+ res.end();
+
+ } catch (e) {
+ if (e instanceof Error && e.code === "ENOENT") {
+ notFound(res);
+ } else {
+ console.error(e);
+ error(res);
+ }
+ }
+}).listen(PORT, () => console.log(`anduril-sim @ http://localhost:${PORT}`));