reitit/examples/pedestal/src/example/server.clj

51 lines
1.4 KiB
Clojure
Raw Normal View History

2018-08-31 07:01:01 +00:00
(ns example.server
(:require [io.pedestal.http :as http]
2018-08-31 07:45:51 +00:00
[reitit.pedestal :as pedestal]
[reitit.http :as reitit-http]
[reitit.ring :as ring]))
2018-08-31 07:01:01 +00:00
2018-08-31 07:45:51 +00:00
(defn interceptor [x]
{:enter (fn [ctx] (println ">>" x) ctx)
:leave (fn [ctx] (println "<<" x) ctx)})
2018-08-31 07:01:01 +00:00
2018-08-31 07:45:51 +00:00
(def routing-interceptor
(pedestal/routing-interceptor
(reitit-http/router
["/api"
{:interceptors [[interceptor :api]
[interceptor :apa]]}
2018-08-31 07:01:01 +00:00
2018-08-31 07:45:51 +00:00
["/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))
2018-08-31 07:01:01 +00:00
(defn start []
2018-08-31 07:45:51 +00:00
(when @server
(http/stop @server)
(println "server stopped"))
(-> {:env :prod
::http/routes []
::http/resource-path "/public"
::http/type :jetty
::http/port 3000}
2018-08-31 07:01:01 +00:00
(merge {:env :dev
::http/join? false
::http/allowed-origins {:creds true :allowed-origins (constantly true)}})
2018-08-31 07:45:51 +00:00
(pedestal/default-interceptors routing-interceptor)
2018-08-31 07:01:01 +00:00
http/dev-interceptors
http/create-server
2018-08-31 07:45:51 +00:00
http/start
(->> (reset! server)))
(println "server running in port 3000"))
2018-08-31 07:01:01 +00:00
2018-08-31 07:45:51 +00:00
(start)