Created
February 7, 2013 02:15
-
-
Save paulosuzart/4727858 to your computer and use it in GitHub Desktop.
This shows how to use custom immutant init functions to load arbitrary files according to the environment the app is running.
This uses a combination of: leiningen profiles + custom immutant init function + clojure's ability to evaluate scripts + some magical stuff with namespaces.
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
(defn handler | |
[r] ((build-routes) r)) | |
(web/start handler | |
:reload true) | |
(msg/start "/queue/delivery.status") | |
(msg/respond "/queue/delivery.status" handle-delivery-status) | |
(register-job #'import-job) | |
(register-job #'check-import) | |
;register-job is function not provided here | |
;since the intention is tho show the configuration solution |
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 tserver.init | |
(:use [clojure.tools.logging :only (info error debug)])) | |
(defn setup-config-ns [e] | |
(binding [*ns* *ns*] | |
(in-ns 'tserver.init) | |
(refer-clojure) | |
(use '[clojure.main :only (load-script)]) | |
(require '[immutant.messaging :as msg] | |
'[immutant.web :as web] | |
'[immutant.util :as util]))) | |
(defn load-config | |
"Attempts to evaluate the specified env file defined by `:env` | |
in the `proficel.clj`. `:env` is a immutant custom config. | |
`:env` MUST be a keyword: Ex.: :dev, :prod :office | |
Uses :dev by default. | |
Note: The abscence of the requrested file will prevent the server to start. | |
These siles MUST be located at `src/tserver/config/%s.clj" | |
[] | |
(binding [*ns* *ns*] | |
(in-ns 'tserver.init) | |
(let [ev (get-in (immutant.registry/get :config) [:env] :dev) | |
config-file (format "src/tserver/config/%s.clj" (name ev))] | |
(setup-config-ns ev) | |
(info "Using config file " config-file) | |
(load-file config-file) | |
(immutant.registry/put :env ev)))) | |
(defn init [] | |
;;mybe do some stuff before | |
(load-config)) ;;maybe do some stuff after |
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
(defproject tserver "0.1.0-SNAPSHOT" | |
:description "A foo project" | |
:url "http://foo.com/" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.4.0"] | |
[compojure "1.0.2"] | |
;... and many other dependecies | |
:jvm-opts ["-Xmx2g" "-server"] | |
:immutant {:init "tserver.init/init"} ;; our custo immutant init function | |
:profiles {:dev {:immutant {:context-path "/" | |
:nrepl-port 4242 | |
:lein-profiles [:dev] | |
:env :dev}}}) ;:dev will identify which config file to load |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment