Create a gist now

Instantly share code, notes, and snippets.

ringアプリケーションをcomponent化
(ns example-component-ring.component
(:require [com.stuartsierra.component :as component]
[example-component-ring.handler :refer [app]]
[ring.adapter.jetty :refer [run-jetty]]))
;; HTTPサーバコンポーネント
(defrecord HttpServer [port server]
component/Lifecycle
(start [this]
(if server
this
(do (println ";; Starting HTTP server")
(let [server (run-jetty app
{:port port
:join? false})]
(assoc this :server server)))))
(stop [this]
(if (not server)
this
(do (try (.stop server)
(catch Throwable t
(print ";; Error when stopping HTTP server")))
(println ";; HTTP server stopped")
(assoc this :server nil)))))
;; HTTPサーバコンポーネントの作成用関数
(defn create-http-server [port]
(map->HttpServer {:port port}))
;; システム作成用関数
(defn create-system [config-options]
(let [{:keys [port]} config-options]
(component/system-map
:http-server (create-http-server port))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment