Pedestal-sample

This commit is contained in:
Tommi Reiman 2018-08-31 10:45:51 +03:00
parent a23cd116b7
commit 07acbb275f
4 changed files with 79 additions and 30 deletions

View file

@ -1,34 +1,50 @@
(ns example.server
(:require [io.pedestal.http :as http]
[io.pedestal.http.route :as route]
[io.pedestal.http.body-params :as body-params]
[io.pedestal.http.route.definition :refer [defroutes]]))
[reitit.pedestal :as pedestal]
[reitit.http :as reitit-http]
[reitit.ring :as ring]))
(defn hello-world [request]
(let [name (get-in request [:params :name] "World")]
{:status 200 :body (str "Hello " name "!\n")}))
(defn interceptor [x]
{:enter (fn [ctx] (println ">>" x) ctx)
:leave (fn [ctx] (println "<<" x) ctx)})
(defroutes routes
[[["/"
["/hello" {:get hello-world}]]]])
(def routing-interceptor
(pedestal/routing-interceptor
(reitit-http/router
["/api"
{:interceptors [[interceptor :api]
[interceptor :apa]]}
(def service {:env :prod
::http/routes routes
::http/resource-path "/public"
::http/type :jetty
::http/port 8080})
["/ping"
{:interceptors [[interceptor :ping]]
:get {:interceptors [[interceptor :get]]
:handler (fn [_]
(println "handler")
{:status 200,
:body "pong"})}}]]
{:data {:interceptors [[interceptor :router]]}})
(ring/create-default-handler)
{:interceptors [[interceptor :top]]}))
(defonce server (atom nil))
(defn start []
(-> service/service
(when @server
(http/stop @server)
(println "server stopped"))
(-> {:env :prod
::http/routes []
::http/resource-path "/public"
::http/type :jetty
::http/port 3000}
(merge {:env :dev
::http/join? false
::http/routes #(deref #'routes)
::http/allowed-origins {:creds true :allowed-origins (constantly true)}})
http/default-interceptors
(pedestal/default-interceptors routing-interceptor)
http/dev-interceptors
http/create-server
http/start))
http/start
(->> (reset! server)))
(println "server running in port 3000"))
(comment
(start))
(start)

View file

@ -1,7 +1,9 @@
(ns example.pedestal
(ns reitit.pedestal
(:require [io.pedestal.interceptor.chain :as chain]
[io.pedestal.interceptor :as interceptor]
[reitit.http :as http])
[io.pedestal.http :as http]
[reitit.interceptor]
[reitit.http])
(:import (reitit.interceptor Executor)))
(def pedestal-executor
@ -20,16 +22,17 @@
(routing-interceptor router nil))
([router default-handler]
(routing-interceptor router default-handler nil))
([router default-handler opts]
([router default-handler {:keys [interceptors]}]
(interceptor/interceptor
(http/routing-interceptor
(reitit.http/routing-interceptor
router
default-handler
(merge {:executor pedestal-executor} opts)))))
{:executor pedestal-executor
:interceptors interceptors}))))
(def router http/router)
(defn with-reitit-router [spec router]
(defn default-interceptors [spec router]
(-> spec
(assoc ::http/routes [])
(http/default-interceptors)
(update ::http/interceptors (comp vec butlast))
(update ::http/interceptors conj router)))

View file

@ -18,7 +18,10 @@
(execute
[this interceptors request]
[this interceptors request respond raise]
"executes the interceptor chain"))
"executes the interceptor chain with a request")
(enqueue
[this context interceptors]
"enqueues the interceptors into the queue"))
(defn context [request]
(map->Context {:request request}))

View file

@ -61,6 +61,33 @@
(let [opts (meta-merge {:coerce coerce-handler, :compile compile-result} opts)]
(r/router data opts))))
(defn routing-interceptor
"A Pedestal-style routing interceptor that enqueus the interceptors into context."
[router default-handler {:keys [interceptors executor]}]
(let [default-handler (or default-handler (fn ([_])))
default-interceptors (->> interceptors
(map #(interceptor/into-interceptor % nil (r/options router))))
default-queue (interceptor/queue executor default-interceptors)]
{:name ::router
:enter (fn [{:keys [request] :as context}]
(if-let [match (r/match-by-path router (:uri request))]
(let [method (:request-method request)
path-params (:path-params match)
endpoint (-> match :result method)
interceptors (or (:queue endpoint) (:interceptors endpoint))
request (-> request
(impl/fast-assoc :path-params path-params)
(impl/fast-assoc ::r/match match)
(impl/fast-assoc ::r/router router))
context (assoc context :request request)
queue (interceptor/queue executor (concat default-interceptors interceptors))]
(interceptor/enqueue executor context queue))
(interceptor/enqueue executor context default-queue)))
:leave (fn [context]
(if-not (:response context)
(assoc context :response (default-handler (:request context)))
context))}))
(defn ring-handler
"Creates a ring-handler out of a http-router,
a default ring-handler and options map, with the following keys: