Change the Swagger-ui read file. (Using AWS/Docker)

Purpose

When Swagger-ui is installed, the pet shop file will be automatically loaded as shown in the image below.

スクリーンショット 2020-12-12 12.26.06.png

** This time, I will write how to make swagger-ui read an arbitrary file. ** ** I use AWS/EC2, but I think I can change it locally in the same way.

Prerequisites

  1. AWS/EC2 created/SSH connection possible
  2. nginx installed
  3. Swagger-ui available (Docker)

If you haven't set 1 yet, click here. ] 1

If you haven't set 2 yet, click here. ] 2

If you haven't set 3 yet, click here. ] 3

1. Create a directory to put arbitrary yaml files.

This time I create a directory **/home/ec2-user/www ** on ec2.

mkdir.ec2


sudo mkdir /home/ec2-user/www 

2. Create the file you want to read

Create your favorite yaml file directly under the directory you created earlier. This time, I will use the yaml file listed in [this article] 4.

pet.yaml


openapi: 3.0.0
info:
  version: 1.0.0
  title:Pet store(After change)
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
          example: 1
        name:
          type: string
          example: "pochi"
        tag:
          type: string
          example: "dog"
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

3. Start swagger-ui

Set swagger-ui to read any file and start it.

swagger-ui.docker


sudo docker run -d -p 8001:8080 -e SWAGGER_JSON=/tmp/pet.yaml -v /home/ec2-user/www:/tmp swaggerapi/swagger-ui

4. Check swagger-ui

Access the ** swagger-ui URL ** specified in ** nginx.conf **

If you haven't set nginx.conf yet, [here] 2

Then, as shown in the image below, the file of the first pet shop should be changed to the yaml file prepared this time.

スクリーンショット 2020-12-14 1.16.30.png

important point

(1) If you have already Docker run swagger-ui, stop it and then start it again.

Search for running containers
sudo docker ps
Container stop
sudo docker stop (Container name)

② How to specify SWAGGER_JSON

Create a volume

Docker has a place called a volume where you can store data. From that volume, you need to specify any yaml file and load it into swagger-ui.

However, the volume cannot be used automatically from the beginning. You will need to prepare your own using the options below.

-v /home/ec2-user/www:/tmp

This ** "-v" ** option signals that ** a volume will be created **. Simply put, the files in the **/home/ec2-user/www ** directory on ec2 It means that you can copy it to a volume called **/tmp ** so that you can use it.

In this case, one ** yaml file (pet.yaml) ** is created directly under **/home/ec2-user/www **, so it is directly under **/tmp (in the volume). One ** same yaml file (pet.yaml) ** is created in ** as well.

Specify the file in the volume in SWAGGER_JSON
SWAGGER_JSON=/tmp/pet.yaml

By the way, **/home/ec2-user/www ** and **/tmp ** can specify the directory you like.

Finally

** The above is how to change the swagger-ui read file using Docker. ** **

** If you have any questions or questions, please feel free to comment. ** **

Recommended Posts

Change the Swagger-ui read file. (Using AWS/Docker)
Read the file line by line VS read at once
Read the file under WEB-INF when executing the Servlet
[Java] Read the file in src / main / resources
Read Java Property file
Change the file name and output destination of the JavaVM error file
How to change the contents of the jar file without decompressing
How to change the file name with Xcode (Refactor Rename)