How to run a mock server on Swagger-ui using stoplight/prism (using AWS/EC2/Docker)

Prerequisites

  1. AWS/EC2 instance has been created ・ SSH connection is possible
  2. nginx installed on EC2
  3. Swagger-ui is available (using Docker)
  4. Loaded any yaml file into swagger-ui

Those who have not yet 1 -[Create AWS/EC2 instance] 1 Those who have not yet 2 ・ [Nginx settings] 2 Those who have not yet 3 ・ [Swagger-ui settings] 3 Those who have not yet 4 -[Settings to make swagger-ui read arbitrary files] 4

Please refer to the above and meet all the prerequisites first.

1. SSH to your EC2 instance

sshLogin.ec2


ssh -i keypair_hogehoge.pem ec2-user@******

image.png

If you see an image like the one above, then ** SSH connection is successful **.

2. Pull docker image

Pull the docker image of [stoplight/prism] 5 after SSH connection

prismPull.docker


sudo docker pull stoplight/prism:3

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

If it looks like the above image, the pull is successful.

3. Set up mock server routing in nginx.conf

Open nginx.conf

sudo vim /etc/nginx/nginx.conf

Add to nginx.conf

server {
        listen       80;
        listen       [::]:80;
        server_name  _;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

     //swagger-editor settings
        location /swagger-editor/docker/ {
          proxy_pass http://localhost:8000/;
          proxy_redirect off;
        }
     
     //swagger-ui settings
        location /swagger-ui/docker/ {
          proxy_pass http://localhost:8001/;
          proxy_redirect off;
        }

     //Mock server settings(This time set this)
        location /swagger-mock/docker/ {
          proxy_pass http://localhost:4010/;
          proxy_redirect off;
        }

4. Add the mock server to the yaml file

This time, we will use the yaml file set in [here] 4. Then add a mock server under ** servers **.

servers:
  - url: http://petstore.swagger.io/
  //Settings for mock server
  - url: http://IP address of the EC2 instance/swagger-mock/docker/

Please change the part of ** IP address of EC2 instance ** to an appropriate value.

5. Start the mock server

You must specify ** any yaml file ** when starting the mock server.

This setting Container name: prism Directory where yaml files are stored:/home/ec2-user/www yaml file: pet.yaml

prism.docker


sudo docker run --name prism -it -p 4010:4010 -v /home/ec2-user/www:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/pet.yaml

Container name specification (optional)

containerName.docker


--name hogehoge

yaml file specification (required)

yaml.docker


mock -h 0.0.0.0 /tmp/pokemon.yaml

If it is displayed in the terminal as shown below, it is OK.

[ec2-user@ip-00-0-0-00 ~]$ sudo docker run --name prism -it -p 4010:4010 -v /home/ec2-user/www:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/pet.yaml
[3:37:33 AM] › [CLI] …  awaiting  Starting Prism…
[3:37:34 AM] › [CLI] ℹ  info      GET        http://0.0.0.0:4010/pets?limit=60
[3:37:34 AM] › [CLI] ▶  start     Prism is listening on http://0.0.0.0:4010

6. Access swagger-ui and try the mock server

6a. Select a mock server

Go to swagger-ui and select the right server

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

6b. Open the URL tab and press the [Try it out] button

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

6c. Press the [execute] button

Screenshot 2020-12-19 14.43.02.png

6d. Check the result

If the success response set in the yaml file is displayed as a result as shown below, it is OK

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

You should also see something like the following in your terminal.

[5:55:41 AM] › [HTTP SERVER] get /pets ℹ  info      Request received
[5:55:41 AM] ›     [NEGOTIATOR] ℹ  info      Request contains an accept header: application/json
[5:55:41 AM] ›     [VALIDATOR] ✔  success   The request passed the validation rules. Looking for the best response
[5:55:41 AM] ›     [NEGOTIATOR] ✔  success   Found a compatible content for application/json
[5:55:41 AM] ›     [NEGOTIATOR] ✔  success   Responding with the requested status code 200

Other settings

How to run a mock server in the background

If you don't need to check the result of [Try it out] → [Excute] on the terminal, you can run the mock server in the background.

Foreground ver

sudo docker run --name prism -it -p 4010:4010 -v /home/ec2-user/www:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/pokemon.yaml

Background ver

sudo docker run --name prism -it -d -p 4010:4010 -v /home/ec2-user/www:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/pet.yaml
-d

With this option, each container can be started in the background.

Error handling

docker: Error response from daemon: Conflict. The container name "/prism" is already in use by container "768393267a3839c7db21028daeb8f5a39b67766f994615de3e068ea7f819c500". You have to remove (or rename) that container to be able to reuse that name.

If you get the above error after entering the ** docker run ** command, it is because the container name is covered. Enter the command below to delete the container.

sudo docker rm arbitrary container name

Don't forget to stop the container before deleting it.

sudo docker stop Arbitrary container name

Finally

The above is how to build a mock server using [stoplight/prism] 5.

If you have any questions or questions, please leave them in the comments section.

Recommended Posts

How to run a mock server on Swagger-ui using stoplight/prism (using AWS/EC2/Docker)
How to run React and Rails on the same server
How to run JavaFX on Docker
How to create a web server on an EC2 instance on AWS
How to execute a contract using web3j
How to sort a List using Comparator
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to execute and mock methods using JUnit
How to deploy a container on AWS Lambda
[Rails] How to create a graph using lazy_high_charts
How to delete a controller etc. using a command
[Ethereum] How to execute a contract using web3j-Part 2-
How to run a djUnit task in Ant
How to generate a primary key using @GeneratedValue
How to build a Pytorch environment on Ubuntu
I tried to implement a server using Netty
How to run NullpoMino 7.5.0 on Ubuntu 20.04.1 64bit version
How to run the SpringBoot app as a service
How to run javafx with Raspberry Pi Posted on 2020/07/12
How to make JavaScript work on a specific page
How to delete custom Adapter elements using a custom model
I tried using Log4j2 on a Java EE server
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
Steps to set up a VNC server on CentOS 8.3
[Java] How to execute tasks on a regular basis
How to mock a super method call in PowerMock
How to convert A to a and a to A using AND and OR in Java
How to start tomcat local server without using eclipse
How to run Java EE Tutial on github on Eclipse
[Rails] How to install a decorator using gem draper
How to create an application server on an EC2 instance on AWS
How to deploy a simple Java Servlet app on Heroku
How to get inside a container running on AWS Fargate
How to run npm install on all projects in Lerna
Try launching a webAP server on the micro using Helidon
How to deploy a kotlin (java) app on AWS fargate
How to display a graph in Ruby on Rails (LazyHighChart)
How to output array values without using a for statement
How to install JDK 8 on Windows without using the installer
How to join a table without using DBFlute and sql
How to install GNOME as a desktop environment on CentOS 7
How to get started with JDBC using PostgresSQL on MacOS
How to deploy a Rails application on AWS (article summary)
How to register as a customer with Square using Tomcat
How to deploy on heroku
How to leave a comment
How to terminate rails server
How to insert a video
How to create a method
How to authorize using graphql-ruby
[Rails 6] How to create a dynamic form input screen using cocoon
How to automatically operate a screen created in Java on Windows
Easily set up a Jenkins server on AWS using Bitnami's AMI
How to run a job with docker login in AWS batch
How to run a Kotlin Coroutine sample from the command line
[Rails] How to put a crown mark on the ranking function
How to place and share SwiftLint config files on the server
How to make a groundbreaking diamond using Java for statement wwww