swagger1st
swagger1st庫提供了符合Ring規(guī)范的handler,能夠根據(jù)Swagger定義解析、驗證、路由HTTP請求。
使用swagger1st,不是在代碼中定義API,而是利用Swagger官方的編輯器根據(jù)Swagger 2.0規(guī)范定義API。這樣產(chǎn)生了一種API優(yōu)先的開發(fā)流程,并且是使用獨(dú)立于任何開發(fā)語言的方式定義API。
friboo
friboo提供了一些component,用于輔助Clojure的微服務(wù)應(yīng)用開發(fā)。friboo鼓勵采用swagger1st實行API優(yōu)先的開發(fā)方式。
HTTP component
使用def-http-component宏創(chuàng)建HTTP component。http組件啟動時,內(nèi)部調(diào)用swagger1st創(chuàng)建handler,并使用jetty適配器運(yùn)行之。
(ns myapi
(:require [org.zalando.stups.friboo.system.http :refer [def-http-component]))
(def-http-component MyAPI "my-api.yaml" [db scheduler])
(defn my-api-function [parameters request db scheduler]
; 這里能夠使用依賴的db和scheduler組件
)
上面的代碼創(chuàng)建了MyAPI組件,依賴于db和scheduler組件。my-api-function函數(shù)的第一個參數(shù)是一個扁平的map,對應(yīng)的Swagger規(guī)范的parameters。
http組件使用:configuration存放配置項,在啟動組件時用到它,例如
(map->MyAPI {:configuration {:port 8080
:cors-origin "*.zalando.de"}})
推薦采用下面的方式創(chuàng)建一個http系統(tǒng)。
(ns my-app.core
(:require [org.zalando.stups.friboo.config :as config]
[org.zalando.stups.friboo.system :as system]))
(defn run
[default-configuration]
(let [configuration (config/load-configuration
(system/default-http-namespaces-and :db)
[my-app.sql/default-db-configuration
my-app.api/default-http-configuration
default-configuration])
system (system/http-system-map configuration
my-app.api/map->API [:db]
:db (my-app.sql/map->DB {:configuration (:db configuration)}))]
(system/run configuration system)))
DB component
使用def-db-component宏創(chuàng)建。DB組件本質(zhì)上是一個兼容db-spec
的數(shù)據(jù)結(jié)構(gòu),使用:datasource存放數(shù)據(jù)源。
(ns mydb
(:require [org.zalando.stups.friboo.system.db :refer [def-db-component]))
(def-db-component MyDB)
與http組件類似,db組件使用:configuration存放配置項,在啟動組件時用到。
(map->MyDB {:configuration {:subprotocol "postgresql"
:subname "localhost/mydb"}})