Aino.Middleware (aino v0.6.0)

Middleware functions for processing a request into a response

Included in Aino are common functions that deal with requests, such as parsing the POST body for form data or parsing query/path params.

Link to this section Summary

Functions

Adjust the request's method based on a special post body parameter

Serve static assets

Common middleware that process low level request data

Processes the Cookie request header

Processes request headers

Stores the request method on the token

Merge params into a single map

Stores the request path on the token on the key :path

Stores query parameters on the token

Processes the request body

Link to this section Functions

Link to this function

adjust_method(token)

Adjust the request's method based on a special post body parameter

Since browsers cannot perform DELETE/PUT/PATCH requests, allow overriding the method based on the _method parameter.

POST body data must be parsed before being able to adjust the method.

iex> token = %{method: :post, parsed_body: %{"_method" => "delete"}}
iex> token = Middleware.adjust_method(token)
iex> token.method
:delete

iex> token = %{method: :post, parsed_body: %{"_method" => "patch"}}
iex> token = Middleware.adjust_method(token)
iex> token.method
:patch

iex> token = %{method: :post, parsed_body: %{"_method" => "put"}}
iex> token = Middleware.adjust_method(token)
iex> token.method
:put

Ignored adjustments

iex> token = %{method: :post, parsed_body: %{"_method" => "new"}}
iex> token = Middleware.adjust_method(token)
iex> token.method
:post

iex> token = %{method: :get}
iex> token = Middleware.adjust_method(token)
iex> token.method
:get

Serve static assets

Loads static files out the priv/static folder for your OTP app. Looks for the path to begin with /assets and everything afterwards is used as a file. If the file exists, it is returned with a 200 status code.

The content type will be guessed at using the MIME hex package.

Example: /assets/js/app.js will look for a file in priv/static/js/app.js

Common middleware that process low level request data

Processes the request:

  • method
  • path
  • headers
  • query parameters
  • parses response body
  • parses cookies

Processes the Cookie request header

Defaults to an empty map if no Cookie header is present.

Stores cookies as a map in the key :cookies

Processes request headers

Downcases all of the headers and stores in the key :headers

Stores the request method on the token

Downcases and converts to an atom on the key :method

iex> request = %Aino.Request{method: :GET}
iex> token = %{request: request}
iex> token = Middleware.method(token)
iex> token.method
:get

Merge params into a single map

Merges in the following order:

  • Path params
  • Query params
  • POST body

Stores the request path on the token on the key :path

iex> request = %Aino.Request{path: "/orders/10"}
iex> token = %{request: request}
iex> token = Middleware.path(token)
iex> token.path
"/orders/10"
Link to this function

query_params(token)

Stores query parameters on the token

Converts map and stores on the key :query_params

iex> request = %Aino.Request{query_params: %{"key[]" => "value"}}
iex> token = %{request: request}
iex> token = Middleware.query_params(token)
iex> token.query_params
%{"key" => ["value"]}
Link to this function

request_body(token)

Processes the request body

Only if the request should have a body (e.g. POST requests)

Handles the following content types:

  • application/x-www-form-urlencoded
  • application/json