Skip to content

Instantly share code, notes, and snippets.

@theikkila
Created October 24, 2016 12:12
Show Gist options
  • Save theikkila/e2aaa641a1052a0e9110bb1074a1c76c to your computer and use it in GitHub Desktop.
Save theikkila/e2aaa641a1052a0e9110bb1074a1c76c to your computer and use it in GitHub Desktop.
(defn auth-middleware [req]
(let [auth-headers (get-in req [:headers :auth])]
(assoc req :user
(if (= auth-headers "secret")
{:name "Secret User"}
nil))))
(defn random-middleware [req]
(assoc req :random "LOL"))
(defn controller [req]
(let [q (get-in req [:query-params :q])
user (get-in req [:user :name])
hello (str "Hello! " user " your name should be " user)]
{:status 201
:headers {:content-type "application/json"}
:body {:query hello :random (:random req)}}))
(def example-request {:method "POST"
:path "/do/something"
:query-params {:q "Teemu"}
:headers {:auth "secret"}
:body ""})
(-> example-request
(auth-middleware)
(random-middleware)
(controller))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment