Skip to content

Instantly share code, notes, and snippets.

@jdf-id-au
Created July 22, 2018 09:54
Show Gist options
  • Save jdf-id-au/419c83f119bad7b4981a168ab6d37b78 to your computer and use it in GitHub Desktop.
Save jdf-id-au/419c83f119bad7b4981a168ab6d37b78 to your computer and use it in GitHub Desktop.
Use mount to control an application's "mode", and an atom to control feature knobs for each mode.
(ns server.config
(:require [mount.core :refer [defstate]))
(defstate mode :start :prod)
(def switchboard (atom {:dev {:feature-one :setting-a}
:prod {:feature-one :setting-b}}))
(defn set-feature! [name value]
(assert (not (instance? mount.core.DerefableState mode)))
(swap! switchboard assoc-in [mode name] value))
(defn feature [name]
(assert (not (instance? mount.core.DerefableState mode)))
(get-in @switchboard [mode name]))
(defn feature-dispatch [name]
(fn [& _] (feature name)))
(ns server.core
(:require [server.config :refer [feature-dispatch]]))
(defmulti feature-one (feature-dispatch :feature-one))
(defmethod feature-one :setting-a [args] ...)
(defmethod feature-one :setting-b [args] ...)
(defmethod feature-one :default [args] ...)
(ns user
(:require [mount.core :as mount :refer [swap]]
[server.config]))
(defn start! []
(-> (swap {#'server.config/mode :dev})
mount/start))
(defn stop! []
(mount/stop))
(defn mode [m]
(mount/start-with {#'server.config/mode m}))
(def set-feature! server.config/set-feature!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment