Skip to content

Commit

Permalink
Add options for specifying port and host of launched UI (#19)
Browse files Browse the repository at this point in the history
- Add host and port options for JVM launcher
- Add host and port options for Node launcher
- Update readme with new options
- Introduce a start api function, to allow control over the http server itself
  • Loading branch information
justone authored Oct 26, 2020
1 parent 0182a43 commit 67060fd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ decide to view it as a coll, and with that viewer selected, `deref` would
return a list of pairs. Not many viewers implement this functionality
currently, but expect more to do so in the future.

### Themes
### Options

#### Themes

There are currently three built-in themes:

Expand All @@ -113,6 +115,17 @@ Which can be passed as an option to `p/open`:
{:portal.colors/theme :portal.colors/nord})
```

#### Launcher

By default, when `p/open` is called, an HTTP server is started on a randomly
chosen port. To control this server's port and host, call the `p/start`
function with the following options:

| Option | Description | If not specified |
|-------------------------|----------------------------|----------------------|
| `:portal.launcher/port` | Port used to access UI | random port selected |
| `:portal.launcher/host` | Hostname used to access UI | "localhost" |

## Datafy and Nav

There is one exception to the behavior described above for the UI,
Expand Down
6 changes: 6 additions & 0 deletions src/portal/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
(add-tap #'rt/update-value)
nil)

(defn start
"Start the HTTP server with non-default options. Only use if you need
control over the HTTP server."
[options]
(l/start options))

(defn open
"Open a new inspector window."
([] (open nil))
Expand Down
25 changes: 16 additions & 9 deletions src/portal/runtime/jvm/launcher.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,24 @@
"--no-first-run"
(str "--app=" url)])

(defn start [options]
(when (nil? @server)
(let [{:portal.launcher/keys [port host] :or {port 0 host "localhost"}} options
http-server (http/run-server #'server/handler
{:port port
:max-ws (* 1024 1024 1024)
:legacy-return-value? false})]
(reset!
server
{:http-server http-server
:port (http/server-port http-server)
:host host}))))

(defn open [options]
(swap! rt/state merge {:portal/open? true} options)
(when (nil? @server)
(reset!
server
(http/run-server #'server/handler
{:port 0
:max-ws (* 1024 1024 1024)
:legacy-return-value? false})))
(let [session-id (random-uuid)
url (str "http://localhost:" (http/server-port @server) "?" session-id)]
{:keys [host port]} (or @server (start nil))
url (str "http://" host ":" port "?" session-id)]
(if-let [bin (get-chrome-bin)]
(let [flags (chrome-flags url)]
(future (apply sh bin flags)))
Expand All @@ -54,5 +61,5 @@

(defn close []
(swap! rt/state assoc :portal/open? false)
(http/server-stop! @server)
(some-> server deref :http-server http/server-stop!)
(reset! server nil))
21 changes: 14 additions & 7 deletions src/portal/runtime/node/launcher.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,32 @@

(defonce ^:private server (atom nil))

(defn- start [handler]
(defn- create-server [handler port host]
(js/Promise.
(fn [resolve _reject]
(let [server (http/createServer #(handler %1 %2))]
(.listen server 0
(.listen server port
#(let [port (.-port (.address server))
result (with-meta {:server server} {:local-port port})]
result {:http-server server
:port port
:host host}]
(resolve result)))))))

(defn start [options]
(when (nil? @server)
(a/let [{:portal.launcher/keys [port host] :or {port 0 host "localhost"}} options
instance (create-server #'server/handler port host)]
(reset! server instance))))

(defn- stop [handle]
(.close (:server handle)))
(some-> handle :http-server .close))

(defn open [options]
(let [session-id (random-uuid)]
(swap! rt/state merge {:portal/open? true} options)
(a/let [chrome-bin (get-chrome-bin)
instance (or @server (start #'server/handler))
url (str "http://localhost:" (-> instance meta :local-port) "?" session-id)]
(reset! server instance)
{:keys [host port]} (or @server (start nil))
url (str "http://" host ":" port "?" session-id)]
(if-not (some? chrome-bin)
(println "Goto" url "to view portal ui.")
(sh chrome-bin
Expand Down

0 comments on commit 67060fd

Please sign in to comment.