personal website
1import { serve } from "bun";
2import { readdir } from "fs/promises";
3import { buildVibesHtml } from "../build";
4import index from "./index.html";
5
6const server = serve({
7 routes: {
8 "/.well-known/atproto-did": async () => {
9 return new Response("did:plc:ttdrpj45ibqunmfhdsb4zdwq", {
10 headers: {
11 "Content-Type": "text/plain",
12 },
13 });
14 },
15
16 // Serve client-metadata.json for OAuth
17 "/client-metadata.json": async () => {
18 try {
19 const file = Bun.file("public/client-metadata.json");
20 return new Response(file, {
21 headers: { "Content-Type": "application/json" },
22 });
23 } catch {
24 return new Response("File not found", { status: 404 });
25 }
26 },
27
28 // Serve static files from public directory
29 "/nekomata.png": async () => {
30 try {
31 const file = Bun.file("public/nekomata.png");
32 return new Response(file);
33 } catch {
34 return new Response("File not found", { status: 404 });
35 }
36 },
37
38 "/vibes": async () => {
39 const files = (await readdir("public/vibes")).filter((f) => f !== "index.html");
40 return new Response(buildVibesHtml(files), { headers: { "Content-Type": "text/html" } });
41 },
42
43 "/vibes/*": async (req) => {
44 const url = new URL(req.url);
45 const file = Bun.file(`public${url.pathname}`);
46 if (await file.exists()) {
47 return new Response(file);
48 }
49 return new Response("Not found", { status: 404 });
50 },
51
52 // Serve index.html for all unmatched routes.
53 "/*": index,
54
55 "/api/hello": {
56 async GET(req) {
57 return Response.json({
58 message: "Hello, world!",
59 method: "GET",
60 });
61 },
62 async PUT(req) {
63 return Response.json({
64 message: "Hello, world!",
65 method: "PUT",
66 });
67 },
68 },
69
70 "/api/hello/:name": async req => {
71 const name = req.params.name;
72 return Response.json({
73 message: `Hello, ${name}!`,
74 });
75 },
76 },
77
78 development: process.env.NODE_ENV !== "production" && {
79 // Enable browser hot reloading in development
80 hmr: true,
81
82 // Echo console logs from the browser to the server
83 console: true,
84 },
85});
86
87console.log(`🚀 Server running at ${server.url}`);