Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
move from environment variables based configuration to config files
Browse files Browse the repository at this point in the history
all configuration is now specified in src/main/resources/config.edn
  • Loading branch information
Daniel Marjenburgh committed Feb 21, 2015
1 parent 41fa5df commit 8c49929
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 79 deletions.
15 changes: 0 additions & 15 deletions profiles.sample.clj

This file was deleted.

10 changes: 3 additions & 7 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ across several browsers, resolutions and platforms."
:license {:name "Apache Licence 2.0"
:url "http://www.apache.org/licenses/LICENSE-2.0.txt"}
:dependencies [[org.clojure/clojure "1.6.0"]
[environ "1.0.0"] ;configuration
[ring/ring-core "1.3.2"] ;webserver middleware
[ring/ring-jetty-adapter "1.3.2"] ;webserver container
[compojure "1.3.1"] ;routes
Expand Down Expand Up @@ -43,12 +42,10 @@ across several browsers, resolutions and platforms."
"bower-install" ["shell" "bower" "install"]
"grunt-build" ["shell" "grunt" "build"]}

:profiles {:dev-common {:dependencies [[midje "1.6.3"]
:profiles {:dev {:dependencies [[midje "1.6.3"]
[clj-http "1.0.1"]]
:plugins [[lein-environ "1.0.0"]
[lein-midje "3.1.3"]]
:resource-paths ["src/integration/resources"]}
:dev [:dev-common :dev-overrides]
:plugins [[lein-midje "3.1.3"]]
:resource-paths ["src/test/resources" "src/integration/resources"]}
:uberjar {:aot :all
:prep-tasks ^:replace [["npm-install"] ["bower-install"] ["grunt-build"]
["resource"] ["javac"] ["compile"]]
Expand All @@ -60,4 +57,3 @@ across several browsers, resolutions and platforms."
:jar-name "visualreview-%s.jar"
:uberjar-name "visualreview-%s-standalone.jar"
:javac-options ["-target" "1.7" "-source" "1.7"])

42 changes: 23 additions & 19 deletions src/main/clojure/com/xebia/visualreview/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(ns com.xebia.visualreview.config
(:require [environ.core :refer [env]]
[com.xebia.visualreview.validation :as v]))
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[com.xebia.visualreview.validation :as v])
(:import [java.io FileNotFoundException]))

(def ^:private config-env-prefix "visualreview-")

(def config-schema
"Expected keys and related validators for configuration data"
{:port [Long []]
:db-uri [String []]
:db-user [String []]
:db-password [String [::v/optional]]
(def ^{:private true :doc "Expected keys and related validators for configuration data"}
config-schema
{:server-port [Long []]
:db-uri [String []]
:db-user [String []]
:db-password [String [::v/optional]]
:screenshots-dir [String [::v/optional]]})

(defn- read-setting [entry-keyword]
"Returns a configuration entry, set by ether a command-line parameter or environment variable.
While all configuration entries are set using the 'visualreview-' prefix, users of this API should omit this prefix."
(when (contains? config-schema entry-keyword)
((keyword (str config-env-prefix (name entry-keyword))) env)))
(def default-config {:server-port "7000"
:screenshots-dir ".visualreview"})

(defonce env {})

(defn- settings []
(into {} (for [k (keys config-schema)] [k (read-setting k)])))
(def ^:private config-file "config.edn")
(defn init!
"Reads the config.edn resource file on the classpath (or the given arg). Parses it and sets the env var"
([] (init! config-file))
([cfg]
(if-let [resource (io/resource cfg)]
(let [conf (merge default-config (-> resource slurp edn/read-string))]
(alter-var-root #'env (fn [_] (v/validate config-schema conf))))
(throw (FileNotFoundException. (format "The configuration file %s could not be found" cfg))))))

(defn parsed-settings []
(v/validate config-schema (settings)))
6 changes: 3 additions & 3 deletions src/main/clojure/com/xebia/visualreview/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@

(defn- config-settings []
(try+
(config/parsed-settings)
(config/init!)
(catch [:type :com.xebia.visualreview.validation/invalid] exception
(timbre/log :fatal (str "Server configuration error: " (exception :message))))))

(defn -main [& _]
(when-let [{:keys [port db-uri db-user db-password screenshots-dir]} (config-settings)]
(when-let [{:keys [server-port db-uri db-user db-password screenshots-dir]} (config-settings)]
(try
(com.xebia.visualreview.io/init-screenshots-dir! screenshots-dir)
(com.xebia.visualreview.persistence.database/init! db-uri db-user db-password)
(starter/start-server port)
(starter/start-server server-port)
:ok
(catch Exception e
(timbre/log :fatal (str "Error initializing: " (.getMessage e)))
Expand Down
6 changes: 3 additions & 3 deletions src/main/clojure/com/xebia/visualreview/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
(:import [java.io File FileNotFoundException]
[java.nio.file NotDirectoryException Files AccessDeniedException LinkOption]
[java.nio.file.attribute FileAttribute]
(javax.imageio ImageIO)
(java.awt.image BufferedImage))
[javax.imageio ImageIO]
[java.awt.image BufferedImage])
(:require [clojure.java.io :as io]))

(defonce screenshots-dir "screenshots")
(def screenshots-dir "screenshots")

(defn init-screenshots-dir! [dir]
(let [dir (or dir "screenshots")
Expand Down
3 changes: 2 additions & 1 deletion src/main/clojure/com/xebia/visualreview/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[compojure.route :as route]
[com.xebia.visualreview.middleware :as m]
[com.xebia.visualreview.resource :as resource]
[com.xebia.visualreview.io :as io]
[com.xebia.visualreview.config :as config]))

(def ^:private resources-root "public")
Expand All @@ -45,7 +46,7 @@
(GET "/" [] (resource-response "index.html" {:root resources-root}))
(context "/api" req api-routes)
(context "/screenshots" []
(route/files "/" {:root (:screenshots-dir (config/parsed-settings))})
(route/files "/" {:root io/screenshots-dir})
(route/not-found nil))
(route/resources "/")
(route/not-found "Page not found")
Expand Down
2 changes: 1 addition & 1 deletion src/main/clojure/com/xebia/visualreview/validation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@
(ex/try+
{:valid? true :data (validate schema data)}
(catch [:type ::invalid] err
{:valid? false :error err})))
{:valid? false :error err})))
13 changes: 13 additions & 0 deletions src/main/resources/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
;; Server
:server-port "7000"

;; Database
:db-uri "file:./.visualreview/visualreview.db"
:db-user ""
:db-password ""

;; File system
:screenshots-dir ".visualreview/screenshots"

}
33 changes: 9 additions & 24 deletions src/test/clojure/com/xebia/visualreview/config_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,13 @@

(ns com.xebia.visualreview.config-test
(:require [midje.sweet :refer :all]
[com.xebia.visualreview.config :refer :all]
[environ.core :as environ]))
[com.xebia.visualreview.config :refer :all]))

(def stub-env {:visualreview-port 1234
:visualreview-db-uri "//localhost:5432/testdb"
:visualreview-db-user "testuser"
:visualreview-db-password "test123"
:some-other-env-var "some-value"})

(defmacro with-env [env & body]
`(with-redefs [environ/env ~env]
~@body))

(facts "about parsed-settings"
(fact "it validates and parses environment variables prefixed with 'visual-review-'. it retrieves only the fields defined in the config-schema"
(with-env stub-env
(parsed-settings)) => {:port 1234
:db-uri "//localhost:5432/testdb"
:db-user "testuser"
:db-password "test123"})
(fact "the port should be a number or parseable to a number"
(with-env (update-in stub-env [:visualreview-port] str)
(:port (parsed-settings)) => 1234)
(with-env (assoc stub-env :visualreview-port "Not a number")
(parsed-settings) => (throws Exception #"not a number"))))
(facts "About init!"
(fact "validates and parses configuration variables"
(init! "test_config.edn") => {:server-port 7001
:db-uri "dummy:123//db"
:db-user "test-user"
:db-password "test-password"
:screenshots-dir ".visualreview/screenshots-test"})
)
15 changes: 15 additions & 0 deletions src/test/resources/test_config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
;; Server
:server-port "7001"

;; Database
:db-uri "dummy:123//db"
:db-user "test-user"
:db-password "test-password"

;; File system
:screenshots-dir ".visualreview/screenshots-test"

;; Fake testing fields
:nonexistent-configuration-key "Some value"
}
6 changes: 0 additions & 6 deletions start.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#!/bin/bash

export VISUALREVIEW_PORT=7000
export VISUALREVIEW_DB_URI="file:./.visualreview/vr-h2.db"
export VISUALREVIEW_DB_USER=""
export VISUALREVIEW_DB_PASSWORD=""
export VISUALREVIEW_SCREENSHOTS_DIR="./.visualreview/screenshots"

java -jar target/visualreview-0.0.1-SNAPSHOT-standalone.jar

0 comments on commit 8c49929

Please sign in to comment.