Pages

Saturday, 22 October 2016

Set Environment Variables When Wildfly Is Run As A Service

If you are running wildfly server as a service under GNU/Linux, setting environment variables under profile.d, bash_rc etc won't be read. In order for your war to get the environment variable follow the below steps. Say the variable is APP_PROPERTIES and the path is /opt/config/app.properties.
# Add the following line in /etc/default/wildfly.conf
APP_PROPERTIES="/opt/config/app.properties"
# export the variable in /etc/init.d/wildfly
export APP_PROPERTIES

Saturday, 15 October 2016

Run a Ring App With Compojure and Boot

Previously I have discussed on how to run a ring app using boot. Here we will see how to run a ring app that uses compojure for routes using boot. We will be using jetty as the server. The difference is that in a pure ring based app, you will pass the ring handler function to the run-jetty method, but here you will pass (compojure.handler/site routes-method) to run-jetty method.
compojure-boot
|   build.boot
|
\---src
    \---qlambda
        \---server
                core.clj
;core.clj
(ns qlambda.server.core
  (:use compojure.core)
  (:require [ring.adapter.jetty :as jetty]
            [compojure.handler :as handler]))

(defn greet []
  "Hello from compojure")

(defroutes api-routes
  (GET "/" [] (greet)))

(defn run-server []
  (jetty/run-jetty (handler/site api-routes) {:port 8080}))
;boot.clj
(set-env!
  :source-paths #{"src"}
  :dependencies '[[org.clojure/clojure "1.8.0"]
                  [ring "1.5.0"]
                  [compojure "1.5.1"]])

(require '[qlambda.server.core :as server])

(deftask run []
  (with-pre-wrap fileset (server/run-server) fileset))
From the project root, run the server using boot as
boot run
Open http://localhost:8080 to see the response.

Hot Code Reloading Using http-kit and Lein

You can use the wrap-reload from ring.middleware.reload package. This reload the namespaces of modified files before each request.
;core.clj
(ns com.qlambda.server.core
  (:use [compojure.core :only [defroutes GET]]
        [compojure.handler :only [site]]
        [ring.middleware.reload :only [wrap-reload]]
        org.httpkit.server))

(defroutes app-routes
  (GET "/" [] "hello world"))

(defn -main []
  (run-server (wrap-reload (site #'app-routes)) {:port 8080})
  (println "Server started."))
;project.clj
(defproject server "1.0.0"
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [ring "1.5.0"]
                 [compojure "1.5.1"]
                 [http-kit "2.2.0"]]
  :main com.qlambda.server.core)
Now start the server using
lein run
Go to http://localhost:8080 and you will see "hello world". Modify the message in the core.clj, reload the browser page and you will see the new message. Note that the you need to pass the route function as a var to wrap-reload using the #' reader macro for this to work.

Run Clojure Ring App Using Boot

Boot is a Clojure build framework. Here is an example of a basic ring application that is run using boot. The project folder is shown below.
\---ring-helloworld
    |   build.boot
    |
    \---src
        \---qlambda
            \---server
                    core.clj
;core.clj
(ns qlambda.server.core
  (:require [ring.adapter.jetty :as jetty]))

(defn handler [req]
  {:status 200
   :body "Hello World"})

(defn -main [& args]
  (jetty/run-jetty handler {:port 8080}))
;boot.clj
(set-env!
  :source-paths #{"src"}
  :dependencies '[[org.clojure/clojure "1.8.0"]
                  [ring "1.5.0"]])

(require '[qlambda.server.core :as server])

(deftask run []
  (with-pre-wrap fileset (server/-main) fileset))
To run go to the project root directory and execute
boot run
Go to http://localhost:8080 to see the response.