-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
58 lines (51 loc) · 1.55 KB
/
handler.ts
File metadata and controls
58 lines (51 loc) · 1.55 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
58
import { Application } from "./app.ts";
import { CORSHeaders, HomeTemplate } from "./constant.ts";
import { nostr } from "./deps.ts";
export function getHandler(app: Application) {
return (req: Request) => {
const url = new URL(req.url);
if (req.method === "OPTIONS") {
return new Response(null, { headers: CORSHeaders });
}
if (
!app.features.disable_nip11 &&
req.headers.get("accept") === "application/nostr+json" &&
url.pathname === "/"
) {
return new Response(
JSON.stringify(app.nip11),
{
headers: {
"content-type": "application/nostr+json",
...CORSHeaders,
},
},
);
}
if (req.headers.has("upgrade")) {
const { socket, response } = Deno.upgradeWebSocket(req);
app.addSocket(socket);
return response;
}
if (req.method === "GET" && url.pathname === "/") {
return new Response(
HomeTemplate.replaceAll("%name", app.nip11.name)
.replaceAll("%desc", app.nip11.description || "~")
.replaceAll(
"%admin",
app.nip11.pubkey ? nostr.nip19.npubEncode(app.nip11.pubkey) : "~",
)
.replaceAll(
"%bot",
app.botPubkey ? nostr.nip19.npubEncode(app.botPubkey) : "~",
)
.replaceAll(
"%url",
`${url.protocol === "https:" ? "wss:" : "ws:"}//${url.host}`,
),
{ headers: { "content-type": "text/plain" } },
);
}
return new Response(null, { status: 404 });
};
}