From 9baac2181a42a9857f830c2a5a123bd327b31152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jouni=20K=2E=20Sepp=C3=A4nen?= Date: Sun, 4 Dec 2022 07:24:06 +0200 Subject: [PATCH] Allow the user to specify hostname to listen on (#138) --- README.md | 5 +++-- cmd/server.ts | 6 ++++-- server/http_server.ts | 8 ++++++-- silverbullet.ts | 1 + website/Silver Bullet.md | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8608dae8..c2327c2f 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,9 @@ terminal: silverbullet ``` -By default, SB will bind to port `3000`, to use a different port use the -`--port` flag. By default SB doesn’t offer any sort of authentication, to add +By default, SB will bind to port `3000` on all interfaces. To specify a +different address or port to listen on, use the `--hostname` and `--port` +options. By default SB doesn’t offer any sort of authentication, to add basic password authentication, pass the `--password` flag. Once downloaded and booted, SB will print out a URL to open SB in your browser diff --git a/cmd/server.ts b/cmd/server.ts index a8c9af0d..c0164401 100644 --- a/cmd/server.ts +++ b/cmd/server.ts @@ -5,11 +5,12 @@ import { AssetBundle, AssetJson } from "../plugos/asset_bundle/bundle.ts"; export function serveCommand(options: any, folder: string) { const pagesPath = path.resolve(Deno.cwd(), folder); + const hostname = options.hostname || "0.0.0.0"; const port = options.port || 3000; console.log( - "Going to start Silver Bullet on port", - port, + "Going to start Silver Bullet on", + `${hostname}:${port}`, "serving pages from", pagesPath, "with db file", @@ -17,6 +18,7 @@ export function serveCommand(options: any, folder: string) { ); const httpServer = new HttpServer({ + hostname, port: port, pagesPath: pagesPath, dbPath: path.join(pagesPath, options.db), diff --git a/server/http_server.ts b/server/http_server.ts index 5b4e3a6b..b3cc40ff 100644 --- a/server/http_server.ts +++ b/server/http_server.ts @@ -7,6 +7,7 @@ import { SpaceSystem } from "./space_system.ts"; import { parseYamlSettings } from "../common/util.ts"; export type ServerOptions = { + hostname: string; port: number; pagesPath: string; dbPath: string; @@ -19,12 +20,14 @@ const staticLastModified = new Date().toUTCString(); export class HttpServer { app: Application; systemBoot: SpaceSystem; + private hostname: string; private port: number; password?: string; settings: { [key: string]: any } = {}; abortController?: AbortController; constructor(options: ServerOptions) { + this.hostname = options.hostname; this.port = options.port; this.app = new Application(); //{ serverConstructor: FlashServer }); this.password = options.password; @@ -125,13 +128,14 @@ export class HttpServer { }); this.abortController = new AbortController(); - this.app.listen({ port: this.port, signal: this.abortController.signal }) + this.app.listen({ hostname: this.hostname, port: this.port, signal: this.abortController.signal }) .catch((e: any) => { console.log("Server listen error:", e.message); Deno.exit(1); }); + const visibleHostname = this.hostname === "0.0.0.0" ? "localhost" : this.hostname; console.log( - `Silver Bullet is now running: http://localhost:${this.port}`, + `Silver Bullet is now running: http://${visibleHostname}:${this.port}`, ); } diff --git a/silverbullet.ts b/silverbullet.ts index 159cc3c3..d30ca65e 100755 --- a/silverbullet.ts +++ b/silverbullet.ts @@ -19,6 +19,7 @@ await new Command() .usage(" | (see below)") // Main command .arguments("") + .option("--hostname ", "Hostname or address to listen on") .option("-p, --port ", "Port to listen on") .option("--db ", "Filename for the database", { default: "data.db", diff --git a/website/Silver Bullet.md b/website/Silver Bullet.md index b6610727..419589eb 100644 --- a/website/Silver Bullet.md +++ b/website/Silver Bullet.md @@ -103,7 +103,7 @@ To run Silver Bullet, create a folder for your pages (it can be empty, or be an silverbullet ``` -By default, Silver Bullet will bind to port `3000`, to use a different port use the the `--port` flag. By default Silver Bullet is unauthenticated, to password-protect it, specify a password with the `--password` flag. +By default, Silver Bullet will bind to port `3000` on all interfaces. To specify a different address or port to listen on, use the the `--hostname` and `--port` flags. By default Silver Bullet is unauthenticated, to password-protect it, specify a password with the `--password` flag. Once downloaded and booted, Silver Bullet will print out a URL to open SB in your browser (by default this will be http://localhost:3000 ).