Skip to content

Commit

Permalink
Merge 2c1f536 into b962435
Browse files Browse the repository at this point in the history
  • Loading branch information
abhicit0802 committed Jun 16, 2020
2 parents b962435 + 2c1f536 commit 10816ad
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
8 changes: 7 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@
[ring/ring-json "0.4.0"]
[ring-logger "0.7.7"]
[tech.gojek/sentry-clj.async "1.0.0" :exclusions [org.clojure/clojure]]
[yleisradio/new-reliquary "1.1.0" :exclusions [org.clojure/clojure]]]
[yleisradio/new-reliquary "1.1.0" :exclusions [org.clojure/clojure]]
[metosin/ring-swagger "0.26.2"
:exclusions [cheshire
com.fasterxml.jackson.core/jackson-core
com.fasterxml.jackson.dataformat/jackson-dataformat-smile
com.fasterxml.jackson.dataformat/jackson-dataformat-cbor]]
[metosin/ring-swagger-ui "3.25.3"]]
:deploy-repositories [["clojars" {:url "https://clojars.org/repo"
:username :env/clojars_username
:password :env/clojars_password
Expand Down
3 changes: 2 additions & 1 deletion resources/config.test.ci.edn
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
:retry {:type [:linear :keyword]
:count [5 :int]
:enabled [true :bool]}
:http-server {:port [8010 :int]
:http-server {:middlewares {:swagger {:enabled false}}
:port [8010 :int]
:thread-count [100 :int]}
:stream-router {:default {:application-id "test"
:bootstrap-servers "localhost:9092"
Expand Down
3 changes: 2 additions & 1 deletion resources/config.test.edn
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
:retry {:count [5 :int]
:type [:linear :keyword]
:enabled [true :bool]}
:http-server {:port [8010 :int]
:http-server {:middlewares {:swagger {:enabled false}}
:port [8010 :int]
:thread-count [100 :int]}
:stream-router {:default {:application-id "test"
:bootstrap-servers "localhost:9092"
Expand Down
3 changes: 2 additions & 1 deletion src/ziggurat/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
:exchange-name "%s_dead_letter_exchange"}}
:retry {:count 5
:enabled false}
:http-server {:port 8080
:http-server {:middlewares {:swagger {:enabled false}}
:port 8080
:thread-count 100}}})

(defn- interpolate-val [val app-name]
Expand Down
12 changes: 11 additions & 1 deletion src/ziggurat/server/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
[sentry-clj.async :as sentry]
[ziggurat.sentry :refer [sentry-reporter]]
[ziggurat.util.map :as umap]
[ziggurat.metrics :as metrics]))
[ziggurat.metrics :as metrics]
[ring.swagger.swagger-ui :as rsui]
[ziggurat.config :refer [get-in-config]]))

(defn wrap-default-content-type-json [handler]
(fn [request]
Expand Down Expand Up @@ -40,3 +42,11 @@
response-status (:status response)]
(metrics/increment-count ["http-server" "requests-served"] "count" {:request-uri request-uri :response-status (str response-status)})
response)))

(defn- swagger-enabled? []
(get-in-config [:http-server :middlewares :swagger :enabled]))

(defn enable-swagger [handler]
(if (swagger-enabled?)
(rsui/wrap-swagger-ui handler {:path "/swagger-ui"})
handler))
1 change: 1 addition & 0 deletions src/ziggurat/server/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
(conj (vec (concat actor-routes (get-routes))))
(bidi/make-handler)
(m/wrap-hyphenate)
(m/enable-swagger)
(ring-defaults/wrap-defaults ring-defaults/api-defaults)
(wrap-json-params)
(wrap-multipart-params)
Expand Down
24 changes: 22 additions & 2 deletions test/ziggurat/server/middleware_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns ziggurat.server.middleware-test
(:require [clojure.test :refer :all])
(:require [ziggurat.server.middleware :refer [wrap-with-metrics]]
[ziggurat.metrics :as metrics]))
(:require [ziggurat.server.middleware :refer [wrap-with-metrics enable-swagger]]
[ziggurat.config :refer [ziggurat-config]]
[ziggurat.metrics :as metrics]
[ring.swagger.swagger-ui :as rsui]))

(deftest wrap-with-metrics-test
(testing "should publish http-server metrics"
Expand All @@ -21,3 +23,21 @@
(swap! increment-count-called not)))]
((wrap-with-metrics handler) request)
(is (= @increment-count-called true))))))

(deftest enable-swagger-test
(testing "when swagger config is enabled"
(let [wrap-swagger-ui-call-count (atom 0)
handler (fn [_] {:status 200})
original-zig-config (ziggurat-config)]
(with-redefs [ziggurat-config (fn [] (assoc-in original-zig-config [:http-server :middlewares :swagger :enabled] true))
rsui/wrap-swagger-ui (fn [& _] (swap! wrap-swagger-ui-call-count inc))]
(enable-swagger handler)
(is (= @wrap-swagger-ui-call-count 1)))))
(testing "when swagger config is disabled"
(let [wrap-swagger-ui-call-count (atom 0)
handler (fn [_] {:status 200})
original-zig-config (ziggurat-config)]
(with-redefs [ziggurat-config (fn [] (assoc-in original-zig-config [:http-server :middlewares :swagger :enabled] false))
rsui/wrap-swagger-ui (fn [& _] (swap! wrap-swagger-ui-call-count inc))]
(enable-swagger handler)
(is (= @wrap-swagger-ui-call-count 0))))))

0 comments on commit 10816ad

Please sign in to comment.