Created
December 17, 2014 22:32
-
-
Save jimbru/ddffd96f6cb0a35248b7 to your computer and use it in GitHub Desktop.
Clojure middleware application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns middleware | |
"Test middleware ordering." | |
(:gen-class)) | |
(defn routes | |
[req] | |
(println "served request!" req)) | |
(defn wrap1 | |
[handler] | |
(println "wrap1: applied") | |
(fn [req] | |
(println "wrap1: before") | |
(handler req) | |
(println "wrap1: after"))) | |
(defn wrap2 | |
[handler] | |
(println "wrap2: applied") | |
(fn [req] | |
(println "wrap2: before") | |
(handler req) | |
(println "wrap2: after"))) | |
(defn wrap3 | |
[handler] | |
(println "wrap3: applied") | |
(fn [req] | |
(println "wrap3: before") | |
(handler req) | |
(println "wrap3: after"))) | |
(def middleware [wrap1 wrap2 wrap3]) | |
(defn app | |
[] | |
(reduce #(%2 %1) routes middleware)) | |
(defn -main | |
[& args] | |
((app) args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pegasus:playground jim$ lein repl | |
nREPL server started on port 52885 on host 127.0.0.1 - nrepl://127.0.0.1:52885 | |
REPL-y 0.3.5, nREPL 0.2.6 | |
Clojure 1.6.0 | |
Java HotSpot(TM) 64-Bit Server VM 1.8.0_25-b17 | |
Docs: (doc function-name-here) | |
(find-doc "part-of-name-here") | |
Source: (source function-name-here) | |
Javadoc: (javadoc java-object-or-class-here) | |
Exit: Control+D or (exit) or (quit) | |
Results: Stored in vars *1, *2, *3, an exception in *e | |
user=> (require 'middleware) | |
nil | |
user=> (middleware/-main :foobar) | |
wrap1: applied | |
wrap2: applied | |
wrap3: applied | |
wrap3: before | |
wrap2: before | |
wrap1: before | |
served request! (:foobar) | |
wrap1: after | |
wrap2: after | |
wrap3: after | |
nil | |
user=> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment