|
| 1 | +package zio.http.endpoint.openapi |
| 2 | + |
| 3 | +import java.net.URLEncoder |
| 4 | + |
| 5 | +import zio.http._ |
| 6 | +import zio.http.codec.PathCodec |
| 7 | + |
| 8 | +object SwaggerUI { |
| 9 | + |
| 10 | + val DefaultSwaggerUIVersion: String = "5.10.3" |
| 11 | + |
| 12 | + //format: off |
| 13 | + /** |
| 14 | + * Creates routes for serving the Swagger UI at the given path. |
| 15 | + * |
| 16 | + * Example: |
| 17 | + * {{{ |
| 18 | + * val routes: Routes[Any, Response] = ??? |
| 19 | + * val openAPIv1: OpenAPI = ??? |
| 20 | + * val openAPIv2: OpenAPI = ??? |
| 21 | + * val swaggerUIRoutes = SwaggerUI.routes("docs" / "openapi", openAPIv1, openAPIv2) |
| 22 | + * val routesWithSwagger = routes ++ swaggerUIRoutes |
| 23 | + * }}} |
| 24 | + * |
| 25 | + * With this middleware in place, a request to `https://www.domain.com/[path]` |
| 26 | + * would serve the Swagger UI. The different OpenAPI specifications are served |
| 27 | + * at `https://www.domain.com/[path]/[title].json`. Where `title` is the title |
| 28 | + * of the OpenAPI specification and is url encoded. |
| 29 | + */ |
| 30 | + //format: on |
| 31 | + def routes(path: PathCodec[Unit], api: OpenAPI, apis: OpenAPI*): Routes[Any, Response] = { |
| 32 | + routes(path, DefaultSwaggerUIVersion, api, apis: _*) |
| 33 | + } |
| 34 | + |
| 35 | + //format: off |
| 36 | + /** |
| 37 | + * Creates a middleware for serving the Swagger UI at the given path and with |
| 38 | + * the given swagger ui version. |
| 39 | + * |
| 40 | + * Example: |
| 41 | + * {{{ |
| 42 | + * val routes: Routes[Any, Response] = ??? |
| 43 | + * val openAPIv1: OpenAPI = ??? |
| 44 | + * val openAPIv2: OpenAPI = ??? |
| 45 | + * val swaggerUIRoutes = SwaggerUI.routes("docs" / "openapi", openAPIv1, openAPIv2) |
| 46 | + * val routesWithSwagger = routes ++ swaggerUIRoutes |
| 47 | + * }}} |
| 48 | + * |
| 49 | + * With this middleware in place, a request to `https://www.domain.com/[path]` |
| 50 | + * would serve the Swagger UI. The different OpenAPI specifications are served |
| 51 | + * at `https://www.domain.com/[path]/[title].json`. Where `title` is the title |
| 52 | + * of the OpenAPI specification and is url encoded. |
| 53 | + */ |
| 54 | + //format: on |
| 55 | + def routes(path: PathCodec[Unit], version: String, api: OpenAPI, apis: OpenAPI*): Routes[Any, Response] = { |
| 56 | + import zio.http.template._ |
| 57 | + val basePath = Method.GET / path |
| 58 | + val jsonRoutes = (api +: apis).map { api => |
| 59 | + basePath / s"${URLEncoder.encode(api.info.title, Charsets.Utf8.name())}.json" -> handler { (_: Request) => |
| 60 | + Response.json(api.toJson) |
| 61 | + } |
| 62 | + } |
| 63 | + val jsonPaths = jsonRoutes.map(_.routePattern.pathCodec.render) |
| 64 | + val jsonTitles = (api +: apis).map(_.info.title) |
| 65 | + val jsonUrls = jsonTitles.zip(jsonPaths).map { case (title, path) => s"""{url: "$path", name: "$title"}""" } |
| 66 | + val uiRoute = basePath -> handler { (_: Request) => |
| 67 | + Response.html( |
| 68 | + html( |
| 69 | + head( |
| 70 | + meta(charsetAttr := "utf-8"), |
| 71 | + meta(nameAttr := "viewport", contentAttr := "width=device-width, initial-scale=1"), |
| 72 | + meta(nameAttr := "description", contentAttr := "SwaggerUI"), |
| 73 | + title("SwaggerUI"), |
| 74 | + link(relAttr := "stylesheet", href := s"https://unpkg.com/swagger-ui-dist@$version/swagger-ui.css"), |
| 75 | + link( |
| 76 | + relAttr := "icon", |
| 77 | + typeAttr := "image/png", |
| 78 | + href := s"https://unpkg.com/swagger-ui-dist@$version/favicon-32x32.png", |
| 79 | + ), |
| 80 | + ), |
| 81 | + body( |
| 82 | + div(id := "swagger-ui"), |
| 83 | + script(srcAttr := s"https://unpkg.com/swagger-ui-dist@$version/swagger-ui-bundle.js"), |
| 84 | + script(srcAttr := s"https://unpkg.com/swagger-ui-dist@$version/swagger-ui-standalone-preset.js"), |
| 85 | + Dom.raw(s"""<script> |
| 86 | + |window.onload = () => { |
| 87 | + | window.ui = SwaggerUIBundle({ |
| 88 | + | urls: ${jsonUrls.mkString("[\n", ",\n", "\n]")}, |
| 89 | + | dom_id: '#swagger-ui', |
| 90 | + | presets: [ |
| 91 | + | SwaggerUIBundle.presets.apis, |
| 92 | + | SwaggerUIStandalonePreset |
| 93 | + | ], |
| 94 | + | layout: "StandaloneLayout", |
| 95 | + | }); |
| 96 | + |}; |
| 97 | + |</script>""".stripMargin), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ) |
| 101 | + } |
| 102 | + Routes.fromIterable(jsonRoutes) :+ uiRoute |
| 103 | + } |
| 104 | +} |
0 commit comments