http://pedestal.io/guides/pedestal-with-component
lein new pedestal-service tnbsst
all pedestal deps already scaffoled, using jetty for this demo
[com.stuartsierra/component "0.4.0"]
- implementing at the root src level
(ns pedestal
(:require [com.stuartsierra.component :as component]
[io.pedestal.http :as http]))
It’s idiomatic in Pedestal services to include an :env key in the service map to communicate the environment of the service. This currently does not affect the behavior of the service but it’s a useful marker. Our component will leverage this idiom and will not start/stop the server if the service is configured for the test environment. The implementation is included below.
(defn test? [service-map]
(= :test (:env service-map)))
controls start and stop lifecycle of our service
(defrecord Pedestal [service-map service]
component/Lifecycle
(start [this]
(if service
this
(cond-> service-map
true http/create-server
(not (test? service-map)) http/start
true ((partial assoc this :service)))))
(stop [this]
(when (and service (not (test? service-map)))
(http/stop service))
(assoc this :service nil)))
(defn new-pedestal []
(map->Pedestal {}))
useful in this case for our REPL work
- again implement at the root level src tree
(ns system
(:require [com.stuartsierra.component :as component]
[io.pedestal.http :as http]
[pedestal]
[tnbsst.service :as service]))
(defn system [env]
(component/system-map
:service-map {:env env
::http/routes service/routes
::http/type :jetty
::http/port 8890
::http/join? false}
:pedestal (component/using (pedestal/new-pedestal) [:service-map])))
- cider-jack-in
- lein repl
- etc
(require 'system)
(in-ns 'system)
(def ssss (system {}))
(alter-var-root #'ssss component/start)
load `http://localhost:8890` … it works!
(alter-var-root #'ssss component/stop)