Created
July 22, 2018 09:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] ...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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