feat(server): pick free port if none specified (#1274)
This commit is contained in:
+4
-1
@@ -7,7 +7,10 @@ export {
|
||||
toFileUrl,
|
||||
} from "https://deno.land/std@0.190.0/path/mod.ts";
|
||||
export { walk } from "https://deno.land/std@0.190.0/fs/walk.ts";
|
||||
export { serve } from "https://deno.land/std@0.190.0/http/server.ts";
|
||||
export {
|
||||
type Handler as ServeHandler,
|
||||
serve,
|
||||
} from "https://deno.land/std@0.190.0/http/server.ts";
|
||||
export type {
|
||||
ConnInfo,
|
||||
Handler as RequestHandler,
|
||||
|
||||
+46
-6
@@ -1,6 +1,6 @@
|
||||
import { ServerContext } from "./context.ts";
|
||||
import * as colors from "https://deno.land/std@0.190.0/fmt/colors.ts";
|
||||
import { serve } from "./deps.ts";
|
||||
import { serve, ServeHandler } from "./deps.ts";
|
||||
export { Status } from "./deps.ts";
|
||||
import {
|
||||
AppModule,
|
||||
@@ -71,7 +71,6 @@ export async function createHandler(
|
||||
|
||||
export async function start(routes: Manifest, opts: StartOptions = {}) {
|
||||
const ctx = await ServerContext.fromManifest(routes, opts);
|
||||
opts.port ??= parseInt(Deno.env.get("PORT") || "8000");
|
||||
|
||||
if (!opts.onListen) {
|
||||
opts.onListen = (params) => {
|
||||
@@ -86,10 +85,51 @@ export async function start(routes: Manifest, opts: StartOptions = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
if (opts.experimentalDenoServe === true) {
|
||||
// @ts-ignore as `Deno.serve` is still unstable.
|
||||
await Deno.serve({ ...opts, handler: ctx.handler() });
|
||||
const portEnv = Deno.env.get("PORT");
|
||||
if (portEnv !== undefined) {
|
||||
opts.port ??= parseInt(portEnv, 10);
|
||||
}
|
||||
|
||||
const handler = ctx.handler();
|
||||
|
||||
if (opts.port) {
|
||||
await bootServer(handler, opts);
|
||||
} else {
|
||||
await serve(ctx.handler(), opts);
|
||||
// No port specified, check for a free port. Instead of picking just
|
||||
// any port we'll check if the next one is free for UX reasons.
|
||||
// That way the user only needs to increment a number when running
|
||||
// multiple apps vs having to remember completely different ports.
|
||||
let firstError;
|
||||
for (let port = 8000; port < 8020; port++) {
|
||||
try {
|
||||
await bootServer(handler, { ...opts, port });
|
||||
firstError = undefined;
|
||||
break;
|
||||
} catch (err) {
|
||||
if (err instanceof Deno.errors.AddrInUse) {
|
||||
// Throw first EADDRINUSE error
|
||||
// if no port is free
|
||||
if (!firstError) {
|
||||
firstError = err;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (firstError) {
|
||||
throw firstError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function bootServer(handler: ServeHandler, opts: StartOptions) {
|
||||
if (opts.experimentalDenoServe === true) {
|
||||
// @ts-ignore as `Deno.serve` is still unstable.
|
||||
await Deno.serve({ ...opts, handler });
|
||||
} else {
|
||||
await serve(handler, opts);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user