feat: deployctl is not needed anymore (#45)

Deno Deploy now supports Deno.listen and Deno.serveHttp.
This commit is contained in:
Luca Casonato
2021-07-30 14:09:04 +02:00
committed by GitHub
parent 8d2935aef3
commit 3bce2fb8d3
14 changed files with 42 additions and 39 deletions
+19 -3
View File
@@ -14,7 +14,23 @@ export { router, ServerContext };
export function start(routes: Routes) {
const ctx = ServerContext.fromRoutes(routes);
const app = router.router(ctx.routes());
addEventListener("fetch", (event: FetchEvent) => {
event.respondWith(app(event.request));
});
serve(app);
}
async function serve(handler: (req: Request) => Response | Promise<Response>) {
async function handleConn(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
for await (const e of httpConn) {
e.respondWith(handler(e.request))
.catch((e) => console.error("Failed handing a request:", e));
}
}
const listener = Deno.listen({ port: 8000 });
const addr = listener.addr as Deno.NetAddr;
console.log(`Listening on http://${addr.hostname}:${addr.port}`);
for await (const conn of listener) {
handleConn(conn)
.catch((e) => console.error("Failed serving a connection:", e));
}
}