mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
Pedestal-sample
This commit is contained in:
parent
a23cd116b7
commit
07acbb275f
4 changed files with 79 additions and 30 deletions
|
|
@ -1,34 +1,50 @@
|
||||||
(ns example.server
|
(ns example.server
|
||||||
(:require [io.pedestal.http :as http]
|
(:require [io.pedestal.http :as http]
|
||||||
[io.pedestal.http.route :as route]
|
[reitit.pedestal :as pedestal]
|
||||||
[io.pedestal.http.body-params :as body-params]
|
[reitit.http :as reitit-http]
|
||||||
[io.pedestal.http.route.definition :refer [defroutes]]))
|
[reitit.ring :as ring]))
|
||||||
|
|
||||||
(defn hello-world [request]
|
(defn interceptor [x]
|
||||||
(let [name (get-in request [:params :name] "World")]
|
{:enter (fn [ctx] (println ">>" x) ctx)
|
||||||
{:status 200 :body (str "Hello " name "!\n")}))
|
:leave (fn [ctx] (println "<<" x) ctx)})
|
||||||
|
|
||||||
(defroutes routes
|
(def routing-interceptor
|
||||||
[[["/"
|
(pedestal/routing-interceptor
|
||||||
["/hello" {:get hello-world}]]]])
|
(reitit-http/router
|
||||||
|
["/api"
|
||||||
|
{:interceptors [[interceptor :api]
|
||||||
|
[interceptor :apa]]}
|
||||||
|
|
||||||
(def service {:env :prod
|
["/ping"
|
||||||
::http/routes routes
|
{:interceptors [[interceptor :ping]]
|
||||||
::http/resource-path "/public"
|
:get {:interceptors [[interceptor :get]]
|
||||||
::http/type :jetty
|
:handler (fn [_]
|
||||||
::http/port 8080})
|
(println "handler")
|
||||||
|
{:status 200,
|
||||||
|
:body "pong"})}}]]
|
||||||
|
{:data {:interceptors [[interceptor :router]]}})
|
||||||
|
(ring/create-default-handler)
|
||||||
|
{:interceptors [[interceptor :top]]}))
|
||||||
|
|
||||||
|
(defonce server (atom nil))
|
||||||
|
|
||||||
(defn start []
|
(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
|
(merge {:env :dev
|
||||||
::http/join? false
|
::http/join? false
|
||||||
::http/routes #(deref #'routes)
|
|
||||||
::http/allowed-origins {:creds true :allowed-origins (constantly true)}})
|
::http/allowed-origins {:creds true :allowed-origins (constantly true)}})
|
||||||
http/default-interceptors
|
(pedestal/default-interceptors routing-interceptor)
|
||||||
http/dev-interceptors
|
http/dev-interceptors
|
||||||
http/create-server
|
http/create-server
|
||||||
http/start))
|
http/start
|
||||||
|
(->> (reset! server)))
|
||||||
|
(println "server running in port 3000"))
|
||||||
|
|
||||||
|
(start)
|
||||||
(comment
|
|
||||||
(start))
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
(ns example.pedestal
|
(ns reitit.pedestal
|
||||||
(:require [io.pedestal.interceptor.chain :as chain]
|
(:require [io.pedestal.interceptor.chain :as chain]
|
||||||
[io.pedestal.interceptor :as interceptor]
|
[io.pedestal.interceptor :as interceptor]
|
||||||
[reitit.http :as http])
|
[io.pedestal.http :as http]
|
||||||
|
[reitit.interceptor]
|
||||||
|
[reitit.http])
|
||||||
(:import (reitit.interceptor Executor)))
|
(:import (reitit.interceptor Executor)))
|
||||||
|
|
||||||
(def pedestal-executor
|
(def pedestal-executor
|
||||||
|
|
@ -20,16 +22,17 @@
|
||||||
(routing-interceptor router nil))
|
(routing-interceptor router nil))
|
||||||
([router default-handler]
|
([router default-handler]
|
||||||
(routing-interceptor router default-handler nil))
|
(routing-interceptor router default-handler nil))
|
||||||
([router default-handler opts]
|
([router default-handler {:keys [interceptors]}]
|
||||||
(interceptor/interceptor
|
(interceptor/interceptor
|
||||||
(http/routing-interceptor
|
(reitit.http/routing-interceptor
|
||||||
router
|
router
|
||||||
default-handler
|
default-handler
|
||||||
(merge {:executor pedestal-executor} opts)))))
|
{:executor pedestal-executor
|
||||||
|
:interceptors interceptors}))))
|
||||||
|
|
||||||
(def router http/router)
|
(defn default-interceptors [spec router]
|
||||||
|
|
||||||
(defn with-reitit-router [spec router]
|
|
||||||
(-> spec
|
(-> spec
|
||||||
|
(assoc ::http/routes [])
|
||||||
|
(http/default-interceptors)
|
||||||
(update ::http/interceptors (comp vec butlast))
|
(update ::http/interceptors (comp vec butlast))
|
||||||
(update ::http/interceptors conj router)))
|
(update ::http/interceptors conj router)))
|
||||||
|
|
@ -18,7 +18,10 @@
|
||||||
(execute
|
(execute
|
||||||
[this interceptors request]
|
[this interceptors request]
|
||||||
[this interceptors request respond raise]
|
[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]
|
(defn context [request]
|
||||||
(map->Context {:request request}))
|
(map->Context {:request request}))
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,33 @@
|
||||||
(let [opts (meta-merge {:coerce coerce-handler, :compile compile-result} opts)]
|
(let [opts (meta-merge {:coerce coerce-handler, :compile compile-result} opts)]
|
||||||
(r/router data 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
|
(defn ring-handler
|
||||||
"Creates a ring-handler out of a http-router,
|
"Creates a ring-handler out of a http-router,
|
||||||
a default ring-handler and options map, with the following keys:
|
a default ring-handler and options map, with the following keys:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue