-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv.ts
40 lines (34 loc) · 958 Bytes
/
srv.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
import { getConfig, ServerOptions } from "./config/boot.ts";
import { createApplication } from "./application.ts";
export function srv(options: ServerOptions) {
const { location, controller, ...userOptions } = getConfig(options);
const application = createApplication(userOptions);
const server = Deno.listen(options);
controller.signal.addEventListener("abort", () => server.close());
async function handleConn(conn: Deno.Conn) {
if (controller.signal.aborted) {
conn.close();
return;
}
for await (const { request, respondWith } of Deno.serveHttp(conn)) {
try {
await respondWith(application.handle({ request }));
} catch {
// client hung up
}
}
}
function listen() {
(async () => {
for await (const conn of server) {
handleConn(conn);
}
})();
}
return {
...application,
location,
listen,
die: () => controller.abort(),
};
}