Correct usage of Environ library in profiles - clojure

I have a question regarding of the usage of profiles and environment variables. Is it possible to export variables with environ depending on the which profile is set? This is what I have tried (partial project.clj)
:env {:time-to-wait-for-response "72" ; in hours
:crm-address "https://app.onepagecrm.com/api/v3/"}
:profiles
{:uberjar {:omit-source true
:aot :all}
:uberwar {:omit-source true
:aot :all}
:prod {:ring {:open-browser? false
:stacktraces? false
:auto-reload? false}
{:env {:prod? true
:db-user "mailer"
:db-password "" }}
:dev {:env {:db-user "mailer"
:db-password ""}
:dependencies [[ring-mock "0.1.5"]
[ring/ring-devel "1.3.1"]
[midje "1.6.3"]]
:ring {:open-browser? false}
:plugins [[lein-midje "3.2.1"]]}})
However if I build the WAR file with lein ring uberwar(even with with-profile dev) and deploy it to Tomcat I will get db-user as nil (called with (env :db-user). However lein ring server correctly uses dev profile so it works. Is there something wrong I am doing? Or environ is not supposed to be used this way?

This reason it is not working as you expect is because you're only setting those variables when the profile is active. Once you have built a WAR file and deployed it, lein is no longer in the picture. You'll need to load these variables in a different way. If you want to stick with environ, the two options seem to be as Java system properties or environment variables.

Related

Dynamic leiningen :profiles

I'm trying to use a function as the value for the :profiles key in a defproject form. Starting from a fresh project (lein new app test) this works fine:
:profiles {}
(as you might hope!). But if I change it to:
:profiles (merge {})
then when I run lein repl it explodes:
Caused by: java.lang.ClassCastException: clojure.lang.Symbol cannot be cast to java.util.Map$Entry
I'm confused by this since if I set :profiles back to the empty map and ask the repl these things are equal:
test.core=> (= {} (merge {}))
true
Where is my misunderstanding? Have I missed something basic? Is this an unfortunate artifact of the defproject macro? Something else?
(clojure 1.8.0, leiningen 2.7.1, java 1.8.0_102)
Edit - working solution with Scott's answer:
(def project-name 'myproj)
(def mains ["foo" "bar"])
...
(defn- lein-alias [main]
{ main ["with-profile" main] })
(defn- lein-profile [main]
(let [jar (str main ".jar")
entry `~(str project-name "." main)]
{(keyword main) {:main entry
:bin {:name main}
:jar-name jar
:uberjar-name jar}}))
(defproject project-name "0.1.0"
...
:profiles ~(apply merge (concat (map lein-profile mains) {:uberjar {:aot :all}}))
:aliases ~(apply merge (map lein-alias mains))
...
So now I can lein foo bin and lein bar bin to my heart's content.
If you unquote your form Leiningen will execute the form before the project map is evaluated. So ~(merge {}) should work.
There is a function called unquote-project in Leiningen
src/leiningen/core/project.clj#L176
"Inside defproject forms, unquoting (~) allows for arbitrary evaluation."
It looks like it looks items to unquote to allow them to be execute. Based on the comment it looks like this might go away in 3.0 and suggests use read-eval syntax
Note: If you are just trying to merge values of different profiles you should look at the Profiles documentation About Merging and Composite Profiles
Composite Profiles
Sometimes it is useful to define a profile as a combination of other profiles. To do this, just use a vector instead of a map as the profile value. This can be used to avoid duplication:
{:shared {:port 9229, :protocol "https"}
:qa [:shared {:servers ["qa.mycorp.com"]}]
:stage [:shared {:servers ["stage.mycorp.com"]}]
:production [:shared {:servers ["prod1.mycorp.com", "prod1.mycorp.com"]}]}
You do not merge the profiles manually. They are merged together by lein either automatically (normal case) or when you use the with-profile keyword (manual control).
For example, consider this project.clj:
(defproject xyz "0.1.0-SNAPSHOT"
:dependencies [ [org.clojure/clojure "1.8.0"]
[tupelo "0.9.19"] ]
:profiles {:dev {:dependencies [ [org.clojure/test.check "0.9.0"]
[criterium "0.4.4"] ] }
:sample {:dependencies [medley "0.8.2"] }
...
)
This project.clj says that the project always requires both org.clojure/clojure and tupelo. During development, the map for :dev will be merged into the root-level, so :dependencies will be updated to include both test.check and criterium. The :dev profile values are not included when a uberjar is created, however, so these libs won't be included in the code delivered to users.
Since :sample is not one of the default profiles, it will only be included if you use a command like:
> lein with-profile sample test
Notice that the leading colon is not included on the command line, although we use a keyword :sample with the colon in the project.clj file.
Full details are here: https://github.com/technomancy/leiningen/blob/master/doc/PROFILES.md#default-profiles
and here: https://github.com/technomancy/leiningen/blob/master/sample.project.clj
Having said all this, I normally don't need to use :profiles. Unless you have something more complex than usual, you should usually just put all of your dependencies at the root level (i.e. the :dependencies keyword at the first level under (defproject xyz ...) in the project that lein new app xyz gives you.
Another suggestion: the word test is used in many places in a lein project (directory name, file name suffix, and others), so it can be very confusion to name the project itself test! You'll save yourself (and any other readers) some grief if you choose a unique name like xyz, joe, or anything else.

Slow loading of cljsbuild in Clojure

I'm new to Clojure and I'm trying to setup a development environment where I can dynamically load my web project files using ring-server and cljsbuild
i have the following snippet in my project file
:ring {
:handler cjohansen-no.web/app
;;:auto-refresh true
;;:auto-reload? true
;;:reload-paths ["resources","src"]
;;:refresh-paths ["resources","src"]
}
:aliases {
"start-server" ["ring" "server-headless"]
"build-site" ["run" "-m" "cjohansen-no.web/export"]
"build-html" ["run" "-m" "cjohansen-no.web/export-pages"]
"build-js" ["cljsbuild" "auto" "dev"]
"build-web" ["do" ["build-site"] ["build-js"]]
"build-dev" ["pdo" ["build-web"] ["start-server"] ["auto" "build-html"]]
}
:source-paths ["src"]
:test-paths ["test/cjohansen_no" "test/cljs" "spec"]
:clean-targets [:target-path "./build/js/out"
:compile-path "classes"
:output-paths "./build/js/output"
"build/js/main.js.map"
]
:main cjohansen-no.web
:clean-non-project-classes true
:figwheel {
;;:server-port 3000
:css-dirs ["resources/public/css"]
:reload-clj-files {:clj true :cljc false}
:ring-handler cjohansen-no.web/app
:repl false
}
:cljsbuild {
:builds [
{
;; :notify-command ["growlnotify" "-m"]
:id "dev"
:source-paths ["src/cljs"]
:figwheel {
:websocket-host :js-client-host
:autoload false
:reload-dependents true
:debug true
}
:compiler {
:main scripts.core
:output-to "resources/public/js/main.js"
:output-dir "resources/public/js/out"
:optimizations :none
:source-map true
:source-map-timestamp true
:recompile-dependents false
:pretty-print true
:asset-path "js/out"
;;:notify-command ["bin/phantomjs" "bin/speclj.js" "resources/public/js/main.js"]
}
},
....
}
:profiles {
:dev {
:dependencies [
;;[figwheel "0.5.4-7"]
]
:plugins [
[lein-pdo "0.1.1"]
[lein-ring "0.9.7"]
[lein-cljsbuild "1.1.3"]
[lein-figwheel "0.5.4-7"]
]
}
I use this snippet to run my server
(def app (->
(stasis/serve-pages get-pages)
(optimus/wrap get-assets optimizations/all serve-live-assets)
;;(wrap-cljsbuild "/js/" cljsbuild)
wrap-content-type
;; wrap-reload
wrap-utf-8))
I'm loading my assets using Optimus
(defn get-assets []
(concat (assets/load-bundle "public" "styles.css" [#"css/.+\.css$"])
(assets/load-assets "public" [#"img/.*" "/questions.json"])
(assets/load-bundle "public" "main.js" [#"js/.+\.js"])
))
when I run lein with-profile dev pdf start-server, cljsbuild auto and bring up my website the js files and its dependants as compiled by the "dev" build takes FOREVER to complete loading. Why is the ring server so slow?
Should I be referencing the build files in this manner? should i bundle it into one file?
The profile used is for development purposes.
That is a pretty complex project.clj file if your just starting up and are new to clojure/clojurescript. It is possible that starting with just the bare lein figwheel template might be a good way to get a basic environment and then just add to it once you know you need additional bits. It could be that simply parsing your project file is slowing things down.
The basic figwheel template will set things up so that it all reloads when necessary - there really isn't much extra you need to worry about.

Ragtime Migrate with Environment Variables throwing Error (Heroku Deployment)

I'm trying to run lein ragtime migrate on a heroku dyno. Normally, I would set the database path in my project.clj like so:
(defproject my-project "0.1.0-SNAPSHOT"
:min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/java.jdbc "0.3.7"]
[postgresql "9.3-1102.jdbc41"]
[ragtime "0.3.9"]
[ring "1.4.0-RC1"]
[ring/ring-defaults "0.1.2"]]
:plugins [[lein-ring "0.8.13"]
[ragtime/ragtime.lein "0.3.9"]]
...
:ragtime {:migrations ragtime.sql.files/migrations
:database (System/getenv "DATABASE_URL")}
...
:profiles
{:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
[ring-mock "0.1.5"]]
:test {:ragtime {:database (System/getenv "DATABASE_URL")}}})
When I run the command, I get the following error both locally and depolying over Heroku
java.lang.IllegalArgumentException: No method in multimethod 'connection' for dispatch value: postgres
Any pointers in the right direction would be very appreciated.
Ragtime 0.3.9 uses the scheme from the connection url as the dispatch value for the connection multimethod. The code is here and here. But the DATABASE_ENV from heroku doesn't have a "jdbc" but a "postgres" scheme (which makes sense, it has to be generic).
A workaround could be to add the "jdbc://" prefix:
:ragtime {:migrations ragtime.sql.files/migrations
:database ~(str "jdbc://" (System/getenv "DATABASE_URL"))}
You can also upgrade to [ragtime "0.4.0"] which doesn't use the scheme to find out how to create the connection. See the wiki for info about the upgrade path from 0.3.x

Leiningen 2-steps build: how to combine lein-less and lein-asset-minifier

I'm making a Clojure/ClojureScript website that I deploy on Heroku. I'm using Leiningen for project management.
I want to write my stylesheets in LESS. I need the build process to compile the LESS files into CSS, then minify these CSS files; obviously, I do not want the generated CSS files to be under version control.
LESS files --lein-less--> CSS files --lein-asset-minifier--> minified CSS files
I have tried to implement this with the lein-less and lein-asset-minifier Leiningen plugins. My attempt consisted of declaring leiningen.less and minify-assets.plugin/hooks as hooks of the :uberjar task, in proper order (see code below). But running lein uberjar fails with the following error:
Uberjar aborting because jar failed: resources/public/css/site.css (No such file or directory)
So it appears the order of build steps is not enforced.
Is it possible to implement this multi-step build with these Leiningen plugins? If not, how are people doing it?
Code
Here is the relevant part of my project.clj :
(defproject sncf-cljs "0.1.0-SNAPSHOT"
;; ...
:min-lein-version "2.5.0"
:source-paths ["src/clj" "src/cljs"]
:dependencies [
;; ...
]
:plugins [
[lein-cljsbuild "1.0.4"]
[lein-environ "1.0.0"]
[lein-ring "0.9.1"]
[lein-asset-minifier "0.2.2"]
[lein-less "1.7.2"]]
:less {:source-paths ["src/less"]
:target-path "resources/public/css"}
:uberjar-name "sncf-cljs.jar"
:minify-assets {:assets
{"resources/public/css/site.min.css" "resources/public/css/site.css"}}
:cljsbuild {
;; ...
}
:profiles {
;; ...
:uberjar {:hooks [leiningen.less
leiningen.cljsbuild
minify-assets.plugin/hooks]
:env {:production true}
:aot :all
:omit-source true
:cljsbuild {:jar true
:builds {:app
{:source-paths ["env/prod/cljs"]
:compiler
{:optimizations :advanced
:pretty-print false}}}}}
:production {:ring {:open-browser? false
:stacktraces? false
:auto-reload? false}
:cljsbuild {:builds {:app {:compiler {:main "sncf-cljs.prod"}}}}
}})
I did some research because it is an interesting question. So I looked around, and could not find informations how to configure this. So I continued to search and I found a plugin for this task.
https://github.com/kumarshantanu/lein-cascade
The readme on the github page should be sufficient to solve this task.

Auto building Clojurescript files for Compojure app

I have a web application where i'm using Compojure on the server and Clojurescript on the client. I'm using the leing-cljsbuild plugin to automatically compile cljs files to js.
I'm able to generate the required client side files and load them in the browser when I set the optimizations to :whitespace or :simple, but when I set optimizations to none, the js files reference their dependencies using the local file-system path, which leads to the files not loading at all in the browser.
So, my question is how do I make the generated files use server urls instead of local file paths when they are generated by the clojurescript compiler.
Here's my project.clj file
(defproject my-proj-clj "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:dependencies [[org.clojure/clojure "1.5.1"]
[compojure "1.1.6"]
[org.clojure/tools.nrepl "0.2.3"]
[hiccup "1.0.3"]
[com.novemberain/monger "1.5.0"]
[org.clojure/clojurescript "0.0-2127"]
[jayq "2.5.0"]
]
:plugins [[lein-ring "0.8.8"]
[lein-cljsbuild "1.0.1"]
]
:ring {:handler my-proj-clj.handler/app
}
:cljsbuild { :builds
[{
:source-paths ["src/my-proj-clj"]
:compiler {
:output-dir "./resources/public/js"
:output-to "./resources/public/js/cljs-file.js"
:pretty-print true
:source-map "./resources/public/js/cljs-file.js.map"
:optimizations :none
}}]}
:profiles {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]]}}
I believe the only valid optimization values are :whitespace, :simple, or :advanced. See line 96 at https://github.com/emezeske/lein-cljsbuild/blob/1.0.1/sample.project.clj.
Thus I would use :whitespace as the optimization level (at least to get something working).
Per your post, an optimization level of :whitespace works? Thus, perhaps you can elaborate.
What results are you expecting from an optimization level of ":none". How does your expected result differ from what an optimization level of :whitespace produces?
An optimization level of :none means cljsbuild is not generating js from your cljs source (it will generate a few goog.include statements but nothing else). Try using an interactive repl to help you prototype. Try running the following : lein trampoline cljsbuild repl-rhino
Hope that helps.
I have roughly same setup, optimizations set to :none, generated files use local paths. However, browser does load the scripts.
What I have is this in index.html:
<script src="js/development/goog/base.js" type="text/javascript"></script>
<script src="js/development/main.js" type="text/javascript" ></script>
<script type="text/javascript">goog.require("ixtlan.core");</script>
this in project.clj:
:cljsbuild {
:builds [{:id "dev"
:source-paths ["src/cljs"]
:compiler {
:output-to "resources/public/js/development/main.js"
:output-dir "resources/public/js/development"
:optimizations :none
:source-map true}}
...
and routes contain:
(defroutes routes
(GET "/" [] (index))
(route/files "/" {:root "resources/public"}))
Hope, this helps.