Saturday, October 15, 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.
|   build.boot
(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}))
    :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.
(ns com.qlambda.server.core
    (:use [compojure.core :only [defroutes GET]]
          [compojure.handler :only [site]]
          [ring.middleware.reload :only [wrap-reload]]

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

(defn -main []
    (run-server (wrap-reload (site #'app-routes)) {:port 8080})
    (println "Server started."))
(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.
    |   build.boot
(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}))
    :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.

Monday, September 19, 2016

Permutation of String in JavaScript

A simple algorithm to find the permutation of a string in JavaScript.
 * Find the permutations of the given string.
 * @param s The input string
 * Algorithm:
 * 1. If the length of the string is 1, return the string as permutation of 'a' is 'a'
 * 2. Else for the given string find the last character char and the substring by stripping the last char
 *    2.1 Recurse with the substring
 * 3. For each permutation from step 2 add the char to each position of the substring and return
function permute(s) {
    var len = s.length, char, i = 0, j = 0, p = [], elem, pArr = [], plen = 0;
    if (len == 1) return s;
    char = s.charAt(len-1);
    pArr = permute(s.substring(0, len-1));
    plen = pArr.length;

    for (i = 0; i < plen; i++) {
        for (j = 0; j < len; j++) {
            elem = pArr[i].split("");
            elem.splice(j, 0, char);
    return [ Set(p)];
permute("abc"); // [ 'cba', 'bca', 'bac', 'cab', 'acb', 'abc' ]
Download from github.

Sunday, September 18, 2016

Make a Copy of an Array in JavaScript

If you want to make a copy of an array in JavaScript, you can use Array#slice method. This way we will get a copy of the array. If we are simply assigning the array to another variable, it is not making a copy. So performing splice() operation on the array will affect the value stored in both variables.
var a = [1,2,3],
    b = a;
a.splice(1,1); // => a is [1,3] and b is also [1,3]
var a = [1,2,3],
    b = a.slice(0);
a.splice(1,1); // => a is [1,3] and b is [1,2,3]

Thursday, September 15, 2016

jQuery & AngularJS Tech Talk Slides

These are presentation slides of jQuery and AngularJS - A Brief Introduction for a tech talk conducted at TCS, Trivandrum DC.

Saturday, September 3, 2016

Override Properties File Using Classpath

Say you have a file containing various configuration details. This file is placed under the resources folder and loaded using classpath. Then you can override the file during runtime by specifying a different file to the classpath command line argument. Let's say that the main class is inside com.example.test package and the test.jar is our program. Use the following command to override the file.
java -classpath .;test.jar;; com.example.test.Main