Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: serve() should log where it is listening #1641

Merged
merged 4 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions http/bench.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { listenAndServe } from "./server.ts";
import { serve } from "./server.ts";

const DEFAULT_PORT = 4500;
const addr = Deno.args[0] || `127.0.0.1:${DEFAULT_PORT}`;
const encoder = new TextEncoder();
const body = encoder.encode("Hello World");
const [hostname, p] = addr.split(":");
listenAndServe({
hostname,
port: parseInt(p ?? DEFAULT_PORT),
}, () => new Response(body));

console.log("Server listening on", addr);
serve(() => new Response(body), { port: (+p || DEFAULT_PORT), hostname });
10 changes: 0 additions & 10 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,16 +826,6 @@ function main(): void {
} else {
serve(handler, { port: Number(port), hostname: host });
}

const protocol = useTls ? "https" : "http";
console.log(
`${protocol.toUpperCase()} server listening on ${protocol}://${
host.replace(
"0.0.0.0",
"localhost",
)
}:${port}/`,
);
}

function printUsage() {
Expand Down
4 changes: 2 additions & 2 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function startFileServer({
assert(fileServer.stdout != null);
const r = new TextProtoReader(new BufReader(fileServer.stdout));
const s = await r.readLine();
assert(s !== null && s.includes("server listening"));
assert(s !== null && s.includes("Listening"));
}

async function startFileServerAsLibrary({}: FileServerCfg = {}) {
Expand Down Expand Up @@ -422,7 +422,7 @@ async function startTlsFileServer({
assert(fileServer.stdout != null);
const r = new TextProtoReader(new BufReader(fileServer.stdout));
const s = await r.readLine();
assert(s !== null && s.includes("server listening"));
assert(s !== null && s.includes("Listening"));
}

Deno.test("serveDirIndex TLS", async function () {
Expand Down
28 changes: 21 additions & 7 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ export async function serveListener(
return await server.serve(listener);
}

function hostnameForDisplay(hostname: string) {
// If the hostname is "0.0.0.0", we display "localhost" in console
// because browsers in Windows don't resolve "0.0.0.0".
// See the discussion in https://github.com/denoland/deno_std/issues/1165
return hostname === "0.0.0.0" ? "localhost" : hostname;
}

/** Serves HTTP requests with the given handler.
*
* You can specify an object with a port and hostname option, which is the address to listen on.
Expand All @@ -533,7 +540,6 @@ export async function serveListener(
* ```ts
* import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
* serve((_req) => new Response("Hello, world"), { port: 3000 });
* console.log("Listening on http://localhost:3000");
* ```
*
* @param handler The handler for individual HTTP requests.
Expand All @@ -543,9 +549,11 @@ export async function serve(
handler: Handler,
options: ServeInit = {},
): Promise<void> {
const port = options.port ?? 8000;
const hostname = options.hostname ?? "0.0.0.0";
const server = new Server({
port: options.port ?? 8000,
hostname: options.hostname ?? "0.0.0.0",
port,
hostname,
handler,
onError: options.onError,
});
Expand All @@ -554,7 +562,9 @@ export async function serve(
once: true,
});

return await server.listenAndServe();
const s = server.listenAndServe();
console.log(`Listening on http://${hostnameForDisplay(hostname)}:${port}/`);
return await s;
}

interface ServeTlsInit extends ServeInit {
Expand Down Expand Up @@ -598,9 +608,11 @@ export async function serveTls(
throw new Error("TLS config is given, but 'certFile' is missing.");
}

const port = options.port ?? 8443;
const hostname = options.hostname ?? "0.0.0.0";
const server = new Server({
port: options.port ?? 8443,
hostname: options.hostname ?? "0.0.0.0",
port,
hostname,
handler,
onError: options.onError,
});
Expand All @@ -609,7 +621,9 @@ export async function serveTls(
once: true,
});

return await server.listenAndServeTls(options.certFile, options.keyFile);
const s = server.listenAndServeTls(options.certFile, options.keyFile);
console.log(`Listening on https://${hostnameForDisplay(hostname)}:${port}/`);
return await s;
}

/**
Expand Down