Clojure(Script) environ compatible config that reloads from project.clj
when in dev mode.
configuron is a configuration library that has interface of environ, but provides additional features.
- easy to migrate from environ
- env variable is updated every time you update your
project.clj
clojurescript
support. you can access your config on frontend the same way you do it on backend.- server-side rendering ready design. let's you choose whether you want to get config using ajax or encode it in html page.
- filtering rules for client side config in order to limit information, that's available to the client.
- Add
:mode
keys for each of your profiles inproject.clj
{:profiles {:dev {:env {:mode :dev}}
:uberjar {:env {:mode :prod}}}}
- Access your config through
rocks.clj.configuron.core/env
instead ofenviron.core/env
.
You can use your config on client-side as well.
Simply add GET route /environ
to your project with handler rocks.clj.configuron.core/config-handler
:
(GET "/environ" [] #'config-handler)
By default your client-side config will be empty.
To add some data you should add paths to :client-config-keys
.
For example given your profiles look like this.
{:profiles {:dev {:env {:mode :dev
:debug-info {:tokens {:sentry ""}}
:client-config-keys [[:mode]
[:debug-info :tokens]]}}
:uberjar {:env {:mode :uberjar
:client-config-keys [[:mode]]}}}}
/environ
ring handler will return.
In dev:
{:mode :dev
:debug-info {:tokens {:sentry ""}}}
In prod:
{:mode :uberjar}
You can access your config on client side as usual through rocks.clj.configuron.core/env
or dynamically by executing http request to /environ
.
When accessing rocks.clj.configuron.core/env
on page load (for example cljs app entry point),
there is no guarantee that config has been received by that time.
There are two solutions for that.
- Wrap your code in go block
(go
(let [env (<! (rocks.clj.configuron.core/get-env))]
;; your web-app initialization goes here
))
or (preferably)
- Write your config into dom on server side.
This will also effectively avoid http request to
/environ
.
Example in html:
<body>
<div id="config" transit="your config goes here" />
</body>
Example in hiccup:
[:body
[:div#config {:transit (rocks.clj.configuron.core/get-client-config)}]]
Copyright © 2018 Eduard Knyshov
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.