Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

HTTP route-matching library for Clojure

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 src
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE.html
Octocat-spinner-32 README.md
Octocat-spinner-32 project.clj
README.md

Clout

Clout is a library for matching Ring HTTP requests. It uses the same routing syntax as used by popular Ruby web frameworks like Ruby on Rails and Sinatra.

Installation

Add the following to your project.clj dependencies:

[clout "1.0.0-RC1"]

Usage

These following examples make use of the ring-mock library to generate Ring request maps.

user=> (use 'ring.mock.request 'clout.core)
nil
user=> (route-matches "/article/:title"
                      (request :get "/article/clojure"))
{:title "clojure"}
user=> (route-matches "/public/*"
                      (request :get "/public/style/screen.css"))
{:* "style/screen.css"}

Clout can also match absolute routes:

user=> (route-matches "http://subdomain.example.com/"
                      (request :get "http://subdomain.example.com/"))
{}

Clout supports both keywords and wildcards. Keywords (like ":title") will match any character but the following: / . , ; ?. Wildcards (*) will match anything.

If a route does not match, nil is returned:

user=> (route-matches "/products" "/articles")
nil

For additional performance, you can choose to pre-compile a route:

user=> (def user-route (route-compile "/user/:id"))
#'user/user-route
user=> (route-matches user-route (request :get "/user/10"))
{:user "10"}

When compiling a route, you can specify a map of regular expressions to use for different keywords. This allows more specific routing:

user=> (def user-route (route-compile "/user/:id" {:id #"\d+"}))
#'user/user-route
user=> (route-matches user-route (request :get "/user/10"))
{:user "10"}
user=> (route-matches user-route (request :get "/user/jsmith"))
nil
Something went wrong with that request. Please try again.