2018-08-31 07:01:01 +00:00
|
|
|
(ns example.server
|
2018-08-31 07:56:56 +00:00
|
|
|
(:require [io.pedestal.http]
|
|
|
|
|
[clojure.core.async :as a]
|
2018-08-31 07:45:51 +00:00
|
|
|
[reitit.pedestal :as pedestal]
|
2018-08-31 07:56:56 +00:00
|
|
|
[reitit.http :as http]
|
2018-08-31 07:45:51 +00:00
|
|
|
[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
|
2018-08-31 07:56:56 +00:00
|
|
|
(http/router
|
2018-08-31 07:45:51 +00:00
|
|
|
["/api"
|
|
|
|
|
{:interceptors [[interceptor :api]
|
|
|
|
|
[interceptor :apa]]}
|
2018-08-31 07:01:01 +00:00
|
|
|
|
2018-08-31 07:56:56 +00:00
|
|
|
["/sync"
|
|
|
|
|
{:interceptors [[interceptor :sync]]
|
2018-08-31 07:45:51 +00:00
|
|
|
:get {:interceptors [[interceptor :get]]
|
|
|
|
|
:handler (fn [_]
|
|
|
|
|
(println "handler")
|
|
|
|
|
{:status 200,
|
2018-08-31 07:56:56 +00:00
|
|
|
:body "pong"})}}]
|
|
|
|
|
|
|
|
|
|
["/async"
|
|
|
|
|
{:interceptors [[interceptor :async]]
|
|
|
|
|
:get {:interceptors [[interceptor :get]]
|
|
|
|
|
:handler (fn [_]
|
|
|
|
|
(a/go
|
|
|
|
|
(println "handler")
|
|
|
|
|
{:status 200,
|
|
|
|
|
:body "pong"}))}}]]
|
2018-08-31 07:45:51 +00:00
|
|
|
{: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
|
2018-08-31 07:56:56 +00:00
|
|
|
(io.pedestal.http/stop @server)
|
2018-08-31 07:45:51 +00:00
|
|
|
(println "server stopped"))
|
|
|
|
|
(-> {:env :prod
|
2018-08-31 07:56:56 +00:00
|
|
|
:io.pedestal.http/routes []
|
|
|
|
|
:io.pedestal.http/resource-path "/public"
|
|
|
|
|
:io.pedestal.http/type :jetty
|
|
|
|
|
:io.pedestal.http/port 3000}
|
2018-08-31 07:01:01 +00:00
|
|
|
(merge {:env :dev
|
2018-08-31 07:56:56 +00:00
|
|
|
:io.pedestal.http/join? false
|
|
|
|
|
:io.pedestal.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:56:56 +00:00
|
|
|
io.pedestal.http/dev-interceptors
|
|
|
|
|
io.pedestal.http/create-server
|
|
|
|
|
io.pedestal.http/start
|
2018-08-31 07:45:51 +00:00
|
|
|
(->> (reset! server)))
|
|
|
|
|
(println "server running in port 3000"))
|
2018-08-31 07:01:01 +00:00
|
|
|
|
2018-08-31 07:56:56 +00:00
|
|
|
(comment
|
|
|
|
|
(start))
|