Add more adaptations to make app run in a prefixed path.

This commit is contained in:
Andrey Antukh 2021-04-19 18:35:36 +02:00 committed by Andrés Moya
parent 2828ccda7f
commit 55ea84a056
14 changed files with 115 additions and 98 deletions

View file

@ -8,13 +8,13 @@
"A http client with rx streams interface."
(:require
[app.common.data :as d]
[app.common.uri :as u]
[app.config :as cfg]
[app.util.globals :as globals]
[app.util.object :as obj]
[app.util.transit :as t]
[beicon.core :as rx]
[cuerdas.core :as str]
[lambdaisland.uri :as u]
[promesa.core :as p]))
(defprotocol IBodyData

View file

@ -1,3 +1,9 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) UXBOX Labs SL
(ns app.util.logging)
(defn- log-expr [form level keyvals]

View file

@ -9,37 +9,17 @@
(:require
[app.common.data :as d]
[app.config :as cfg]
[app.common.uri :as u]
[app.util.browser-history :as bhistory]
[app.util.timers :as ts]
[beicon.core :as rx]
[cuerdas.core :as str]
[goog.events :as e]
[potok.core :as ptk]
[reitit.core :as r])
(:import
goog.Uri
goog.Uri.QueryData))
[reitit.core :as r]))
;; --- Router API
(defn- parse-query-data
[^QueryData qdata]
(persistent!
(reduce (fn [acc key]
(let [values (.getValues qdata key)
rkey (str/keyword key)]
(cond
(> (alength values) 1)
(assoc! acc rkey (into [] values))
(= (alength values) 1)
(assoc! acc rkey (aget values 0))
:else
acc)))
(transient {})
(.getKeys qdata))))
(defn resolve
([router id] (resolve router id {} {}))
([router id params] (resolve router id params {}))
@ -47,12 +27,10 @@
(when-let [match (r/match-by-name router id params)]
(if (empty? qparams)
(r/match->path match)
(let [uri (.parse goog.Uri (r/match->path match))
qdt (.createFromMap QueryData (-> qparams
(d/without-nils)
(clj->js)))]
(.setQueryData ^js uri qdt)
(.toString ^js uri))))))
(let [query (u/map->query-string qparams)]
(-> (u/uri (r/match->path match))
(assoc :query query)
(str)))))))
(defn create
[routes]
@ -65,26 +43,18 @@
(update [_ state]
(assoc state :router (create routes)))))
(defn query-params
"Given goog.Uri, read query parameters into Clojure map."
[^goog.Uri uri]
(let [^js q (.getQueryData uri)]
(->> q
(.getKeys)
(map (juxt keyword #(.get q %)))
(into {}))))
(defn match
"Given routing tree and current path, return match with possibly
coerced parameters. Return nil if no match found."
[router path]
(let [uri (.parse ^js Uri path)]
(when-let [match (r/match-by-path router (.getPath ^js uri))]
(let [qparams (parse-query-data (.getQueryData ^js uri))
params {:path (:path-params match) :query qparams}]
(assoc match
:params params
:query-params qparams)))))
(let [uri (u/uri path)]
(when-let [match (r/match-by-path router (:path uri))]
(let [qparams (u/query-string->map (:query uri))
params {:path (:path-params match)
:query qparams}]
(-> match
(assoc :params params)
(assoc :query-params qparams))))))
;; --- Navigate (Event)
@ -119,8 +89,9 @@
(effect [_ state stream]
(let [router (:router state)
path (resolve router id params qparams)
uri (str cfg/public-uri "/#" path)]
(js/window.open uri "_blank"))))
uri (-> (u/uri cfg/public-uri)
(assoc :fragment path))]
(js/window.open (str uri) "_blank"))))
(defn nav-new-window
([id] (nav-new-window id nil nil))

View file

@ -7,13 +7,13 @@
(ns app.util.websockets
"A interface to webworkers exposed functionality."
(:require
[app.common.uri :as u]
[app.config :as cfg]
[app.util.transit :as t]
[beicon.core :as rx]
[goog.events :as ev]
[potok.core :as ptk])
(:import
goog.Uri
goog.net.WebSocket
goog.net.WebSocket.EventType))
@ -22,19 +22,6 @@
(-send [_ message] "send a message")
(-close [_] "close websocket"))
(defn uri
([path] (uri path {}))
([path params]
(let [uri (.parse ^js Uri cfg/public-uri)]
(.setPath ^js uri path)
(if (= (.getScheme ^js uri) "http")
(.setScheme ^js uri "ws")
(.setScheme ^js uri "wss"))
(run! (fn [[k v]]
(.setParameterValue ^js uri (name k) (str v)))
params)
(.toString uri))))
(defn open
[uri]
(let [sb (rx/subject)
@ -45,7 +32,7 @@
#(rx/push! sb {:type :error :payload %}))
lk3 (ev/listen ws EventType.OPENED
#(rx/push! sb {:type :opened :payload %}))]
(.open ws uri)
(.open ws (str uri))
(reify
cljs.core/IDeref
(-deref [_] ws)