Previous: , Up: Basic concepts  

2.3 How to pass some additional request context?

Naturally, to make use from some kind of middlewares, some additional context needs to be attached to the request. E.g. the authentication middleware needs a way to provide the information about the user, which could be needed by the router or some renderer middleware.

Guile has a beautiful built-it utility for that: called parameters. The middleware package, along with middleware procedures, defines some parameters, which add the needed context for request processing. Here is a prototype, to illustrate the idea:

(define request-context:authenticated-user #f)

(define (authenticate-user-sample request body cont)
  ;; authenticate user
  ;; and set the request context parameter
  (if (not authenticated-user)
    (respond-401)
    (parameterize ((request-context:authenticated-user authenticated-user))
      (cont request body))))

Middleware implementations note This package suggests a simple convention to follow: not introduce some request-wrapper type to contain all context to be passed as a first argument, but parameterize every additional piece of context data and pass just a native request and body through the middleware chain. For those context parameter names, prepend a request-context: prefix and export them for the consumer code. This would allow a very easy way to independently implement middleware procedures by different packages.


Previous: , Up: Basic concepts