Skip to content

Commit

Permalink
Upgrade to use Deno.serve instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Tao-VanJS committed May 6, 2024
1 parent 3445f13 commit fb00760
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 36 deletions.
31 changes: 5 additions & 26 deletions demo/terminal/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/terminal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "deno run --allow-net --allow-run --allow-read server.ts --allowRemote --skipLogin"
},
"devDependencies": {
"deno-bin": "^1.36.0",
"deno-bin": "^1.43.1",
"vanjs-core": "^1.5.0"
}
}
12 changes: 3 additions & 9 deletions demo/terminal/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const readFileOrError = async (path: string) => {
}
}

const serveHttp = async (conn: Deno.Conn, req: Request) => {
if (!flags.allowRemote && (<Deno.NetAddr>conn.remoteAddr).hostname !== "127.0.0.1")
Deno.serve({port: Number(flags.port)}, async (req: Request, {remoteAddr}) => {
if (!flags.allowRemote && remoteAddr.hostname !== "127.0.0.1")
return new Response(
"Only local requests are allowed for security purposes", {status: 403})
const url = new URL(req.url)
Expand Down Expand Up @@ -110,12 +110,7 @@ const serveHttp = async (conn: Deno.Conn, req: Request) => {
}
}
return new Response("Unsupported HTTP method", {status: 404})
}

const serveConn = async (conn: Deno.Conn) => {
for await (const reqEvent of Deno.serveHttp(conn))
(async () => reqEvent.respondWith(await serveHttp(conn, reqEvent.request)))()
}
})

console.log(`Visit http://localhost:${flags.port}/ in your browser`)
if (!flags.skipLogin) {
Expand All @@ -124,4 +119,3 @@ if (!flags.skipLogin) {
console.log("%cFor security purposes, DO NOT share the key with anyone else",
"color: red; font-weight: bold")
}
for await (const conn of Deno.listen({port: Number(flags.port)})) serveConn(conn)

0 comments on commit fb00760

Please sign in to comment.