原文:https://github.com/weavejester/compojure/wiki
[compojure "1.1.5"]
lein new compojure hello-world
cd hello-world
lein ring server
(GET "/user/:id" [id]
(str "<h1>Hello user " id "</h1>"))
GET
"/user/:id"
["/user/:id", :id #"[0-9]+"]
[id]
{{id :id} :params}
(str "<h1>Hello user " id "</h1>"))
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body "<h1>Hello user 1</h1>"}
compojure.response/render 将任意一种类型(String、map、File等)的返回值处理成合适的response。它也支持你使用自定义类型重写。
(GET "/:foo" {{foo :foo} :params}
(str "Foo = " foo))
(GET "/:foo" [foo]
(str "Foo = " foo))
{:request-method :get
:uri "/foobar"
:headers {}
:params {:x "foo", :y "bar", :z "baz", :w "qux"}}
[x y z]
x -> "foo"
y -> "bar"
z -> "baz"
[x y & z]
x -> "foo"
y -> "bar"
z -> {:z "baz", :w "qux"}
[x y :as r]
x -> "foo"
y -> "bar"
r -> {:request-method :get
:uri "/foobar"
:headers {}
:params {:x "foo", :y "bar", :z "baz", :w "qux"}}
[x y :as {u :uri}]
x -> "foo"
y -> "bar"
u -> "/foobar"}
user> (use 'compojure.core)
nil
user> (require '[compojure.handler :as handler])
nil
user> (require '[compojure.route :as route])
nil
user> (def my-request
{:request-method :get
:uri "/my-uri"
:headers []
:params {:x "foo" :y "bar" :z "baz" :w "qux"}})
#'user/my-request
user> (defroutes my-3-parameter-route
(GET "/:my" [x y z]
(str "x -> " x "; "
"y -> " y "; "
"z -> " z)))
#'user/my-3-parameter-route
user> (my-3-parameter-route my-request)
;;-> {:status 200
;;-> :headers {"Content-Type" "text/html; charset=utf-8"},
;;-> :body "x -> foo; y -> bar; z -> baz"}
user> (defroutes my-2-parameter-and-remainder-route
(GET "/:my" [x y & z] ; & binds remainder of request map to z
(str "x -> " x "; "
"y -> " y "; "
"z -> " z)))
#'user/my-2-parameter-and-remainder-route
user> (my-2-parameter-and-remainder-route my-request)
;;-> {:status 200,
;;-> :headers {"Content-Type" "text/html; charset=utf-8"},
;;-> :body "x -> foo; y -> bar; z -> {:my \"my-uri\", :z \"baz\", :w \"qux\"}"}
user> (defroutes my-remainder-symbol-route
(GET "/:my" [x y :as r] ; :as keyword assigns entire request map to symbol
(str "x -> " x "; "
"y -> " y "; "
"r -> " r)))
#'user/my-remainder-symbol-route
user> (my-remainder-symbol-route my-request)
;;-> {:status 200,
;;->:headers {"Content-Type" "text/html; charset=utf-8"},
;;->:body "x -> foo; y -> bar;
;;-> r -> {:route-params {:my \"my-uri\"},
;;-> :request-method :get,
;;-> :uri \"/my-uri\",
;;-> :headers [],
;;-> :params {:my \"my-uri\",
;;-> :z \"baz\",
;;-> :y \"bar\",
;;-> :x \"foo\",
;;-> :w \"qux\"}}"}
user> (defroutes my-destructuring-map-route
(GET "/:my" [x y :as {u :uri}] ; :as keyword and destructuring map
; to bind :uri value to u
(str "x -> " x "; "
"y -> " y "; "
"u -> " u)))
#'user/my-destructuring-map-route
user> (my-destructuring-map-route my-request)
;;-> {:status 200,
;;-> :headers {"Content-Type" "text/html; charset=utf-8"},
;;-> :body "x -> foo; y -> bar; u -> /my-uri"}
(defroutes api-routes
(GET "/something" [] ...)) ; matches /api/something
(defroutes main-routes
(context "/api" [] api-routes)
other-routes)