Skip to content

Commit

Permalink
Making most keys optional
Browse files Browse the repository at this point in the history
  • Loading branch information
a-luz committed Nov 3, 2023
1 parent 4aab4f9 commit 495822c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 113 deletions.
33 changes: 16 additions & 17 deletions config.namespaced-example.edn
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{;; server (server)
:triangulum.server/http-port 8080
:triangulum.server/https-port 8443
:triangulum.server/nrepl false
:triangulum.server/nrepl-port 5555
:triangulum.server/nrepl-bind "127.0.0.1"
:triangulum.server/cider-nrepl true
:triangulum.server/mode "dev"
:triangulum.server/log-dir "logs"
:triangulum.server/handler product-ns.routing/handler
:triangulum.server/keystore-file "keystore.pkcs12"
:triangulum.server/keystore-type "pkcs12"
:triangulum.server/keystore-password "foobar"
:triangulum.server/http-port 8080
:triangulum.server/https-port 8443
:triangulum.server/nrepl false
:triangulum.server/nrepl-port 5555
:triangulum.server/nrepl-bind "127.0.0.1"
:triangulum.server/cider-nrepl true
:triangulum.server/mode "dev"
:triangulum.server/log-dir "logs"
:triangulum.server/handler product-ns.routing/handler
:triangulum.server/keystore-file "keystore.pkcs12"
:triangulum.server/keystore-type "pkcs12"
:triangulum.server/keystore-password "foobar"

;; handler (server)
:triangulum.handler/not-found-handler product-ns.handlers/not-found-handler
:triangulum.handler/redirect-handler product-ns.handlers/redirect-handler
:triangulum.handler/route-authenticator product-ns.handlers/route-authenticator
:triangulum.handler/routing-tables [backend-libary-ns.routing/routes product-ns.routing/routes]
:triangulum.handler/session-key "changeme12345678" ; must be 16 characters
:triangulum.handler/not-found-handler product-ns.handlers/not-found-handler
:triangulum.handler/redirect-handler product-ns.handlers/redirect-handler
:triangulum.handler/route-authenticator product-ns.handlers/route-authenticator
:triangulum.handler/routing-tables [backend-libary-ns.routing/routes product-ns.routing/routes]
:triangulum.handler/bad-tokens #{".php"}
:triangulum.handler/private-request-keys #{:base64Image :plotFileBase64 :sampleFileBase64}
:triangulum.handler/private-response-keys #{}
Expand Down
1 change: 0 additions & 1 deletion config.nested-example.edn
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
:redirect-handler product-ns.handlers/redirect-handler
:route-authenticator product-ns.handlers/route-authenticator
:routing-tables [common-libary-ns.routing/routes product-ns.routing/routes]
:session-key "changeme12345678" ; must be 16 characters
:bad-tokens #{".php"}
:private-request-keys #{:base64Image :plotFileBase64 :sampleFileBase64}
:private-response-keys #{}
Expand Down
137 changes: 81 additions & 56 deletions src/triangulum/config_namespaced_spec.clj
Original file line number Diff line number Diff line change
@@ -1,61 +1,86 @@
(ns triangulum.config-namespaced-spec
(:require [clojure.spec.alpha :as s]))

;; Helper functions

(defn- key-namespaces
[m]
(set (map namespace (keys m))))

(defn- no-keys-of-ns?
[m ns]
(not (contains? (key-namespaces m) ns)))

;; New Format (namespaced)

(s/def ::server (s/keys :req [:triangulum.server/http-port
:triangulum.server/mode
:triangulum.server/log-dir
:triangulum.server/handler
:triangulum.handler/session-key
:triangulum.response/response-type]
:opt [:triangulum.server/https-port
:triangulum.server/nrepl
:triangulum.server/nrepl-port
:triangulum.server/nrepl-bind
:triangulum.server/cider-nrepl
:triangulum.server/keystore-file
:triangulum.server/keystore-type
:triangulum.server/keystore-password
:triangulum.handler/not-found-handler
:triangulum.handler/redirect-handler
:triangulum.handler/route-authenticator
:triangulum.handler/routing-tables
:triangulum.handler/private-request-keys
:triangulum.handler/private-response-keys
:triangulum.handler/bad-tokens
:triangulum.worker/workers]))

(s/def ::app (s/keys :opt [:triangulum.views/title
:triangulum.views/description
:triangulum.views/keywords
:triangulum.views/extra-head-tags
:triangulum.views/gtag-id
:triangulum.views/static-css-files
:triangulum.views/static-js-files
:triangulum.views/get-user-lang
:triangulum.views/js-init
:triangulum.views/cljs-init
:triangulum.views/client-keys
:triangulum.git/tags-url]))

(s/def ::database (s/keys :req [:triangulum.database/dbname
:triangulum.database/user
:triangulum.database/password]
:opt [:triangulum.database/host
:triangulum.database/port
:triangulum.build-db/admin-pass
:triangulum.build-db/dev-data
:triangulum.build-db/file
:triangulum.build-db/verbose]))

(s/def ::mail (s/keys :req [:triangulum.email/host
:triangulum.email/user
:triangulum.email/pass]
:opt [:triangulum.email/port]))

(s/def ::https (s/keys :req [:triangulum.https/email
:triangulum.https/domain]
:opt [:triangulum.https/path
:triangulum.https/cert-only
:triangulum.https/webroot]))
(s/def ::server (s/or
:no-server-keys
#(no-keys-of-ns? % "triangulum.server")
:server-keys
(s/keys :req [:triangulum.server/http-port
:triangulum.server/handler]
:opt [:triangulum.server/https-port
:triangulum.server/nrepl
:triangulum.server/nrepl-port
:triangulum.server/nrepl-bind
:triangulum.server/cider-nrepl
:triangulum.server/keystore-file
:triangulum.server/keystore-type
:triangulum.server/keystore-password
:triangulum.server/mode
:triangulum.server/log-dir
:triangulum.handler/not-found-handler
:triangulum.handler/redirect-handler
:triangulum.handler/route-authenticator
:triangulum.handler/routing-tables
:triangulum.handler/private-request-keys
:triangulum.handler/private-response-keys
:triangulum.handler/bad-tokens
:triangulum.worker/workers
:triangulum.response/response-type])))

(s/def ::app (s/keys :opt [:triangulum.views/title
:triangulum.views/description
:triangulum.views/keywords
:triangulum.views/extra-head-tags
:triangulum.views/gtag-id
:triangulum.views/static-css-files
:triangulum.views/static-js-files
:triangulum.views/get-user-lang
:triangulum.views/js-init
:triangulum.views/cljs-init
:triangulum.views/client-keys
:triangulum.git/tags-url]))

(s/def ::database (s/or
:no-database-keys
#(no-keys-of-ns? % "triangulum.database")
:database-keys
(s/keys :req [:triangulum.database/dbname
:triangulum.database/user
:triangulum.database/password]
:opt [:triangulum.database/host
:triangulum.database/port
:triangulum.build-db/admin-pass
:triangulum.build-db/dev-data
:triangulum.build-db/file
:triangulum.build-db/verbose])))

(s/def ::mail (s/or
:no-email-keys
#(no-keys-of-ns? % "triangulum.email")
:email-keys
(s/keys :req [:triangulum.email/host
:triangulum.email/user
:triangulum.email/pass]
:opt [:triangulum.email/port])))

(s/def ::https (s/or
:no-https-keys
#(no-keys-of-ns? % "triangulum.https")
:https-keys
(s/keys :req [:triangulum.https/email
:triangulum.https/domain]
:opt [:triangulum.https/path
:triangulum.https/cert-only
:triangulum.https/webroot])))
11 changes: 5 additions & 6 deletions src/triangulum/config_nested_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
;; Old Format (un-namespaced)

(s/def ::server (s/keys :req-un [:triangulum.server/http-port
:triangulum.server/mode
:triangulum.server/log-dir
:triangulum.server/handler
:triangulum.handler/session-key
:triangulum.response/response-type]
:triangulum.server/handler]
:opt-un [:triangulum.server/https-port
:triangulum.server/nrepl
:triangulum.server/nrepl-port
Expand All @@ -17,14 +13,17 @@
:triangulum.server/keystore-file
:triangulum.server/keystore-type
:triangulum.server/keystore-password
:triangulum.server/mode
:triangulum.server/log-dir
:triangulum.handler/not-found-handler
:triangulum.handler/redirect-handler
:triangulum.handler/route-authenticator
:triangulum.handler/routing-tables
:triangulum.handler/bad-tokens
:triangulum.handler/private-request-keys
:triangulum.handler/private-response-keys
:triangulum.worker/workers]))
:triangulum.worker/workers
:triangulum.response/response-type]))

(s/def ::app (s/keys :opt-un [:triangulum.views/title
:triangulum.views/description
Expand Down
35 changes: 13 additions & 22 deletions src/triangulum/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,10 @@
(s/def ::not-found-handler ::config/namespaced-symbol)
(s/def ::route-authenticator ::config/namespaced-symbol)
(s/def ::routing-tables (s/coll-of ::config/namespaced-symbol))
(s/def ::session-key (s/and ::config/string #(= 16 (count %))))
(s/def ::bad-tokens (s/coll-of ::config/string :kind set? :min-count 0))
(s/def ::private-request-keys (s/coll-of keyword :kind set?))
(s/def ::private-response-keys (s/coll-of keyword :kind set?))

;; state

;; FIXME: Make this into a reloadable component
(defonce ^:private session-cookie-store (atom nil))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Routing Handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -163,22 +156,20 @@
[^String s]
(.getBytes s))

(defn random-string
"Returns a random alphanumeric string of length n."
[n]
(let [char-seq (map char (concat (range 48 58) ; 0-9
(range 65 91) ; A-Z
(range 97 123) ; a-z
))]
(apply str (take n (shuffle char-seq)))))

(defn get-cookie-store
"Computes a new `ring.middleware.session.cookie/cookie-store` object
on the first call and caches it for use in all future calls."
"Computes a new `ring.middleware.session.cookie/cookie-store` object."
[]
(or @session-cookie-store
(reset! session-cookie-store
(cookie-store {:key (-> (get-config :server :session-key)
(string-to-bytes))}))))

(defn wrap-wrap-session
"Wrapper around `ring.middleware.session/wrap-session` that defers
looking up the cookie store until a request has been made."
[handler]
(fn [request]
(let [wrapped-handler (wrap-session handler {:store (get-cookie-store)})]
(wrapped-handler request))))
(cookie-store {:key (-> (random-string 16)
(string-to-bytes))}))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Handler Stack
Expand All @@ -204,7 +195,7 @@
wrap-nested-params
wrap-multipart-params
wrap-params
wrap-wrap-session ; used to prevent running `get-config` at file load time
(wrap-session {:store (get-cookie-store)})
wrap-absolute-redirects
(wrap-resource "public")
wrap-content-type
Expand Down
14 changes: 8 additions & 6 deletions src/triangulum/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
(s/def ::cider-nrepl boolean?)
(s/def ::mode (s/and ::config/string #{"dev" "prod"}))
(s/def ::log-dir ::config/string)
(s/def ::handler ::config/namespaced-symbol)
(s/def ::handler ::config/namespaced-symbol)
(s/def ::keystore-file ::config/string)
(s/def ::keystore-type ::config/string)
(s/def ::keystore-password ::config/string)
Expand All @@ -45,11 +45,13 @@
"See README.org -> Web Framework -> triangulum.server for details."
[{:keys [http-port https-port nrepl cider-nrepl nrepl-bind nrepl-port mode log-dir
handler workers keystore-file keystore-type keystore-password]
:or {nrepl-bind "127.0.0.1"
nrepl-port 5555
keystore-file "./.key/keystore.pkcs12"
keystore-type "pkcs12"
keystore-password "foobar"}}]
:or {nrepl-bind "127.0.0.1"
nrepl-port 5555
keystore-file "./.key/keystore.pkcs12"
keystore-type "pkcs12"
keystore-password "foobar"
log-dir ""
mode "prod"}}]
(let [has-key? (and keystore-file (.exists (io/file keystore-file)))
ssl? (and has-key? https-port)
reload? (= mode "dev")
Expand Down
10 changes: 5 additions & 5 deletions src/triangulum/views.clj
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
(include-js (find-cljs-app-js))

;; JS app/prod
(= "prod" (get-config :server :mode))
(not= "dev" (get-config :server :mode))
(map (fn [f] [:script {:type "module" :src f}])
(butlast bundle-js-files))

Expand Down Expand Up @@ -157,13 +157,13 @@
(format "window.onload = function () { %s(%s, %s); };" (-> cljs-init name kebab->snake) js-params js-session)]
;; JS app
[:script {:type "module"}
(if (= "prod" (get-config :server :mode))
(if (= "dev" (get-config :server :mode))
(format "import { pageInit } from \"%s\"; window.onload = function () { pageInit(%s, %s); };"
entry-file
(str "http://localhost:5173" (get-config :app :js-init))
js-params
js-session)
(format "import { pageInit } from \"%s\"; window.onload = function () { pageInit(%s, %s); };"
(str "http://localhost:5173" (get-config :app :js-init))
entry-file
js-params
js-session))])))

Expand Down Expand Up @@ -229,7 +229,7 @@
"Returns the page's html."
[uri]
(fn [request]
(let [response-params (when (= "prod" (get-config :server :mode))
(let [response-params (when (not= "dev" (get-config :server :mode))
(get-response-params uri request))]
{:status 200
:headers {"Content-Type" "text/html"}
Expand Down

0 comments on commit 495822c

Please sign in to comment.