Coder Social home page Coder Social logo

python-flask's Introduction

OpenAPI generated server

Overview

This server was generated by the OpenAPI Generator project. By using the OpenAPI-Spec from a remote server, you can easily generate a server stub. This is an example of building a OpenAPI-enabled Flask server.

This example uses the Connexion library on top of Flask.

Requirements

Python 3.5.2+

Steps to create

Create directory, to save your openapi generated code.

mkdir python-flask
cd python-flask

Create templates directory to write your controller implementation. This is the clean way to glue generated flask app code (Swagger-Codegen) to with your backend controller implementation. Download and copy templates/controller.mustache to your project directory.

If you want to write your own mustache for controller you can refer to https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python-flask/controller.mustache and make changes as per your requirements.

mkdir templates

Download and copy templates/controller.mustache to python-flask/templates/

cp ~/Downloads/controller.mustache ./templates

Now you are ready to use openapi-generator. Copy the location of your yaml to generate code, in my case its ../openapi.yaml and exceute following command :

openapi-generator generate -i ../openapi.yaml -g python-flask -o . -t ./templates

If all is well, the directory structure must be seen as in the following:

├── Dockerfile
├── git_push.sh
├── openapi_server
│   ├── controllers
│   │   ├── default_controller.py
│   │   ├── __init__.py
│   │   └── security_controller_.py
│   ├── encoder.py
│   ├── __init__.py
│   ├── __main__.py
│   ├── models
│   │   ├── base_model_.py
│   │   ├── employee.py
│   │   └── __init__.py
│   ├── openapi
│   │   └── openapi.yaml
│   ├── test
│   │   ├── __init__.py
│   │   └── test_default_controller.py
│   ├── typing_utils.py
│   └── util.py
├── README.md
├── requirements.txt
├── setup.py
├── templates
│   └── controller.mustache
├── test-requirements.txt
└── tox.ini

Now its a good time to implement backend controllers. All the openapi_server implementation will be stored in src/openapi_server_impl directory. We will write only controller_impl for now. So lets create src/openapi_server_impl/controller_impl directory.

mkdir src
mkdir src/openapi_server_impl
mkdir src/openapi_server_impl/controller_impl

Copy the controller you want to implement to controller_impl using naming convention mentioned in your controller.mustache. In my case it as follows:

cp openapi_server/controllers/default_controller.py src/openapi_server_impl/controller_impl/DefaultController_impl.py

Modify the DefaultController_impl.py and create a symbolic link to import openapi_server_impl as a python module.

cd openapi_server/
ln -s ../src/openapi_server_impl/

You are all set to run the server and see some magic !!!

Usage

To run the server, please execute the following from the root directory:

pip3 install -r requirements.txt
python3 -m openapi_server

and open your browser to here:

http://localhost:8080/ui/

Your OpenAPI definition lives here:

http://localhost:8080/openapi.json

To launch the integration tests, use tox:

sudo pip install tox
tox

Running with Docker

To run the server on a Docker container, please execute the following from the root directory:

# building the image
docker build -t openapi_server .

# starting up a container
docker run -p 8080:8080 openapi_server

Deploy code to azure app service

Steps to commit code

As openapi_server/openapi_server_impl with symbolic link, before commiting code delete openapi_server/openapi_server_impl and copy src/openapi_server_impl to openapi_server/ manually.

cp -r src/openapi_server_impl openapi_server/

You can add src/ to .gitignore to ignore commiting it, as same code is available in openapi_server/openapi_server_impl. For first commit :

git add .
git commit
git push master origin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.