This commit is contained in:
Tommi Reiman 2018-08-31 10:01:01 +03:00
parent 9fc8710cad
commit c3a3b923a7
5 changed files with 104 additions and 0 deletions

11
examples/pedestal/.gitignore vendored Normal file
View file

@ -0,0 +1,11 @@
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.hgignore
.hg/

View file

@ -0,0 +1,17 @@
# reitit-http example with pedestal
## Usage
```clj
> lein repl
(start)
```
Go with browser to:
* http://localhost:3000/api/sync - synchronous
* http://localhost:3000/api/async - with core.async
## License
Copyright © 2018 Metosin Oy

View file

@ -0,0 +1,7 @@
(defproject ring-example "0.1.0-SNAPSHOT"
:description "Reitit-http with pedestal"
:dependencies [[org.clojure/clojure "1.9.0"]
[io.pedestal/pedestal.service "0.5.1"]
[io.pedestal/pedestal.jetty "0.5.1"]
[metosin/reitit "0.2.0-SNAPSHOT"]]
:repl-options {:init-ns example.server})

View file

@ -0,0 +1,35 @@
(ns example.pedestal
(:require [io.pedestal.interceptor.chain :as chain]
[io.pedestal.interceptor :as interceptor]
[reitit.http :as http])
(:import (reitit.interceptor Executor)))
(def pedestal-executor
(reify
Executor
(queue [_ interceptors]
(->> interceptors
(map (fn [{:keys [::interceptor/handler] :as interceptor}]
(or handler interceptor)))
(map interceptor/interceptor)))
(enqueue [_ context interceptors]
(chain/enqueue context interceptors))))
(defn routing-interceptor
([router]
(routing-interceptor router nil))
([router default-handler]
(routing-interceptor router default-handler nil))
([router default-handler opts]
(interceptor/interceptor
(http/routing-interceptor
router
default-handler
(merge {:executor pedestal-executor} opts)))))
(def router http/router)
(defn with-reitit-router [spec router]
(-> spec
(update ::http/interceptors (comp vec butlast))
(update ::http/interceptors conj router)))

View file

@ -0,0 +1,34 @@
(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]]))
(defn hello-world [request]
(let [name (get-in request [:params :name] "World")]
{:status 200 :body (str "Hello " name "!\n")}))
(defroutes routes
[[["/"
["/hello" {:get hello-world}]]]])
(def service {:env :prod
::http/routes routes
::http/resource-path "/public"
::http/type :jetty
::http/port 8080})
(defn start []
(-> service/service
(merge {:env :dev
::http/join? false
::http/routes #(deref #'routes)
::http/allowed-origins {:creds true :allowed-origins (constantly true)}})
http/default-interceptors
http/dev-interceptors
http/create-server
http/start))
(comment
(start))