Compojure is no longer a complete framework for developing web applications. Since the 0.4 release, compojure has been broken off into several projects.
Ring provides the foundation by abstracting away the HTTP request and response process. Ring will parse the incoming request and generate a map containing all of the parts of the request such as uri, server-name and request-method. The application will then handle the request and based on the request generate a response. A response is represented as a map containing the following keys: status, headers, and body. So a simple application would look like:
(def app [req]
(if (= "/home" (:uri req))
{:status 200
:body "<h3>Welcome Home</h3>"}
{:status 200
:body "<a href='/home'>Go Home!</a>"}))
One other part of Ring is the concept of middle-ware. This is code that sits between the handler and the incoming request and/or the outgoing response. Some built in middle-ware include sessions and stacktrace. The session middle-ware will add a :session key to the request map that contains all of the session info for the user making the request. If the :session key is present in the response map, it will be stored for the next request made by the current user. While the stack trace middle-ware will capture any exceptions that occur while processing the request and generate a stack trace that is sent back as the response if any exceptions do occur.
Working directly with Ring can be tedious, so Compojure is built on top of Ring abstracting away the details. The application can now be expressed in terms of routing so you can have something like this:
(defroutes my-routes
(GET "/" [] "<h1>Hello all!</h1>")
(GET "/user/:id" [id] (str "<h1>Hello " id "</h1>")))
Compojure is still working with the request/response maps so you can always access them if needed:
(defroutes my-routes
(GET "*" {uri :uri}
{:staus 200 :body (str "The uri of the current page is: " uri)}))
In this case the {uri :uri} part accesses the :uri key in the request map and sets uri to that value.
The last component is Hiccup which makes generating the html easier. The various html tags are represented as vectors with the first element representing the tag name and the rest being the body of the tag. "<h2>A header</h2>"
becomes [:h2 "A Header"]
. The attributes of a tag are in an optional map. "<a href='/login'>Log In Page</a>"
becomes [:a {:href "/login"} "Log In Page"]
. Here is a small example using a template to generate the html.
(defn layout [title & body]
(html
[:head [:title title]]
[:body [:h1.header title] body]))
(defn say-hello [name]
(layout "Welcome Page" [:h3 (str "Hello " name)]))
(defn hiccup-routes
(GET "/user/:name" [name] (say-hello name)))
Here is a link to a rough draft of some documentation currently being written by the author of compojure that you might find helpful: Compojure Doc
By far the best Clojure web framework I have yet encountered is Compojure: http://github.com/weavejester/compojure/tree/master
It's small but powerful, and has beautifully elegant syntax. (It uses Jetty under the hood, but it hides the Servlet API from you unless you want it, which won't be often). Go look at the README at that URL, then download a snapshot and start playing.
There's also "Noir" (http://www.webnoir.org/), which is a new Clojure web framework (so new the docs aren't there yet). Coming from Django/Rails, I dig the simple, straightforward syntax and it's pretty lean.
Consider the Luminus web framework. I have no affiliation but have heard good things from friends I respect.
My current go-to web library is now yada.
If you are just starting out, the introductory server is Compojure
. I see it as the apache
of web servers in the Clojure world (in which case yada/aleph would be nginx). You could use Luminus
as a template. There are variants of it, like compojure-api
.
I tried ou Pedestal
and was globally satisfied with it. I don't claim to master it, but it has a pleasant syntax, feels very cohesive, and looks like it does have great performance. It is also backed by Cognitect
(the Clojure/Datomic company where Rich Hickey works).
I found Aleph
to present an interesting abstraction, and the built-in backpressure seems interesting. I have yet to play with it, but it's definitely on my list.
After playing a bit with various web servers, here is my quick Pro/Cons list :
Short answer : have a look at Luminus to get started quickly, maybe move on to something else as your needs evolve (Yada maybe).
Pros (1):
Cons (2):
Pros (3):
Cons (4):
Pro (3):
Cons (1):
Pro (3):
Cons (1):
Pro (2):
Cons (2):
Note : I haven't played with it, mainly because of the lack of documentation. It looks interesting though, and very performant.
Pros (2):
Cons (1):
Note : I haven't played with it.
Pros :
Cons :
Note : I haven't played with it, although the documentation looks excellent. I am probably going to try it next. There are example chat projects that look interesting, their heavy use of protocols put me off at first as a novice Clojure dev.
Pros (6):
Cons (2):
Note : a benchmark of Clojure web servers is available, if raw performance is all that matters.
These days Pedestal is a framework worth a look. It's a server-side framework that builds on top of Ring, but also frees the incoming request from the initial thread by being able to pause and resume that particular request (otherwise a slow request actually block that serverthread). Maybe sort of like a JavaBean.
Other cool frameworks are hoplon.io and David Nolen's Om (based on React)
Webjure, a web programming framework for Clojure.
Features: Dispatch servlet calls Clojure functions. Dynamic HTML generation. SQL query interface (through JDBC).
This answer is meant as a placeholder for Webjure information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With