Self-hosted, federated location sharing app and server that prioritizes user privacy and security
end-to-end-encryption
location-sharing
privacy
self-hosted
federated
1export let server: ReturnType<typeof Bun.spawn> | null = null;
2
3export const SERVER_DIR = `${import.meta.dir}/..`;
4
5export async function startOrRestartServer(): Promise<string | undefined> {
6 if (server !== null) {
7 server.kill();
8 server = null;
9 }
10
11 server = Bun.spawn({
12 cmd: ["cargo", "run"],
13 cwd: SERVER_DIR,
14 stdout: "pipe",
15 stderr: "inherit",
16 });
17
18 const log_reader = server.stdout.getReader();
19 const decoder = new TextDecoder();
20
21 // Key‑finder with timeout
22 const timeoutMs = 1000;
23 const start = Date.now();
24 let signupKey: string | undefined;
25
26 while (Date.now() - start < timeoutMs) {
27 const { value, done } = await log_reader.read();
28 if (done) break; // stream closed
29 const text = decoder.decode(value);
30 const match = text.match(/Admin signup key:\s*([^\s]+)/);
31 if (match) {
32 signupKey = match[1];
33 break;
34 }
35 if (text.includes("Server started")) {
36 break;
37 }
38 }
39
40 // Background logger. This runs until the server is stopped.
41 (async () => {
42 while (true) {
43 const { value, done } = await log_reader.read();
44 if (done) break;
45 process.stdout.write(decoder.decode(value));
46 }
47 })();
48
49 return signupKey;
50}
51
52export function stopServer() {
53 if (server !== null) {
54 server.kill();
55 server = null;
56 }
57}