When Swagger-ui is installed, the pet shop file will be automatically loaded as shown in the image below.
** 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.
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
This time I create a directory **/home/ec2-user/www ** on ec2.
mkdir.ec2
sudo mkdir /home/ec2-user/www
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
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
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.
sudo docker ps
sudo docker stop (Container name)
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.
SWAGGER_JSON=/tmp/pet.yaml
By the way, **/home/ec2-user/www ** and **/tmp ** can specify the directory you like.
** 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