Skip to content

Commit

Permalink
remove environments
Browse files Browse the repository at this point in the history
  • Loading branch information
duck1123 committed Oct 3, 2016
1 parent 64fd535 commit 6ad9141
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 73 deletions.
50 changes: 2 additions & 48 deletions src/ciste/config.clj
Expand Up @@ -25,15 +25,7 @@
[clojurewerkz.propertied.properties :as p]
[environ.core :refer [env]]
[slingshot.slingshot :refer [throw+ try+]])
(:import java.io.FileNotFoundException
java.net.InetAddress))

;; TODO: read from env var
(defonce
^{:dynamic true
:doc "The current environment. use the set-environment!,
environment, and with"}
*environment* (atom nil))
(:import java.net.InetAddress))

(defonce
^{:dynamic true
Expand All @@ -52,20 +44,6 @@
[]
(.getHostAddress (InetAddress/getLocalHost)))

(defn environment*
[]
@*environment*)

(defn environment
"Returns the currently bound environment.
Throws an exception if no environment is bound"
[]
(or (environment*)
(throw
(RuntimeException.
"Environment not set. export CISTE_ENV to choose an environment"))))

(defn merge-config
"Recursively merges m1 into m2. If the value of any of the key is a map, the
elements in that map are also merged"
Expand Down Expand Up @@ -125,31 +103,7 @@
(let [value (apply config* ks)]
(if-not (nil? value)
value
(throw
(IllegalArgumentException.
(str "no config option matching path " ks " for " (environment))))))))

(defn set-config!
"Set the value of the config setting matching the key sequence"
[ks value]
(dosync
(alter *config-map*
assoc-in (concat [(environment)] ks) value))
value)

(defn set-environment!
"Sets's the environment globally"
[env]
(timbre/with-context {:env env}
(timbre/debugf "Setting environment - %s" env))
(dosync (reset! *environment* env)))

(defmacro with-environment
"Run body with the evironment bound"
[environment & body]
`(binding [ciste.config/*environment* (atom nil)]
(set-environment! ~environment)
~@body))
(throw (IllegalArgumentException. (str "no config option matching path " ks)))))))

(defmacro describe-config
"Macro to record config information
Expand Down
26 changes: 13 additions & 13 deletions src/ciste/runner.clj
Expand Up @@ -2,7 +2,7 @@
"This is the runner for ciste applications.
Specify this namespace as the main class of your application."
(:require [ciste.config :refer [config describe-config set-environment!]]
(:require [ciste.config :refer [config describe-config]]
[ciste.service :as service]
[environ.core :refer [env]]
[taoensso.timbre :as timbre])
Expand All @@ -15,10 +15,13 @@
"A namespace containing a logging config")

(defn configure-logging
[environment]
(let [logger (symbol (env :ciste-logger "ciste.logger"))]
(require logger)
((ns-resolve logger 'set-logger))))
[]
(try
(let [logger (symbol (env :ciste-logger "ciste.logger"))]
(require logger)
((ns-resolve logger 'set-logger)))
(catch Exception ex
(timbre/error "Could not set up logging" ex))))

(defn stop-application!
[]
Expand All @@ -29,14 +32,11 @@
(timbre/error "Application promise is nil")))

(defn start-application!
([] (start-application! (env :ciste-env "default")))
([environment] (start-application! environment nil))
([environment modules]
(set-environment! environment)
(configure-logging environment)
(timbre/with-context {:env environment}
(timbre/infof "Starting application with environment: %s" environment))
(service/init-services environment modules)
([] (start-application! nil))
([modules]
(configure-logging)
(timbre/infof "Starting application")
(service/init-services modules)
;; (service/start-services!)
(dosync (ref-set application-promise (promise)))
(timbre/info "application initialized")
Expand Down
12 changes: 5 additions & 7 deletions src/ciste/service.clj
Expand Up @@ -24,13 +24,11 @@
(defn init-services
"Ensure that all namespaces for services have been required and that the
config provider has benn initialized"
([environment] (init-services environment nil))
([environment modules]
(timbre/info "initializing services")
;; TODO: initialize config backend
(load-config! (env :ciste-properties (str "config/" (name environment) ".properties")))
(loader/require-modules modules)
(loader/process-requires)))
[modules]
;; TODO: initialize config backend
(load-config! (env :ciste-properties "config/ciste.properties"))
(loader/require-modules modules)
(loader/process-requires))

(defn stop-services!
"Shut down all services"
Expand Down
2 changes: 1 addition & 1 deletion src/ciste/test_helper.clj
Expand Up @@ -5,6 +5,6 @@
"Wrapper to ensure tests are run in the test environment"
[& body]
`(do
(start-application! :test)
(start-application!)
~@body
(stop-application!)))
5 changes: 1 addition & 4 deletions test/ciste/config_test.clj
@@ -1,14 +1,11 @@
(ns ciste.config-test
(:require [ciste.config :refer [environment get-host-address merge-config]]
(:require [ciste.config :refer [get-host-address merge-config]]
[midje.sweet :refer [contains fact =>]]))

(fact "#'ciste.config/get-host-address"
(fact "should return a string"
(get-host-address) => string?))

(fact "#'ciste.config/environment"
(environment) => :test)

(fact "#'ciste.config/merge-config"
(let [m1 {:key1 "value1"
:key2 {:sub-key1 "value2"
Expand Down

0 comments on commit 6ad9141

Please sign in to comment.