Marcus Junius Brutus
Marcus Junius Brutus

Reputation: 27286

Clojure :: store configuration data in external file

At the beginning of my Clojure program I do a:

(def db-coords
    {:classname "org.postgresql.Driver"
     :subprotocol "postgresql"
     :subname (str "//" host ":" port "/" dbname) ;; host, port and dbname are defd above
     :user      "foo"
     :password  "bar"})

I would like to store these values in an external file (.clj or otherwise) on the classpath and load them from there. Clearly one way to do it would be using java.util.Properties but I suspect there is an idiomatic way in Clojure.

Upvotes: 2

Views: 627

Answers (3)

brendanb
brendanb

Reputation: 149

I have created a library for handling env specific configuration regions via Clojure maps and JVM system properties:

https://github.com/bbbates/confijulate This was a pattern that I found myself repeating over and over for every project.

Upvotes: 0

narkisr
narkisr

Reputation: 311

clj-config is a nice library which encapsulates the spit, read-string api

https://github.com/Raynes/clj-config

Upvotes: 1

Scott
Scott

Reputation: 17257

Consider using spit and slurp with read-string.

Clojure has a built-in ability to serialize and deserialize its data structures.

To serialize to a file:

(spit "./coords.txt" db-coords)

To load the file back in as a string and deserialize:

(read-string (slurp "./coords.txt"))

Upvotes: 5

Related Questions