Trot v0.7.0 Trot.Router View Source

Module for creating routes based on the URL path. Routes are specified using one of the HTTP method macros: get/2 post/2 put/2 patch/2 delete/2 options/2. The first argument is a the path to route to, and the second argument is the block of code to execute. See examples below.

Module attributes

@path_root: URL path to prefix to all routes in the module. Defaults to “/“.

@static_root: File path to use as the root when looking for static files. Defaults to “priv/static”.

Link to this section Summary

Functions

Extracts the request headers to be used in route matches. All headers are sanitized to lower case strings

Same as build_headers_match/1 but with an existing dictionary of match options passed in

Builds the pattern that will be used to match against the request’s host

Same as build_host_match/1 but with an existing dictionary of match options passed in

Redirect the request to another location

Extract the path and any guard statements from the path. Guard statements defaults to true

Sets up routes from other modules my plugging into the match/2 function in the module

Encodes HTTP responses as appropriate and passes them to Plug.Conn

Redirects all incoming requests for “from” to “to”. The value of “to” will be put into the Location response header

Sets up a route for static to path at the @static_root, which defaults to “priv/static”

Sets up a route to static assets. All requests beginning with “at” will look for a matching file under “from”. @static_root will be prepended to “from” and defaults to “priv/static”

Link to this section Functions

Link to this function build_headers_match(headers) View Source

Extracts the request headers to be used in route matches. All headers are sanitized to lower case strings.

Examples

iex> Trot.Router.build_headers_match(nil)
%{}
iex> Trot.Router.build_headers_match(["X-Tasty-Header": "bacon"])
%{"x-tasty-header" => "bacon"}
Link to this function build_headers_match(matcher, headers) View Source

Same as build_headers_match/1 but with an existing dictionary of match options passed in.

Builds the pattern that will be used to match against the request’s host.

Examples

iex> Trot.Router.build_host_match(nil)
%{}
iex> Trot.Router.build_host_match("foo.com")
%{host: "foo.com"}
Link to this function build_host_match(matcher, host) View Source

Same as build_host_match/1 but with an existing dictionary of match options passed in.

Link to this macro delete(path, options, contents \\ []) View Source (macro)

Redirect the request to another location.

Link to this function extract_path_and_guards(path) View Source

Extract the path and any guard statements from the path. Guard statements defaults to true.

Link to this macro get(path, options, contents \\ []) View Source (macro)
Link to this macro import_routes(module) View Source (macro)

Sets up routes from other modules my plugging into the match/2 function in the module.

Link to this function make_response(conn, conn) View Source

Encodes HTTP responses as appropriate and passes them to Plug.Conn.

Examples

# Sets status code to 200 with an empty body
get "/" do
  200
end

# Returns an empty body with a status code of 400
get "/bad" do
  :bad_request
end

# Sets the status code to 200 with a text body
get "/text" do
  "Thank you for your question."
end

# Sets the status code to 201 with a text body
get "/text/body" do
  {201, "Thank you for your question."}
end

# Sets status code to 200 with a JSON-encoded body
get "/json" do
  %{"hyper" => "social"}
end

# Sets the status code to 201 with a JSON-encoded body
get "/json/code" do
  {201, %{"hyper" => "social"}}
end

# Set the response manually as when using Plug directly
get "/conn" do
  send_resp(conn, 200, "optimal tip-to-tip efficiency")
end

# Pattern match part of the path into a variable
get "/presenter/:name" do
  "The presenter is #{name}"
end

# Redirect the incoming request
get "/redirect" do
  {:redirect, "/text/body"}
end
Link to this macro options(path, options, contents \\ []) View Source (macro)
Link to this macro patch(path, options, contents \\ []) View Source (macro)
Link to this macro post(path, options, contents \\ []) View Source (macro)
Link to this macro put(path, options, contents \\ []) View Source (macro)
Link to this macro redirect(from, to) View Source (macro)

Redirects all incoming requests for “from” to “to”. The value of “to” will be put into the Location response header.

Sets up a route for static to path at the @static_root, which defaults to “priv/static”.

Link to this macro static(at, from) View Source (macro)

Sets up a route to static assets. All requests beginning with “at” will look for a matching file under “from”. @static_root will be prepended to “from” and defaults to “priv/static”.

Examples

static "/js", "assets/js"

@static_root "priv/assets/static"
static "/css", "css"