clojure Getting started with web development Create new Ring application with http-kit


Ring is de-facto standard API for clojure HTTP applications, similar to Ruby's Rack and Python's WSGI.

We're going to use it with http-kit webserver.

Create new Leiningen project:

lein new app myapp

Add http-kit dependency to project.clj:

  :dependencies [[org.clojure/clojure "1.8.0"]
                 [http-kit "2.1.18"]]

Add :require for http-kit to core.clj:

(ns test.core
  (:require [org.httpkit.server :refer [run-server]]))

Define ring request handler. Request handlers are just functions from request to response and response is just a map:

(defn app [req]
  {:status  200
   :headers {"Content-Type" "text/html"}
   :body    "hello HTTP!"})

Here we just return 200 OK with the same content for any request.

Start the server in -main function:

(defn -main
  [& args]
  (run-server app {:port 8080}))

Run with lein run and open http://localhost:8080/ in browser.