Brad Lucas

Programming, Clojure and other interests
June 30, 2017

Devnull

For testing it is sometimes of interest to have a web service which returns OK for anything that is passed to it much like the null device in Unix. In Unix you can open open the /dev/null device and write to it. It is an empty file that accepts all data written to it without storing the data.

For a web service to act in a similar fashion it would run and accept anything passed as a parameter in urls passed to it.

Clojure

How could such an application be built in Clojure? One way would be to use HTTP Kit and have it return OK for anything that is passed to it.

Example

Add http-kit to your project.clj file.

 :dependencies [
                 [http-kit "2.2.0"]
               ]

Then you'll want the most basic of handlers. This returns OK for any request that is passed to it.

(defn handler [req]
  {:status 200
   :headers {"Context-Type" "text/html"}
   :body "OK"})

To run the server from a repl session you can use the run-server function. This returns another function which you can use to shutdown the server.

(def shutdown
    (run-server handler {:3002}))

Calling `http://localhost:3002/' should return OK.

Finally, to stop the server use the shutdown function.

(shutdown)

See the sample in the GitHub link below for a complete project of the above.

Tags: clojure