Accessing vars from another clojure namespace?
- by erikcw
In my main namespace, I have a top level var named "settings" which is initialized as an empty {}.
My -main fn sets the contents of settings using def and conj based on some command line args (different database hosts for production/development, etc).
I'm trying to access the contents of this map from another namespace to pull out some of the settings. When I try to compile with lein into an uberjar, I get a traceback saying "No such var: lb/settings".
What am I missing? Is there a more idiomatic way to handle app wide settings such as these? Is it safe to use "def" inside of -main like I am, or should I be use an atom or ref to make this threadsafe?
Thanks!
(ns main
(:use ...)
(:gen-class))
(def settings {})
(defn -main [& args]
(with-command-line-args... ;set devel? based on args
(if (true? devel?)
(def settings (conj settings {:mongodb {:host "127.0.0.1"}
:memcached {:host "127.0.0.1"}}))
(def settings (conj settings {:mongodb {:host "PRODUCTION_IP"}
:memcached {:host "PRODUCTION_IP"}})))
;file2.clj
(ns some-other-namespace
(:require [main :as lb]
...)
;configure MongoDB
(congo/mongo!
:db "dbname" :host (:host (mongodb lb/settings))))
...