|
1 | 1 | # lab-flask-restplus-swagger
|
2 | 2 |
|
3 |
| -[](https://travis-ci.org/nyu-devops/lab-flask-restplus-swagger) |
4 |
| -[]() |
| 3 | +[](https://github.com/nyu-devops/lab-flask-restplus-swagger/actions) |
| 4 | +[](https://codecov.io/gh/nyu-devops/lab-flask-restplus-swagger) |
| 5 | +[](https://opensource.org/licenses/Apache-2.0) |
| 6 | +[](https://www.python.org/) |
| 7 | +[](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/nyu-devops/lab-flask-restplus-swagger) |
5 | 8 |
|
6 | 9 | **NYU DevOps** lab that demonstrates how to use [Flask-RESTX](https://flask-restx.readthedocs.io/en/latest/) to generate [Swagger](http://swagger.io)/[OpenAPI](https://www.openapis.org) documentation for your [Python](https://www.python.org) [Flask](http://flask.pocoo.org) application. This repository is part of lab for the NYU DevOps class [CSCI-GA.2820-001](https://cs.nyu.edu/courses/spring21/CSCI-GA.2820-001/), Graduate Division, Computer Science
|
7 | 10 |
|
8 | 11 | ## Introduction
|
9 | 12 |
|
10 | 13 | When developing microservices with API's that others are going to call, it is critically important to provide the proper API documentation. OpenAPI has become a standard for documenting APIs and Swagger is an implementation of this. This lab shows you how to use a Flask plug-in called **Flask-RESTX** which is a fork of the original **Flask-RESTPlus** that is no longer maintained, to imbed Swagger documentation into your Python Flask microservice so that the Swagger docs are generated for you.
|
11 | 14 |
|
12 |
| -I feel that it is much better to include the documentation with the code because programmers are more likely to update the docs if it's right there above the code they are working on. (...at least that's the therory and I'm sticking to it) ;-) |
| 15 | +I feel that it is much better to include the documentation with the code because programmers are more likely to update the docs if it's right there above the code they are working on. (...at least that's the theory and I'm sticking to it) ;-) |
13 | 16 |
|
14 |
| -## Prerequisite Installation using Vagrant |
| 17 | +### Prerequisite Installation |
15 | 18 |
|
16 |
| -The easiest way to use this lab is with Vagrant and VirtualBox. If you don't have this software the first step is down download and install it. If you have an 2020 Apple Mac with the M1 chip, you should download Docker Desktop instead of VirtualBox. Here is what you need: |
| 19 | +All of our development is done in Docker containers using **Visual Studio Code**. This project contains a `.devcontainer` folder that will set up a Docker environment in VSCode for you. You will need the following: |
17 | 20 |
|
18 |
| -Download: [Vagrant](https://www.vagrantup.com/) |
| 21 | +- Docker Desktop for [Mac](https://docs.docker.com/docker-for-mac/install/) or [Windows](https://docs.docker.com/docker-for-windows/install/) |
| 22 | +- Microsoft Visual Studio Code ([VSCode](https://code.visualstudio.com/download)) |
| 23 | +- [Remote Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) VSCode Extension |
19 | 24 |
|
20 |
| -Intel Download: [VirtualBox](https://www.virtualbox.org/) |
| 25 | +It is a good idea to add VSCode to your path so that you can invoke it from the command line. To do this, open VSCode and type `Shift+Command+P` on Mac or `Shift+Ctrl+P` on Windows to open the command pallet and then search for "shell" and select the option **Shell Command: Install 'code' command in Path**. This will install VSCode in your path. |
21 | 26 |
|
22 |
| -Apple M1 Download: [Apple M1 Tech Preview](https://docs.docker.com/docker-for-mac/apple-m1/) |
23 |
| - |
24 |
| -Install each of those. Then all you have to do is clone this repo and invoke vagrant: |
25 |
| - |
26 |
| -### Using Vagrant and VirtualBox (Intel only) |
27 |
| - |
28 |
| -If you have an Intel Mac or Windows PC you can use these steps: |
| 27 | +Then you can start your development environment up with: |
29 | 28 |
|
30 | 29 | ```bash
|
31 |
| -$ git clone https://github.com/nyu-devops/lab-flask-restplus-swagger.git |
32 |
| -$ cd lab-flask-restplus-swagger |
33 |
| -$ vagrant up |
34 |
| -$ vagrant ssh |
35 |
| -$ cd /vagrant |
| 30 | + git clone https://github.com/nyu-devops/lab-flask-restplus-swagger.git |
| 31 | + cd lab-flask-restplus-swagger |
| 32 | + code . |
36 | 33 | ```
|
37 | 34 |
|
38 |
| -### Using Vagrant and Docker Desktop |
39 |
| - |
40 |
| -If you have an Apple M1 Silicon Mac or other ARM |
41 |
| -based computer, or if you just want to use Docker as a provider instead of VirtualBox, you can use these steps: |
42 |
| - |
43 |
| -This is useful for owners of Apple M1 Silicon Macs which cannot run VirtualBox because they have a CPU based on ARM architecture instead of Intel. |
| 35 | +The first time it will build the Docker image but after that it will just create a container and place you inside of it in your `/app` folder which actually contains the repo shared from your computer. It will also install all of the VSCode extensions needed for Python development. |
44 | 36 |
|
45 |
| -Just add `--provider docker` to the `vagrant up` command like this: |
46 |
| - |
47 |
| -```bash |
48 |
| -$ git clone https://github.com/nyu-devops/lab-flask-restplus-swagger.git |
49 |
| -$ cd lab-flask-restplus-swagger |
50 |
| -$ vagrant up --provider docker |
51 |
| -$ vagrant ssh |
52 |
| -$ cd /vagrant |
53 |
| -``` |
54 |
| - |
55 |
| -This will use a Docker container instead of a Virtual Machine (VM). Everything else should be the same. |
| 37 | +If it does not automatically prompt you to open the project in a container, you can select the green icon at the bottom left of your VSCode UI and select: **Remote Containers: Reopen in Container**. |
56 | 38 |
|
57 | 39 | ## Running the code
|
58 | 40 |
|
59 |
| -You can now run `green` to run the tests and make sure that everything works as expected. |
| 41 | +You can now run `pytest` to run the tests and make sure that everything works as expected. |
60 | 42 |
|
61 | 43 | ```bash
|
62 |
| -$ green |
| 44 | +make test |
63 | 45 | ```
|
64 | 46 |
|
65 | 47 | You can then run the server with:
|
66 | 48 |
|
67 | 49 | ```bash
|
68 |
| -$ honcho start |
| 50 | +honcho start |
69 | 51 | ```
|
70 | 52 |
|
71 | 53 | Finally you can see the microservice Swagger docs at: [http://localhost:8080/](http://localhost:8080/)
|
72 | 54 |
|
73 |
| -When you are done, you can exit and shut down the VM with: |
74 |
| - |
75 |
| -```bash |
76 |
| -$ exit |
77 |
| -$ vagrant halt |
78 |
| -``` |
79 |
| - |
80 |
| -If the VM is no longer needed you can remove it with: |
81 |
| - |
82 |
| -```bash |
83 |
| -$ vagrant destroy |
84 |
| -``` |
85 |
| - |
86 | 55 | ## What's featured in the project?
|
87 | 56 |
|
88 | 57 | * service/routes.py -- the main Service using Python Flask-RESTX for Swagger
|
89 | 58 | * service/models.py -- a Pet model that uses Cloudant for persistence
|
90 | 59 | * tests/test_routes.py -- test cases using unittest for the microservice
|
91 | 60 | * tests/test_models.py -- test cases using unittest for the Pet model
|
92 | 61 |
|
93 |
| -This repository is part of the NYU graduate class **CSCI-GA.2810-001: DevOps and Agile Methodologies** taught by [John Rofrano](http://www.johnrofrano.com/), Adjunct Instructor, NYU Curant Institute, Graduate Division, Computer Science and at Stern Business School. |
| 62 | +## License |
| 63 | + |
| 64 | +Copyright (c) 2021, 2025 John Rofrano. All rights reserved. |
| 65 | + |
| 66 | +Licensed under the Apache License. See [LICENSE](LICENSE) |
| 67 | + |
| 68 | +This repository is part of the NYU graduate class **CSCI-GA.2810-001: DevOps and Agile Methodologies** taught by [John Rofrano](http://www.johnrofrano.com/), Adjunct Instructor, NYU Courant Institute, Graduate Division, Computer Science and at Stern Business School. |
0 commit comments