From f8023493f044113a3eb87c1424e681dbbf60b292 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 4 Apr 2022 16:31:04 -0400 Subject: [PATCH] added hostname option to serve command (#519) * added hostname option to serve command * update log to include hostname and port --- src/commands/serve.command.ts | 5 +++-- src/program.ts | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/commands/serve.command.ts b/src/commands/serve.command.ts index 5f5f72f..bbea6ca 100644 --- a/src/commands/serve.command.ts +++ b/src/commands/serve.command.ts @@ -147,6 +147,7 @@ export class ServeCommand { async run(options: program.OptionValues) { const port = options.port || 8087; + const hostname = options.hostname || "localhost"; const server = new koa(); const router = new koaRouter(); process.env.BW_SERVE = "true"; @@ -355,8 +356,8 @@ export class ServeCommand { server .use(router.routes()) .use(router.allowedMethods()) - .listen(port, () => { - this.main.logService.info("Listening on port " + port); + .listen(port, hostname === "all" ? null : hostname, () => { + this.main.logService.info("Listening on " + hostname + ":" + port); }); } diff --git a/src/program.ts b/src/program.ts index 2c3401d..2644bac 100644 --- a/src/program.ts +++ b/src/program.ts @@ -470,12 +470,20 @@ export class Program extends BaseProgram { program .command("serve") .description("Start a RESTful API webserver.") - .option("--port ", "The port to run your API webserver on. Default port is 8087.") + .option("--hostname ", "The hostname to bind your API webserver to.") + .option("--port ", "The port to run your API webserver on.") .on("--help", () => { - writeLn("\n Examples:"); + writeLn("\n Notes:"); + writeLn(""); + writeLn(" Default hostname is `localhost`."); + writeLn(" Use hostname `all` for no hostname binding."); + writeLn(" Default port is `8087`."); + writeLn(""); + writeLn(" Examples:"); writeLn(""); writeLn(" bw serve"); writeLn(" bw serve --port 8080"); + writeLn(" bw serve --hostname bwapi.mydomain.com --port 80"); writeLn("", true); }) .action(async (cmd) => {