forked from metosin/reitit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
125 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(defproject metosin/reitit-swagger-ui "0.1.1-SNAPSHOT" | ||
:description "Reitit: Swagger-ui support" | ||
:url "https://github.com/metosin/reitit" | ||
:license {:name "Eclipse Public License" | ||
:url "http://www.eclipse.org/legal/epl-v10.html"} | ||
:plugins [[lein-parent "0.3.2"]] | ||
:parent-project {:path "../../project.clj" | ||
:inherit [:deploy-repositories :managed-dependencies]} | ||
:dependencies [[metosin/reitit-ring] | ||
[metosin/jsonista] | ||
[metosin/ring-swagger-ui]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
(ns reitit.swagger-ui | ||
(:require [clojure.string :as str] | ||
[reitit.ring :as ring] | ||
#?@(:clj [ | ||
[jsonista.core :as j]]))) | ||
|
||
#?(:clj | ||
(defn create-swagger-ui-handler | ||
"Creates a ring handler which can be used to serve swagger-ui. | ||
| key | description | | ||
| -----------------|-------------| | ||
| :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:` | ||
| :root | optional resource root, defaults to `\"swagger-ui\"` | ||
| :url | path to swagger endpoint, defaults to `/swagger.json` | ||
| :path | optional path to mount the handler to. Works only if mounted outside of a router. | ||
| :config | parameters passed to swaggger-ui, keys transformed into camelCase. | ||
See https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md | ||
for all available :config options | ||
Examples: | ||
;; with defaults | ||
(create-swagger-ui-handler) | ||
;; with path and url set, swagger validator disabled | ||
(swagger-ui/create-swagger-ui-handler | ||
{:path \"\" | ||
:url \"/api/swagger.json\" | ||
:config {:validator-url nil})" | ||
([] | ||
(create-swagger-ui-handler nil)) | ||
([options] | ||
(let [mixed-case (fn [k] | ||
(let [[f & rest] (str/split (name k) #"-")] | ||
(apply str (str/lower-case f) (map str/capitalize rest)))) | ||
mixed-case-key (fn [[k v]] [(mixed-case k) v]) | ||
config-json (fn [{:keys [url config]}] (j/write-value-as-string (merge config {:url url}))) | ||
conf-js (fn [opts] (str "window.API_CONF = " (config-json opts) ";")) | ||
options (as-> options $ | ||
(update $ :root (fnil identity "swagger-ui")) | ||
(update $ :url (fnil identity "/swagger.json")) | ||
(update $ :config #(->> % (map mixed-case-key) (into {}))) | ||
(assoc $ :paths {"conf.js" {:headers {"Content-Type" "application/javascript"} | ||
:status 200 | ||
:body (conf-js $)} | ||
"config.json" {:headers {"Content-Type" "application/json"} | ||
:status 200 | ||
:body (config-json $)}}))] | ||
(ring/routes | ||
(ring/create-resource-handler options)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters