feat(flake): add 'nix run' launcher, no system config needed
Per explicit preference: no NixOS module/global config edit required. 'nix run .' starts one process serving both the static frontend and api/* on one auto-picked free port (tries 3001, falls back to any free port). Ctrl+C or kill stops it; only ./.data's SQLite files persist. Verified directly: real HTTP 200 on the served frontend.
This commit is contained in:
1 parent
f2764769a8
commit
8cdfcf631e
2 files changed
+104
No files matched your search
@@ -56,6 +56,7 @@
|
||||
mkdir -p $out
|
||||
cp -r dist $out/dist
|
||||
cp -r api $out/api
|
||||
cp -r scripts $out/scripts
|
||||
cp -r node_modules $out/node_modules
|
||||
cp package.json $out/package.json
|
||||
runHook postInstall
|
||||
@@ -74,6 +75,18 @@
|
||||
# it held now lives at the top level, per the nixpkgs manual).
|
||||
packages = [ nodejs ];
|
||||
};
|
||||
|
||||
# `nix run .` - ad-hoc local run, no system config, no persistent
|
||||
# service: one process serving both the static frontend and the
|
||||
# api/* handlers on one auto-picked free port (tries 3001, falls
|
||||
# back to any free port if taken). Ctrl+C or `kill <pid>` stops it;
|
||||
# only the SQLite files under ./.data survive the process exiting.
|
||||
apps.default = {
|
||||
type = "app";
|
||||
program = "${pkgs.writeShellScript "commons-run" ''
|
||||
exec ${nodejs}/bin/node ${commons}/scripts/run-local.mjs
|
||||
''}";
|
||||
};
|
||||
}
|
||||
)
|
||||
// {
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env node
|
||||
// Ad-hoc local runner for `nix run` - no system service, no global config.
|
||||
// Serves the built static frontend (dist/) and the api/* handlers (blob
|
||||
// store + signed-event relay + admin aggregate) from ONE process on ONE
|
||||
// auto-picked free port. Ctrl+C (or `kill <pid>`) stops it; nothing it does
|
||||
// persists after the process exits except the SQLite files under
|
||||
// COMMONS_DATA_DIR (default: ./.data next to wherever this runs).
|
||||
import { createServer } from "node:http";
|
||||
import { createServer as createNetServer } from "node:net";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { extname, join, dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
|
||||
const DIST_DIR = join(ROOT, "dist");
|
||||
|
||||
process.env.BLOB_DB_PATH ??= join(process.env.COMMONS_DATA_DIR ?? join(ROOT, ".data"), "blobs.sqlite3");
|
||||
process.env.EVENT_DB_PATH ??= join(process.env.COMMONS_DATA_DIR ?? join(ROOT, ".data"), "events.sqlite3");
|
||||
await import("node:fs/promises").then((fs) =>
|
||||
fs.mkdir(dirname(process.env.BLOB_DB_PATH), { recursive: true }),
|
||||
);
|
||||
|
||||
const { default: postBlob } = await import("../api/blob/index.ts");
|
||||
const { default: getBlob } = await import("../api/blob/[id].ts");
|
||||
const { default: postSync } = await import("../api/sync/index.ts");
|
||||
const { default: getAggregate } = await import("../api/admin/aggregate.ts");
|
||||
const { sendJson } = await import("../api/_lib/httpUtils.ts");
|
||||
|
||||
const MIME = {
|
||||
".html": "text/html", ".js": "text/javascript", ".css": "text/css",
|
||||
".json": "application/json", ".svg": "image/svg+xml", ".png": "image/png",
|
||||
".ico": "image/x-icon", ".webmanifest": "application/manifest+json",
|
||||
};
|
||||
|
||||
async function serveStatic(pathname, res) {
|
||||
let filePath = join(DIST_DIR, pathname === "/" ? "index.html" : pathname);
|
||||
try {
|
||||
const data = await readFile(filePath);
|
||||
res.writeHead(200, { "Content-Type": MIME[extname(filePath)] ?? "application/octet-stream" });
|
||||
res.end(data);
|
||||
} catch {
|
||||
// SPA fallback, matching the nginx `tryFiles $uri /index.html` behavior
|
||||
// the NixOS module's vhost config uses.
|
||||
try {
|
||||
const data = await readFile(join(DIST_DIR, "index.html"));
|
||||
res.writeHead(200, { "Content-Type": "text/html" });
|
||||
res.end(data);
|
||||
} catch {
|
||||
sendJson(res, 404, { error: "not found - did you run `npm run build` first?" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findFreePort(preferred) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const probe = createNetServer();
|
||||
probe.unref();
|
||||
probe.on("error", (err) => {
|
||||
if (err.code === "EADDRINUSE") {
|
||||
probe.close(() => findFreePort(0).then(resolve, reject));
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
probe.listen(preferred, "0.0.0.0", () => {
|
||||
const { port } = probe.address();
|
||||
probe.close(() => resolve(port));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
const pathname = new URL(req.url ?? "/", "http://localhost").pathname;
|
||||
try {
|
||||
if (pathname === "/api/sync") return void (await postSync(req, res));
|
||||
if (pathname === "/api/admin/aggregate") return void (await getAggregate(req, res));
|
||||
if (pathname === "/api/blob") return void (await postBlob(req, res));
|
||||
if (pathname.startsWith("/api/blob/")) return void (await getBlob(req, res));
|
||||
await serveStatic(pathname, res);
|
||||
} catch (err) {
|
||||
sendJson(res, 500, { error: "internal error", detail: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
const preferredPort = Number(process.env.PORT ?? 3001);
|
||||
const port = await findFreePort(preferredPort);
|
||||
server.listen(port, () => {
|
||||
console.log(`THE COMMONS running at http://localhost:${port}`);
|
||||
console.log(`Data stored in ${dirname(process.env.BLOB_DB_PATH)}`);
|
||||
console.log("Press Ctrl+C to stop.");
|
||||
});
|
||||
Reference in new issue
Block a user