import index from "./src/index.html" import { Database } from "bun:sqlite" import { initDb, getEntriesData, saveEntry } from "./api" const db = new Database("data.db", { create: true }) initDb(db) const clients = new Set>() const server = Bun.serve({ routes: { "/": index, }, fetch(req, srv) { const { pathname } = new URL(req.url) if (pathname === "/ws") { if (srv.upgrade(req)) return new Response() return new Response("WebSocket upgrade failed", { status: 400 }) } return new Response("Not found", { status: 404 }) }, websocket: { open(ws) { clients.add(ws) ws.send(JSON.stringify({ type: "init", entries: getEntriesData(db) })) }, message(ws, msg) { const entry = JSON.parse(msg as string) saveEntry(db, entry) const payload = JSON.stringify({ type: "entry", entry }) for (const client of clients) client.send(payload) }, close(ws) { clients.delete(ws) }, }, development: { hmr: true, console: true }, }) console.log(`Listening on http://localhost:${server.port}`)