added hostname option to serve command (#519)

* added hostname option to serve command

* update log to include hostname  and port
This commit is contained in:
Kyle Spearrin 2022-04-04 16:31:04 -04:00 committed by GitHub
parent 1e9a557494
commit f8e0e8be06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -147,6 +147,7 @@ export class ServeCommand {
async run(options: program.OptionValues) { async run(options: program.OptionValues) {
const port = options.port || 8087; const port = options.port || 8087;
const hostname = options.hostname || "localhost";
const server = new koa(); const server = new koa();
const router = new koaRouter(); const router = new koaRouter();
process.env.BW_SERVE = "true"; process.env.BW_SERVE = "true";
@ -355,8 +356,8 @@ export class ServeCommand {
server server
.use(router.routes()) .use(router.routes())
.use(router.allowedMethods()) .use(router.allowedMethods())
.listen(port, () => { .listen(port, hostname === "all" ? null : hostname, () => {
this.main.logService.info("Listening on port " + port); this.main.logService.info("Listening on " + hostname + ":" + port);
}); });
} }

View File

@ -470,12 +470,20 @@ export class Program extends BaseProgram {
program program
.command("serve") .command("serve")
.description("Start a RESTful API webserver.") .description("Start a RESTful API webserver.")
.option("--port <port>", "The port to run your API webserver on. Default port is 8087.") .option("--hostname <hostname>", "The hostname to bind your API webserver to.")
.option("--port <port>", "The port to run your API webserver on.")
.on("--help", () => { .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("");
writeLn(" bw serve"); writeLn(" bw serve");
writeLn(" bw serve --port 8080"); writeLn(" bw serve --port 8080");
writeLn(" bw serve --hostname bwapi.mydomain.com --port 80");
writeLn("", true); writeLn("", true);
}) })
.action(async (cmd) => { .action(async (cmd) => {