Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

(fixes #8) added Clack.Middleware.Conditional.

  • Loading branch information...
commit e97af58b7856bc9e9fa7b9ceef5a03b3016c4981 1 parent 402e6a7
@fukamachi authored
View
1  clack-test.asd
@@ -38,6 +38,7 @@
(:file "app/file")
(:file "app/urlmap")
(:file "middleware/static")
+ (:file "middleware/conditional")
(:file "middleware/session")
(:file "middleware/logger")))
(:module "util"
View
3  clack.asd
@@ -54,7 +54,7 @@
"handler"
"middleware/logger"))
(:file "builder"
- :depends-on ("component" "middleware"))
+ :depends-on ("component" "middleware" "mw"))
(:file "request")
(:file "response")
(:file "component")
@@ -88,6 +88,7 @@
:depends-on ("clack" "response" "request" "app")
:components
((:file "static")
+ (:file "conditional")
(:module "session"
:serial t
:components
View
10 src/core/builder.lisp
@@ -13,7 +13,9 @@
(:import-from :clack.component
:call)
(:import-from :clack.middleware
- :wrap))
+ :wrap)
+ (:import-from :clack.middleware.conditional
+ :<clack-middleware-conditional>))
(cl-annot:enable-annot-syntax)
@@ -28,7 +30,11 @@ This is useful in development phase.")
`(reduce #'wrap
(list ,@(loop for item in (butlast app-or-middleware)
for (class . args) = (ensure-list item)
- collect `(make-instance ',class ,@args)))
+ if (eq class :condition)
+ collect `(make-instance '<clack-middleware-conditional>
+ :condition ,@args)
+ else
+ collect `(make-instance ',class ,@args)))
:initial-value ,(car (last app-or-middleware))
:from-end t))
View
78 src/core/middleware/conditional.lisp
@@ -0,0 +1,78 @@
+#|
+ This file is a part of Clack package.
+ URL: http://github.com/fukamachi/clack
+ Copyright (c) 2011 Eitarow Fukamachi <e.arrows@gmail.com>
+
+ Clack is freely distributable under the LLGPL License.
+|#
+
+(clack.util:namespace clack.middleware.conditional
+ (:use :cl
+ :clack))
+
+(cl-annot:enable-annot-syntax)
+
+@export
+(defclass <clack-middleware-conditional> (<middleware>)
+ ((condition :type (or function <component>)
+ :initarg :condition
+ :accessor condition)
+ (builder :type (or function <component> symbol list)
+ :initarg :builder
+ :accessor builder)
+ (middleware :type (or function <component>)
+ :accessor middleware)))
+
+(defmethod initialize-instance :after ((this <clack-middleware-conditional>) &key)
+ (setf (builder this)
+ (typecase (builder this)
+ (symbol (make-instance (builder this)))
+ (list (apply #'make-instance (builder this)))
+ (t (builder this)))))
+
+(defmethod wrap ((this <clack-middleware-conditional>) app)
+ (setf (middleware this)
+ (wrap (builder this) app))
+ (call-next-method))
+
+(defmethod call ((this <clack-middleware-conditional>) env)
+ (if (call (condition this) env)
+ (call (middleware this) env)
+ (call-next this env)))
+
+(doc:start)
+
+@doc:NAME "
+Clack.Middleware.Conditional - Conditional wrapper for Clack middleware.
+"
+
+@doc:SYNOPSIS "
+ (builder
+ (:condition (lambda (env)
+ (scan \"WebKit\" (getf env :http-user-agent)))
+ :builder (builder
+ <clack-middleware-something>
+ app)))
+
+ (builder
+ (<clack-middleware-conditional>
+ :condition (lambda (env)
+ (scan \"WebKit\" (getf env :http-user-agent)))
+ :builder '(<clack-middleware-static>
+ :path \"/public/\"
+ :root #p\"/static-files/\")
+ app))
+
+ (wrap
+ (make-instance '<clack-middleware-conditional>
+ :condition (lambda (env)
+ (scan \"WebKit\" (getf env :http-user-agent)))
+ :builder '(<clack-middleware-something>
+ :path \"/public/\"
+ :root #p\"/static-files/\"))
+ app)
+"
+
+@doc:AUTHOR "
+* Eitarow Fukamachi (e.arrows@gmail.com)
+"
View
55 t/core/middleware/conditional.lisp
@@ -0,0 +1,55 @@
+(clack.util:namespace clack-test.middleware.conditional
+ (:use :cl
+ :clack
+ :clack.builder
+ :cl-test-more
+ :clack.middleware.conditional
+ :clack.middleware.static
+ :cl-ppcre))
+
+(plan 4)
+
+(defvar *app* (lambda (env)
+ (declare (ignore env))
+ '(200 nil ("Hello, Clack"))))
+
+(is-type (builder
+ (<clack-middleware-conditional>
+ :condition (lambda (env)
+ (scan "WebKit" (getf env :http-user-agent)))
+ :builder '(<clack-middleware-static>
+ :path "/public/"
+ :root #p"/static-files/"))
+ *app*)
+ 'function)
+
+(is-type (wrap
+ (make-instance '<clack-middleware-conditional>
+ :condition (lambda (env)
+ (scan "WebKit" (getf env :http-user-agent)))
+ :builder '(<clack-middleware-static>
+ :path "/public/"
+ :root #p"/static-files/"))
+ *app*)
+ 'function)
+
+(defclass <clack-middleware-conditional-test> (<middleware>) ())
+(defmethod call ((this <clack-middleware-conditional-test>) env)
+ (declare (ignore env))
+ '(200 nil ("Hello from Conditional Middleware")))
+
+(defvar *built-app*
+ (builder
+ (<clack-middleware-conditional>
+ :condition (lambda (env)
+ (scan "WebKit" (getf env :http-user-agent)))
+ :builder '<clack-middleware-conditional-test>)
+ *app*))
+
+(is (call *built-app* '(:http-user-agent "Firefox"))
+ '(200 nil ("Hello, Clack")))
+
+(is (call *built-app* '(:http-user-agent "WebKit"))
+ '(200 nil ("Hello from Conditional Middleware")))
+
+(finalize)
Please sign in to comment.
Something went wrong with that request. Please try again.