This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-08
Channels
- # aleph (5)
- # announcements (3)
- # aws-lambda (24)
- # babashka (17)
- # beginners (59)
- # calva (168)
- # clerk (4)
- # clj-kondo (62)
- # clojure (77)
- # clojure-belgium (4)
- # clojure-brasil (10)
- # clojure-ecuador (3)
- # clojure-europe (41)
- # clojure-losangeles (2)
- # clojure-nl (2)
- # clojure-norway (24)
- # clojure-uk (2)
- # clojurescript (44)
- # clr (21)
- # community-development (7)
- # conjure (1)
- # cursive (6)
- # datalevin (15)
- # datomic (1)
- # deps-new (12)
- # emacs (45)
- # events (1)
- # fulcro (8)
- # funcool (7)
- # graphql (5)
- # hugsql (15)
- # jobs (2)
- # matcher-combinators (17)
- # meander (14)
- # membrane (31)
- # pathom (28)
- # pedestal (8)
- # practicalli (6)
- # re-frame (12)
- # releases (1)
- # remote-jobs (1)
- # shadow-cljs (32)
- # tools-deps (8)
- # vim (16)
Hi! I'm trying to host a swagger-ui on a pedestal route which depends on manually written documentation, i.e. a swagger.json file. I've looked at https://github.com/frankiesardo/route-swagger/blob/8ebab45d03cb43fdec323b955f5ae4c152814d39/src/route_swagger/interceptor.clj and I've gotten it to work by autogenerating the swagger.json file as described in route-swagger. However, I haven't figured out how I can make it depend on a manually written swagger.json file that I've hosted myself on a different endpoint (I've solved the hosting of a manually written swagger.json). I've also looked at using the https://github.com/metosin/ring-swagger-ui directly to wrap the swagger-ui middleware in a Pedestal interceptor, but haven't succeded. This would be my preferred method, if possible. Anyone got any suggestions? And please let me know if something's unclear or there's an even better way! Here's an example of what I would like to achieve in building the Pedestal routes table:
(ns nice.nm
(:require [cheshire.core :as json]
[ring.swagger.swagger-ui :refer :all]
[ring.swagger.swagger-ui/swagger-ui]))
(def swagger-json
{:name ::swagger-json-interceptor
:enter (fn [context]
(assoc context :response {:status 200
:headers {"Content-Type" "application/json; charset=utf-8"}
:body (cheshire.core/parse-string (slurp "<path-to-dir>/swagger.json"))}))})
(def swagger-ui
{:name ::swagger-ui-interceptor
:enter (fn [context]
(ring.swagger.swagger-ui/swagger-ui {:path "/test"
:swagger-docs "/applications/api/v1/swagger-json-static"}))})
(defn routes
[]
#{["/endpoint1"
:post [interceptor1
endpoint1-handler]
:route-name :endpoint1]
["/endpoint2"
:get [endpoint2]
:route-name :endpoint2]
["/manually-written-swagger.json"
:get [swagger-json]
:route-name :swagger-json]
["/swagger-ui"
:get [swagger-ui]
:route-name :swagger-ui]})
@U04GATKRD35, even I am trying to do it now, one thing is swagger-json
is not creating an interceptor, it supposed to bound between (interceptor/interceptor
right ?
You're right!
In the end I actually just used the Swagger-UI as a static resource by placing the content of the Swagger-UI /dist
folder in the "resources/public" directory in my project, as explained in the https://github.com/swagger-api/swagger-ui#general (last line in General section: "If you are looking for plain ol' HTML/JS/CSS, https://github.com/swagger-api/swagger-ui/releases/latest and copy the contents of the /dist
folder to your server."). You can read about static routes in Pedestal http://pedestal.io/cookbook/index#_how_to_serve_static_resources.
Then I just changed the config of Swagger-UI in https://github.com/swagger-api/swagger-ui/blob/master/dist/swagger-initializer.js#L5. Info on the config can be found https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/.
Thanks for the instructions @U04GATKRD35, I went down this route as well. Adding one more change I needed to make to get everything working: I added
::http/secure-headers
{:content-security-policy-settings "object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https: http:;"}
to the service map, because the browser refused to load
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
in /dist/index.html
due to the default Content Security Policy.
What I’ve changed in content-security-policy-settings
is adding 'self'
in script-src
. Everything else is the default.Great to hear @U051S5XR3!