You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

installation.md 3.0 KiB

7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Installation
  2. ## Distribution channels
  3. ### NPM Registry
  4. We publish two modules to npm: **`swagger-ui`** and **`swagger-ui-dist`**.
  5. **`swagger-ui`** is meant for consumption by JavaScript web projects that include module bundlers, such as Webpack, Browserify, and Rollup. Its main file exports Swagger-UI's main function, and the module also includes a namespaced stylesheet at `swagger-ui/dist/swagger-ui.css`. Here's an example:
  6. ```javascript
  7. import SwaggerUI from 'swagger-ui'
  8. // or use require, if you prefer
  9. const SwaggerUI = require('swagger-ui')
  10. SwaggerUI({
  11. dom_id: '#myDomId'
  12. })
  13. ```
  14. In contrast, **`swagger-ui-dist`** is meant for server-side projects that need assets to serve to clients. The module, when imported, includes an `absolutePath` helper function that returns the absolute filesystem path to where the `swagger-ui-dist` module is installed.
  15. _Note: we suggest using `swagger-ui` when your tooling makes it possible, as `swagger-ui-dist`
  16. will result in more code going across the wire._
  17. The module's contents mirrors the `dist` folder you see in the Git repository. The most useful file is `swagger-ui-bundle.js`, which is a build of Swagger-UI that includes all the code it needs to run in one file. The folder also has an `index.html` asset, to make it easy to serve Swagger-UI like so:
  18. ```javascript
  19. const express = require('express')
  20. const pathToSwaggerUi = require('swagger-ui-dist').absolutePath()
  21. const app = express()
  22. app.use(express.static(pathToSwaggerUi))
  23. app.listen(3000)
  24. ```
  25. The module also exports `SwaggerUIBundle` and `SwaggerUIStandalonePreset`, so
  26. if you're in a JavaScript project that can't handle a traditional npm module,
  27. you could do something like this:
  28. ```js
  29. var SwaggerUIBundle = require('swagger-ui-dist').SwaggerUIBundle
  30. const ui = SwaggerUIBundle({
  31. url: "https://petstore.swagger.io/v2/swagger.json",
  32. dom_id: '#swagger-ui',
  33. presets: [
  34. SwaggerUIBundle.presets.apis,
  35. SwaggerUIBundle.SwaggerUIStandalonePreset
  36. ],
  37. layout: "StandaloneLayout"
  38. })
  39. ```
  40. `SwaggerUIBundle` is equivalent to `SwaggerUI`.
  41. ### Docker Hub
  42. You can pull a pre-built docker image of the swagger-ui directly from Dockerhub:
  43. ```
  44. docker pull swaggerapi/swagger-ui
  45. docker run -p 80:8080 swaggerapi/swagger-ui
  46. ```
  47. Will start nginx with swagger-ui on port 80.
  48. Or you can provide your own swagger.json on your host
  49. ```
  50. docker run -p 80:8080 -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui
  51. ```
  52. The base URL of the web application can be changed by specifying the `BASE_URL` environment variable:
  53. ```
  54. docker run -p 80:8080 -e BASE_URL=/swagger -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui
  55. ```
  56. This will serve Swagger UI at `/swagger` instead of `/`.
  57. ### unpkg
  58. You can embed Swagger-UI's code directly in your HTML by using unpkg's interface:
  59. ```html
  60. <script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
  61. <!-- `SwaggerUIBundle` is now available on the page -->
  62. ```
  63. See [unpkg's main page](https://unpkg.com/) for more information on how to use unpkg.