2

In my clojure Luminus/Compojure app I have this in routes.clj:

   (def page-size 12)
   (def images-path "/public/images/.....")

I need to move them to a config of some sort. Where is the best place? I'd like something simple and not to use any additional library on top on the ones that I'm already using which come with Luminus.

Dett
  • 85
  • 1
  • 1
  • 5

2 Answers2

4

Luminus uses it's config library for configuration. You can put your configuration variables into appropriate config.edn files (per environment). Config values are available as a map stored in config.core/env. You can see an example in your <app>.core namespace:

(defn http-port [port]
  ;;default production port is set in
  ;;env/prod/resources/config.edn
  (parse-port (or port (env :port))))
Piotrek Bzdyl
  • 12,965
  • 1
  • 31
  • 49
  • can I have a setting shared between all environments? for example, I want `page-size` always to be equal 12 in all of them, but I don't want to repeat myself. – Dett Jun 07 '16 at 07:07
  • I would write my own code which would read a file with common config (e.g. `common-config.edn`) and merge it with `config.core/env` one and use it instead. – Piotrek Bzdyl Jun 07 '16 at 10:00
  • I was equally surprised to find out that cprop doesn't read any common config file by default. Using Luminus, add this to your config.clj: (defstate env :start (load-config :merge [(source/from-resource "common-config.edn") – Kalle Nov 14 '16 at 19:20
2

Ask yourself this question:

Would I ever want multiple deployments of my application where this setting differs?

If the answer to that question is "yes" then the configuration should be dictated by the environment running you application either by an edn file, Environ or other means.

If not... then you are talking about something I would categorize as an application constant, which helps avoiding magic numbers. In some situations it can improve readability by placing these in specific namespaces like so.

Constants namespace:

(ns my.app.constants)

(def page-size 12)
(def images-path "/public/images/.....")

Application:

(ns my.app.core
  (:require [my.app.constants :as const)

;; now access the application constant like this
;; const/page-size

(defn image-url [image-name]
  (str const/images-path "/" image-name))
Jacob
  • 974
  • 1
  • 12
  • 21