Skip to content

Middlewares

LiuTao edited this page Nov 11, 2016 · 4 revisions

Introduction

A middleware is function called before invoking a actual action function. It can modify the HTTP request before passed to the next procedure, and modify the response returned from the next procedure, too. It can also send a response back to client earlier.

Configuration

File config/middlewares.lisp contains the middlewares' functions to be invoked in order. A middleware's function must be exported from its package.

Middleware prototype

Each middleware function's parameter list looks like the following form:

(request next &key config)
  • request is an instance of HTTP request;
  • next is the function for processing request at the next step;
  • config is the configuration of application

Built-in middlewares

eloquent.mvc.middleware:access-log

For every request, collects information from both request and response, and write to log files under directory log/. The structure of one line log message is similar the the access log of nginx.

eloquent.mvc.middleware:compress

For every request, if the value of the header Accept-Encoding includes a sub-string gzip, this function will encode the original response body into bytes in GZIP format, and return to client.

eloquent.mvc.middleware:handle-error

For every request, if there is a condition(exception in other languages) signaled by the next procedures, it will be catched by this middleware and converted to a response of HTTP code 500, and the backtrace of this condition as its HTTP body, send back to client.

eloquent.mvc.middleware:parse-body

For every request, if the value of header Content-Type is recognized, then the body of incomming HTTP body will be parsed by a suitable parser. Now this middleware only support the following Content-Type:

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

eloquent.mvc.middleware:static-file

For every request, if the path of it starts with a prefix specified in the configuration file's static-file section, this middleware will construct a path for file under directory static/, return this pathname if exists, or respond 404 if file doesn't exist

Clone this wiki locally