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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import { Application, Router, RouterContext, send } from "https://deno.land/x/oak/mod.ts";
import { api } from "./room.ts";
const app = new Application()
const root = new Router()
let bundleFiles: Record<string, string> = {}
root.get("/", async c => { await c.send({ path: "index.html", root: `${Deno.cwd()}/public` }) })
root.get("/room/:id", async c => { await c.send({ path: "index.html", root: `${Deno.cwd()}/public` }) })
root.get("/licen(c|s)e", async c => {
c.response.body = await Deno.readTextFile("LICENCE")
c.response.headers.set("Content-Type", "text/plain")
})
root.get("/favicon.ico", c => { c.response.status = 204 })
// deno-lint-ignore no-explicit-any
function respondWithType(mimeType: string, f: () => string): (c: RouterContext<any, any, any>) => void {
return c => {
c.response.headers.set("Content-Type", mimeType)
c.response.body = f()
}
}
root.get("/bundle.js", respondWithType("application/javascript", () => bundleFiles["deno:///bundle.js"]))
root.get("/bundle.js.map", respondWithType("application/javascript", () => bundleFiles["deno:///bundle.js.map"]))
function mountFilesystem(r: Router, route: string, path: string) {
r.get(route + "/(.*)", async (context) => {
await send(context, context.request.url.pathname, { root: Deno.cwd() + path });
})
}
mountFilesystem(root, "/style", "/public")
mountFilesystem(root, "/rnnoise", "/public")
root.use(api.routes())
app.use(root.routes())
app.use(root.allowedMethods())
app.addEventListener("listen", ({ hostname, port, secure }) => {
console.log(`listening on: ${secure ? "https://" : "http://"}${hostname}:${port}`);
});
app.listen({
hostname: Deno.env.get("HOSTNAME") ?? "127.0.0.1",
port: parseInt(Deno.env.get("PORT") ?? "8080")
});
let refresh_needed = false
let refresh_pending = false
async function refresh() {
refresh_needed = true
if (refresh_pending) return
refresh_needed = false
refresh_pending = true
try {
const { files } = await Deno.emit("source/client/index.ts", { bundle: "module", check: false })
bundleFiles = files
} catch (e) { console.error(e) }
refresh_pending = false
if (refresh_needed) refresh()
}
refresh()
for await (const event of Deno.watchFs("source/client")) {
if (event.kind == "modify" || event.kind == "create") {
refresh()
}
}
|