Accessing vars from another clojure namespace?

Posted by erikcw on Stack Overflow See other posts from Stack Overflow or by erikcw
Published on 2011-01-05T23:50:38Z Indexed on 2011/01/05 23:53 UTC
Read the original article Hit count: 175

Filed under:
|

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))))
...

© Stack Overflow or respective owner

Related posts about clojure

Related posts about settings