Clojure database settings per environment - clojure

I am using monger to fetch and save some data in MongoDb from my Clojure simple app. I have strong Ruby on Rails background so I am familiar with database settings per environment (development, test, production). I want to have something similar in Clojure. How can I add the environment to my code? I want to do it in Clojure-way, code as data, without any yaml files. I am using Leiningen if it changes something.

You can use Leiningen profiles feature.
In your project.clj define your profiles (most cases you need dev and prod)
:profiles {:dev {:resource-paths ["resource-dev"]}
:prod {:resource-paths ["resource-prod"]}}
Now create 2 directories resource-dev and resource-prod and create config.clj file in both of them which will have define a map to store configuration. Something like:
(ns myapp.config)
(def config {:database "dev"})
Then in your app code you can use below snippet to load the config file (only once) and access the config map:
(use 'clojure.java.io)
(def config (delay (load-file (.getFile (resource "config.clj")))))
(defn get-config []
#(force config))
Now you can use get-config function to access the config map.

Have a look at clj-boilerplate, a sample web app I created.
There's info in the README about how it understands environments out of the box and an example environment file can be seen here - but it looks something like this:
(def config
(let [env (or (System/getenv "ENVIRONMENT") "development")]
((keyword env)
{:development
{:database-url "postgres://lborges:#localhost/clj-boilerplate"}
:test
{:database-url "postgres://lborges:#localhost/clj-boilerplate-test"
:production
{:database-url (System/getenv "DATABASE_URL")}})))
I have since evolved this approach but this should get you started.
Hope this helps.

Have a look at using Confijulate (plug for a personal project!):
https://github.com/bbbates/confijulate
It allows you to define environment specific configuration maps, and specify which one to use via JVM system properties.

Related

Setting up leiningen profiles to multiplex between environments AND multiple mains

I currently have a setup for my Clojure project which allows me to toggle configuration from dev to test or prod by configuring profiles in Leiningen's project map. The section related to the profiles looks like this:
:main ^:skip-aot myproject.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}
:dev {:env {:clj-env :development
:database-uri "jdbc:postgresql://localhost:5432/db_dev"}}
:test {:env {:clj-env :test
:database-uri "jdbc:postgresql://localhost:5432/db_test"}}
:production {:env {:clj-env :production
:database-uri "jdbc:postgresql://localhost:5432/db"}}})
The issue is I would further like to enhance this and be able to toggle between multiple mains. I have seen in other posts that people usually achieve this by configuring the profiles like here.
What I don't know how to do is how to preserve the configuration I have so that profiles correspond to environments but also further configure it in order to be able to choose the main class by simply adding a parameter to lein run.
I have figured out that one way is obviously to keep having just one main class and add that multiplexing with actual Clojure code, but I was hoping to be able to do it via lein configuration.
After trying multiple options, the only way I have found to do this is via aliases.
First setting the :main option to nil, so the MANIFEST.MF doesn't have any Main.class set, and then simply adding a couple of aliases specifying which main class to run.
:main nil
:target-path "target/%s"
:aliases {"main1" ["run" "-m" "project.main1.core"]
"main2" ["run" "-m" "project.main2.core"]}

deploy compojure app that is also a CLI app

I have a Clojure app that I can use both from the command-line, and as a Compojure app.
I did that by putting a ring handler and a main function (clojure.tools.cli) in my leiningen project.clj.
{...
:main my-app.core
:ring {:handler my-app.handler/handler }}
The handler is defined (defroutes handler ...).
Now if I want to run the CLI app, I can run lein uberjar and then java -jar arguments.
And I can also run the Compojure app via lein ring server PORT.
Now, how do I deploy the thing as a Compojure app (and not a CLI app) in a production server ? (Bonus points for explaining how lein ring server works.)
Note : I already use nginx if that can help, and I'm flexible on the container to be used.
Here's kind of the default template I use for new projects. It allows you to do dependency injection into ring apps and run the app from the command line as an uberjar. You can read more here: http://www.sparxeng.com/blog/software/improved-clojure-workflow
; handler.clj
(defn wrap-inject-deps
"Ring middleware that injects the dependencies into each ring request map"
[handler deps]
(fn [req]
(handler (assoc req :deps deps))))
(defn create-handler
"Similar to the default ring 'handler', but with a parameter to let you inject dependencies"
[deps]
(-> (routes
api-routes
web-routes
(route/resources "/"))
(kwp/wrap-keyword-params)
(params/wrap-params)
(json/wrap-json-params)
(json/wrap-json-response)
(wrap-inject-deps deps))) ; this injects dependencies
(defn start-jetty
"Launch the Jetty instance"
[deps]
(let [port (Integer. (or (-> system :env :port) 5000))
handler (create-handler deps)]
(jetty/run-jetty handler {:port port :join? false})))
; system.clj
(defn get-env
"Initialize configuration vars"
[]
{:aws-access-key-id (System/getenv "AWS_ACCESS_KEY_ID")
:aws-secret-access-key (System/getenv "AWS_SECRET_ACCESS_KEY")
:mongo-url (System/getenv "MONGO_URL"))
(defn start
"Launch dependencies such as DB connection and Jetty. Return these as a map, for use in REPL"
[& [env]]
(let [env (or env (get-env))
deps {:env env
:monger (db/init env)}
jetty (handler/start-jetty deps)]
(assoc deps :jetty jetty)))
; program.clj
(defn -main [& [port]]
"App entrypoint"
(let [env (system/get-env) ; "env" is just a map of config variables, that can be hard-coded, read from file, or whatever you want.
env (if port (assoc env :port port) env)]
(system/start env)))
You can then use leiningen profiles if you need to create multiple apps with different entrypoints from your codebase.
lein ring server fires up jetty to serve the web application you've built. It does this by starting the server with the options you specify in :ring. If you specify LEIN_NO_DEV in your environment, the server will run in production mode. Whether jetty is the right fit for your productions needs or not, is up to you to decide.
If you would like to run your web application on a different server, there are multiple options. One of them is running on Tomcat. Take another look at the documentation for lein ring, in particular the section on war files. Basically, you would call lein war or lein uberwar to generate a war file. You can then e.g. drop the war file into the webapps directory of Tomcat.
Another option would be using immutant, which has substantial documentation on installation and running applications.

slf4j File Not Found error when building uberwar

I would like to understand why my Ring application is attempting to open the log file at compile time.
I have a webservice in Compojure and Ring. The application works without issue but whenever I compile the application to an uberwar or run lein ring server I get the below error from slf4j.
I don't understand what is causing the Clojure compiler to attempt to access this log file during compilation. The only clue which I have is that it only started happening when I included the Korma library.
If I create the directory I can get rid of the error, however, I want to understand why the application is attempting to open the log file at compile time.
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /var/log/ege/myservice/myservice.log (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:133)
at org.apache.log4j.FileAppender.setFile(FileAppender.java:294)
at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:207)
at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:165)
at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:307)
at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:172)
at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:104)
at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:809)
at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502)
log4j configuration is a runtime configuration of your app. Korma uses log4j configuration for its usage in runtime. So, it's not surprising that when running lein ring server, korma uses log4j configuration for its logging and the log4j logging throws error cause it can not find the log directory.
As for the error thrown during lein check or lein ring war. Maybe you have some forms which attempts to connect to database or even do some query to your database in top-level of some of your namespaces.
maybe you have something like this:
(ns my-app.db
(:use korma.db)
(:use korma.core))
(def db (create-db (mysql {:db "my_db"
:user "root"
:password ""})))
;; this form below attempts to get a db connection
(def conn (get-connection db))
(defentity users)
;; this form below attempts to query the database
(select users)
when clojure compiles, it executes every form, thus, clojure connects to db or query the db. In other words, clojure runs a part of your app during compilation.
unrelated note
Everything above aside, you should separate the log4j configuration or any other runtime-configs for development environment and production environment.
I usually separate development environment and production environment configuration using :profiles in project.clj.
:profiles
{:production
{:ring
{:open-browser? false, :stacktraces? false, :auto-reload? false}
:source-paths ["prod-config"]}
:dev
{:dependencies [[ring-mock "0.1.5"] [ring/ring-devel "1.3.1"]]
:source-paths ["dev-config"]}}
I put my development's log4j.properties in dev-config dir and production's log4j.properties in prod-config dir.

How to get Clojure Compojure app to run headless via compiled jar within Docker container?

Update: this question has changed since the original set of commenters left responses. Apologies for any confusion.
This is my code repository https://github.com/Integralist/spurious-clojure-example you can use it as an example for what I'm working with.
Note that the above repo relies on a library I've not yet published to Clojars (as I'm still testing it - hence this question opened). You can see the source code of the library here: https://github.com/Integralist/spurious-clojure-aws-sdk-helper
I have a "hello world" Clojure web app written with Compojure that I have working fine when run using lein ring server and lein run (as I have a -main function now created). It also runs to a certain extent when compiled down into a jar and I run java -jar app.jar.
My problem now is that if I try to run the default java -jar app.jar from within a Docker container I get the following error telling me...
spurious-clojure-example is starting
2015-02-14 00:58:03.812:INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
2015-02-14 00:58:03.854:INFO:oejs.AbstractConnector:Started SelectChannelConnector#0.0.0.0:8080
Started server on port 8080
Exception in thread "main" java.awt.HeadlessException:
My code is currently using a -main function like so...
(ns spurious-clojure-example.repl
(:use spurious-clojure-example.handler
ring.server.standalone
[ring.middleware file-info file])
(:gen-class))
(defonce server (atom nil))
(defn get-handler []
(-> #'app
(wrap-file "resources")
(wrap-file-info)))
(defn start-server
"used for starting the server in development mode from REPL"
[& [port]]
(let [port (if port (Integer/parseInt port) 8080)]
(reset! server
(serve (get-handler)
{:port port
:init init
:auto-reload? true
:destroy destroy
:join true}))
(println (str "You can view the site at http://localhost:" port))))
(defn stop-server []
(.stop #server)
(reset! server nil))
(defn -main []
(start-server))
...but how do I get the server to start headless? I can't quite follow the Compojure boilerplate code to decipher where or how it knows when to run headlessly or via browser?
I know that on the command line you can do lein ring server-headless so what's the programmatic equivalent of that?
Because ring-server is primarily meant for development, it tries to open a browser when the server starts. This fails with a java.awt.HeadlessException on platforms without a GUI. You'll want to set the :open-browser? option to false to prevent this.
From the offical Docker docs on EXPOSE
The EXPOSE instructions informs Docker that the container will listen
on the specified network ports at runtime. Docker uses this
information to interconnect containers using links (see the Docker
User Guide) and to determine which ports to expose to the host when
using the -P flag. Note: EXPOSE doesn't define which ports can be
exposed to the host or make ports accessible from the host by default.
To expose ports to the host, at runtime, use the -p flag or the -P flag.
So if you're setting the ring server port manually in your project.clj, ensure that you're using the same port also in your Dockerfile and then provide a mapping via -p or -P when starting docker.
I can't say for sure but I believe that your headless error message doesn't contribute to your problem.

Sharing configuration in Leiningen project.clj

I have a Clojure project that uses the lein-beanstalk Leiningen plugin to deploy my app to Amazon Elastic Beanstalk.
My Elastic Beanstalk application has multiple environments (e.g. development, staging, production, etc.), each with different configuration. The configuration is managed by Elastic Beanstalk environment properties, which are set in the JVM as system properties. lein-beanstalk manages these properties with the following entry in project.clj:
(defproject eb-app "1.0-SNAPSHOT"
:aws {:beanstalk {:environments [{:name "staging"
:env {"com.example.fooLimit" "3"
"com.example.barName" "BAR"}}
{:name "production"
:env {"com.example.fooLimit" "27"
"com.example.barName" "BAR"}}]}})
In this example, only com.example.fooLimit varies between my environments, so I'd like to use a var to hold the shared conf. Since defproject is a macro that quotes everything, I can accomplish what I want with a syntax unquote:
(def cfg {"com.example.barName" "BAR"})
(defproject eb-app "1.0-SNAPSHOT"
:aws {:environments [{:name "staging"
:env ~(merge cfg {"com.example.fooLimit" "3"}})
{:name "production"
:env ~(merge cfg {"com.example.fooLimit" "27"}})}]}})
My question is whether this is common in Clojure projects, or if there is a more idiomatic or industry-standard way (among Clojure programmers) to DRY up project.clj?