-
Notifications
You must be signed in to change notification settings - Fork 0
Routes
A route in Gate is a Clojure map that requires the key :path
, can optionally contain the keys :middleware
and :children
, and can may contain one or more keys representing an HTTP method (ie :get
, :post
, etc).
{:path "/hello"
:get (fn [req] "Hello, World!")
:middleware [gate.middleware/api]}
The only required key for a gate route is :path
.
{:path "/"} ; This won't do much....
A route path is a string representing and abstracted url. It must start with a /
, and can contain any of the following elements, separated by /
.
-
A literal segment of a path is a literal representation of that portion of the path.
For example, in the the path
/hello/world
,hello
andworld
are literal segments. -
A variable segment of a path begins with a
:
and matches anything before the next/
.The path
/hello/:name
would match the url/hello/world
or/hello/github
, and the value of :name would be associated with that keyword in the:params
and:path-params
keys of a request. -
A splat segment of a path begins with a
*
and matches everything from there until the end of the path string./hello/*name
would match/hello/world
, but it would also match/hello/world/here/is/a/cuppa
. Like variable segments, splats associate the string they match with a keyword of their name (ie *name would be :name) in the:params
and:path-params
of a request.
Route maps can contain one or more keys representing HTTP methods. These keys are paired with a handler function that will be called if that method is used to make a request matching the route path. In the following example, the route indicates that if a GET request is made to "/hello/github", the route will return the text "Hello, github! It's been a while!".
{:path "/hello/:name"
:get (fn [req]
(let [{:keys [name]} (get req :params)]
(str "Hello, " name "! It's been a while!")))}
The keywords following keywords are available to use in route maps as possible methods:
:get :post :head :put :delete :trace :connect :options :any
The only keyword not representing an actual HTTP method is :any
, which will match any method of request.