-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.deno.ts
54 lines (47 loc) · 1.33 KB
/
server.deno.ts
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
import {
handleRequest as handleTgRequest,
init,
webhookPath,
} from "./tgbot.deno.ts";
async function handleHttp(conn: Deno.Conn) {
for await (const e of Deno.serveHttp(conn)) {
const start = performance.now();
const mockEvent: Deno.RequestEvent = {
request: e.request,
async respondWith(r) {
const resp = await r;
const end = performance.now();
console.log(
`${new Date().toISOString()} ${resp.status} ${e.request.method} ${
e.request.url
} ${(end - start).toFixed(1)}ms`
);
return await e.respondWith(resp);
},
};
handleEvent(mockEvent)
.then(async (response) => {
if (response !== null) {
await mockEvent.respondWith(response);
}
})
.catch((err) => console.error(err));
}
}
async function handleEvent(e: Deno.RequestEvent): Promise<Response | null> {
const url = new URL(e.request.url);
if (url.pathname === webhookPath) {
await handleTgRequest(e);
return null;
}
return new Response("t.me/bereal_notifications", {
status: 301,
headers: {
location: "https://t.me/bereal_notifications",
"content-type": "text/plain",
},
});
}
await init();
for await (const conn of Deno.listen({ port: 8000 }))
handleHttp(conn).catch((err) => console.error(err));