Skip to content
This repository
Browse code

New middleware clack.middleware.postmodern

clack.middleware.postmodern is a middleware that handles postmodern connections.
  • Loading branch information...
commit db8bee15e2775bb0b19e2ca9ea899e9bcb3f8180 1 parent 9b053f2
Karl Heinrichmeyer h11r authored
16 clack-middleware-postmodern.asd
... ... @@ -0,0 +1,16 @@
  1 +
  2 +(in-package :cl-user)
  3 +(defpackage :clack-middleware-postmodern-asd
  4 + (:use :cl :asdf))
  5 +(in-package :clack-middleware-postmodern-asd)
  6 +
  7 +(defsystem clack-middleware-postmodern
  8 + :version "0.1"
  9 + :author "Karl Heinrichmeyer"
  10 + :license "BSD2"
  11 + :depends-on (:clack
  12 + :cl-syntax
  13 + :cl-syntax-annot
  14 + :postmodern)
  15 + :components ((:file "src/contrib/middleware/postmodern"))
  16 + :description "Middleware for POSTMODERN connection management")
144 src/contrib/middleware/postmodern.lisp
... ... @@ -0,0 +1,144 @@
  1 +
  2 +
  3 +(clack.util:namespace clack.middleware.postmodern
  4 + (:use #:cl
  5 + #:clack)
  6 + (:import-from #:postmodern
  7 + #:with-connection
  8 + #:*database*))
  9 +
  10 +
  11 +(cl-syntax:use-syntax :annot)
  12 +
  13 +@export
  14 +(defclass <clack-middleware-postmodern> (<middleware>)
  15 + ((database :type string
  16 + :initarg :database
  17 + :accessor database
  18 + :documentation "The name of the database to connect to.")
  19 + (user :type string
  20 + :initarg :user
  21 + :accessor user
  22 + :documentation "The name of the database user.")
  23 + (password :type string
  24 + :initarg :password
  25 + :accessor password
  26 + :documentation "The password for the database.")
  27 + (host :type string
  28 + :initarg :host
  29 + :initform "localhost"
  30 + :accessor host
  31 + :documentation "The host the database server is on.")
  32 + (port :type integer
  33 + :initarg :port
  34 + :initform 5432
  35 + :accessor port
  36 + :documentation "The port the database server is listening on.")
  37 + (pooled-p :type boolean
  38 + :initarg :pooled-p
  39 + :initform t
  40 + :accessor pooled-p
  41 + :documentation "Shall the connections be pooled?")
  42 + (use-ssl :type keyword
  43 + :initarg :use-ssl
  44 + :initform :no
  45 + :accessor use-ssl
  46 + :documentation "Keyword for the use-ssl configuration."))
  47 + (:documentation
  48 + "This middleware opens a connection to the specified database
  49 +and stores it in *database* as expected by postmodern. It also
  50 +stores the connection(s) in an alist (database name as key) in
  51 +the environment with the key :clack.postmodern.databases."))
  52 +
  53 +(defmethod call ((this <clack-middleware-postmodern>) env)
  54 + (with-slots (database
  55 + user
  56 + password
  57 + host
  58 + port
  59 + pooled-p
  60 + use-ssl) this
  61 + (with-connection (list database
  62 + user
  63 + password
  64 + host
  65 + :port port
  66 + :pooled-p pooled-p
  67 + :use-ssl use-ssl)
  68 + (let* ((old-dbs (getf env :databases))
  69 + (new-dbs (acons database *database* old-dbs))
  70 + (new-env (copy-list env)))
  71 + (setf (getf new-env :clack.postmodern.databases) new-dbs)
  72 + (call-next this new-env)))))
  73 +
  74 +@export
  75 +(defun get-connection (db-name env)
  76 + (cdr (assoc db-name
  77 + (getf env :clack.postmodern.databases)
  78 + :test #'string=)))
  79 +
  80 +;;; TODO documentation
  81 +(doc:start)
  82 +
  83 +@doc:NAME "
  84 +Clack.Middleware.Postmodern - Middleware for Postmodern connection management
  85 +"
  86 +
  87 +@doc:SYNOPSIS "
  88 + (builder
  89 + (<clack-middleware-postmodern>
  90 + :database \"database-name\"
  91 + :user \"database-user\"
  92 + :password \"database-password\"
  93 + :host \"remote-address\")
  94 + app)
  95 +"
  96 +
  97 +@doc:DESCRIPTION "
  98 +This is a Clack Middleware component for managing Postmodern connections.
  99 +
  100 +The database connection will be bound to postmodern:*database* as expected
  101 +by most of the postmodern functionality. Additionally it can be found
  102 +in the environment under the key :clack.postmodern.databases in form of
  103 +an list that maps the database names to the connections. This allows
  104 +the connection to multiple databases. The connections can be retrieved
  105 +by calling (get-connection db-name env).
  106 +
  107 +## Slots
  108 +
  109 +* database (String)
  110 +
  111 +The name of the database.
  112 +
  113 +* user (String)
  114 +
  115 +The database user name.
  116 +
  117 +* password (String)
  118 +
  119 +The password to the database.
  120 +
  121 +* host (Optional, String)
  122 +
  123 +The address of the database server. The default is \"localhost\"
  124 +
  125 +* port (Optional, Integer)
  126 +
  127 +The port on which the database server is listening.
  128 +
  129 +* pooled-p (Optional, Boolean)
  130 +
  131 +If true, then the connection pool is used. The default is true.
  132 +
  133 +* use-ssl (Optional, Keyword)
  134 +
  135 +Same meaning as in the connection spec to a postmodern database.
  136 +"
  137 +
  138 +@doc:AUTHOR "
  139 +* Karl Heinrichmeyer (karl.heinrichmeyer@gmail.com)
  140 +"
  141 +
  142 +@doc:SEE "
  143 +* [Postmodern](http://marijnhaverbeke.nl/postmodern/)
  144 +"

0 comments on commit db8bee1

Please sign in to comment.
Something went wrong with that request. Please try again.