-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.ts
More file actions
57 lines (50 loc) · 1.28 KB
/
server.ts
File metadata and controls
57 lines (50 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { serve } from "bun";
import { version } from "./package.json";
import index from "./src/index.html";
const openBrowser = (url: string) => {
const platform = process.platform;
let command: string;
if (platform === "darwin") {
command = "open";
} else if (platform === "win32") {
command = "start";
} else {
command = "xdg-open";
}
Bun.spawn([command, url]);
};
const server = serve({
port: 3876,
routes: {
"/": index,
},
fetch(req, server) {
if (server.upgrade(req)) {
return;
}
},
websocket: {
message(ws, message) {
// When we receive a 'shutdown' message, close the server
if (message === "shutdown") {
console.log("Shutdown signal received. Exiting.");
ws.close();
server.stop(true);
process.exit(0);
}
},
close(ws) {
// If the client disconnects, shut down the server.
// This handles the case where the tab is closed abruptly.
console.log("Client disconnected. Exiting.");
server.stop(true);
process.exit(0);
},
},
});
const url = `http://localhost:${server.port}`;
console.log(
`Morphaweb v${version} is running at: ${url} (Cmd+click to open). `,
);
console.log(`Attempting to start in your default browser...`);
openBrowser(url);