-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.deno.ts
115 lines (98 loc) · 2.85 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// The authors disclaim copyright to this source code (they are ashamed to
// admit they wrote it)
import { serveDir } from "https://deno.land/[email protected]/http/file_server.ts";
import {
handleRequest as handleTgRequest,
handleTgWeb,
init as tgBotInit,
webhookPath as tgWebhookPath,
} from "./tgbot.deno.ts";
const indexContent = new TextDecoder().decode(
await Deno.readFile("index.html"),
);
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 === tgWebhookPath) {
await handleTgRequest(e);
return null;
}
if (Math.random() < 0.001) {
return new Response("Yo mama so fat she became a teapot", { status: 418 });
}
if (url.pathname === "/" || url.pathname === "/index.html") {
return new Response(indexContent, {
headers: {
"content-type": "text/html; charset=utf-8",
},
});
}
if (url.pathname === "/postele.html") {
return new Response(posteleContent, {
headers: {
"content-type": "text/html; charset=utf-8",
},
});
}
if (url.pathname === "/about") {
return new Response("", {
headers: {
Location: "/lore",
},
status: 301,
});
}
if (url.pathname === "/lore") {
url.pathname = "/lore.jpg";
e.request = new Request(url, e.request);
}
if (url.pathname.startsWith("/tgweb/")) {
return await handleTgWeb(e);
}
const resp = await serveDir(e.request, { fsRoot: "static", quiet: true });
if (![200, 301, 304].includes(resp.status)) {
if (resp.status !== 404) console.error(resp);
return new Response("Yo mama so fat she ate this page (404 Not Found)", {
status: 404,
headers: {
"content-type": "text/plain",
},
});
}
if (url.pathname.startsWith("/.well-known/matrix/")) {
resp.headers.set("Access-Control-Allow-Origin", "*");
resp.headers.set("Content-Type", "application/json");
}
return resp;
}
///////// MAIN /////////
await tgBotInit();
for await (const conn of Deno.listen({ port: 8000 })) {
handleHttp(conn).catch((err) => console.error(err));
}