Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the good starting point to developing RESTful web service in Clojure?

I am looking into something lightweight, that, at a minimum should support the following features:

  • Support for easy definition of actions through metadata
  • Wrapper that extracts parameters from request into clojure map, or as function parameters
  • Support for multiple forms of authentication (basic, form, cookie)
  • basic authorization based of api method metadata
  • session object wrapped in clojure map
  • live coding from REPL (no need to restart server)
  • automatic serialization of return value to json and xml
  • have nice (pluggable) url parameter handling (eg /action/par1/par2 instead of /action?par1=val1&par2=val2)

I know it is relatively easy to roll my own micro-framework for each one of these options, but why reinvent the wheel if something like that already exists? Especially if it is:

  • Active project with rising number of contributors/users
  • Have at least basic documentation and tutorial online.
like image 469
Marko Avatar asked Jul 05 '10 15:07

Marko


People also ask

How do I use a RESTful web service?

A more useful way to consume a REST web service is programmatically. To help you with that task, Spring provides a convenient template class called RestTemplate . RestTemplate makes interacting with most RESTful services a one-line incantation. And it can even bind that data to custom domain types.


1 Answers

First of all, I think that you are unlikely to find a single shrinkwrapped solution to do all this in Clojure (except in the form of a Java library to be used through interop). What is becoming Clojure's standard Web stack comprises a number of libraries which people mix and match in all sorts of ways (since they happily tend to be perfectly compatible).1

Here's a list of some building blocks which you might find useful:

  1. Ring -- Clojure's basic HTTP request handling library; all the other webby libraries (for writing routes &c.) that I know of are compatible with Ring. Ring is being actively developed, has a robust community, is very well-written and has a nice SPEC document detailing its design philosophy. This blog post provides a nice example of how it might be used (reacting to GitHub commits).

  2. Sandbar -- currently an authentication library, more types of functionality planned; under development.

  3. Compojure -- a mature and robust library which provides a nice DSL for writing routes to be used on top of Ring. This will give you the nice URL parameter handling.

  4. Compojure-rest -- "a library for building RESTful applications on top of Compojure". Compojure-rest is, as far as I can tell, in its early stages of development; perhaps you might see this as an opportunity to influence its design. :-)

  5. For dealing with XML, there's clojure.contrib.lazy-xml (and the helper library clojure.contrib.zip-filter.xml) and Enlive (the built-in clojure.xml namespace is currently not very usable); these would be used in tandem (though for your purposes the former might suffice).

  6. For JSON, there a library in contrib and clojure-json (and I think there was at least one other lib I seem to be forgetting now...); pick the one you like best.

All of will be perfectly happy with a REPL-driven development style (see the accepted answer to this SO question for a Ring trick which is very much to the purpose here). I suppose the above collection of links does leave a few blind spots (in particular, the authentication story is still being ironed out, as far as I can tell), but hopefully it's a good start.


1The only single-package solution for building webapps in Clojure that I know of is Conjure, inspired by Rails; unfortunately I have to admit that I don't know much about it, so if you feel interested, follow the link and look around the sources, wiki &c.

like image 143
Michał Marczyk Avatar answered Oct 05 '22 13:10

Michał Marczyk