Skip to content
项目

关于

HTTP route-matching library for Clojure

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Clout

Build Status

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 "2.2.1"]

Usage

Require Clout in the normal way:

(require '[clout.core :as clout])

These following examples also make use of the Ring-Mock library to generate Ring request maps:

(require '[ring.mock.request :as mock])

Routes can match by keyword:

(clout/route-matches
 "/article/:title"
 (mock/request :get "/article/clojure"))

=> {:title "clojure"}

Or with wildcards:

(clout/route-matches
 "/public/*"
 (mock/request :get "/public/style/screen.css"))
 
=> {:* "style/screen.css"}

Clout can also match absolute routes:

(clout/route-matches
 "http://subdomain.example.com/"
 (mock/request :get "http://subdomain.example.com/"))

=> {}

And scheme-relative routes:

(clout/route-matches
 "//subdomain.example.com/"
 (mock/request :get "http://subdomain.example.com/"))

=> {}

(clout/route-matches
 "//subdomain.example.com/"
 (mock/request :get "https://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:

(clout/route-matches "/products" (mock/request :get "/articles"))

=> nil

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

(def user-route
  (clout/route-compile "/user/:id"))

(clout/route-matches user-route (mock/request :get "/user/10"))

=> {:id "10"}

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

(def user-route
  (clout/route-compile "/user/:id" {:id #"\d+"}))

(clout/route-matches user-route (mock/request :get "/user/10"))

=> {:user "10"}

(clout/route-matches user-route (mock/request :get "/user/jsmith"))

=> nil

You can also specify regular expressions inline in braces after the keyword:

(def user-route
  (clout/route-compile "/user/:id{\\d+}"))

Note that regular expression escape sequences (like \d) need to be double-escaped when placed inline in a string.

License

Copyright © 2018 James Reeves

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

探索相关项目

加载中…