Using Flask to build a Restful API Server with Swagger document.
Integration with Flask-restplus, Flask-Cors, Flask-Testing, Flask-SQLalchemy,and Flask-OAuth extensions.
-
Restful: Flask-restplus
-
SQL ORM: Flask-SQLalchemy
-
Testing: Flask-Testing
-
OAuth: Flask-OAuth
-
ESDAO: elasticsearch , elasticsearch-dsl
Install with pip:
$ pip install -r requirements.txt
.
|──────app/
| |────__init__.py
| |────api/
| | |────__init__.py
| | |────cve/
| | |────user/
| | |────oauth/
| |──────config.Development.cfg
| |──────config.Production.cfg
| |──────config.Testing.cfg
| |────dao/
| |────model/
| |────oauth/
| |────util/
|──────run.py
|──────tests/
app = Flask(__name__)
app.config['DEBUG'] = True
app = Flask(__name__ )
app.config.from_pyfile('config.Development.cfg')
##Flask settings
DEBUG = True # True/False
TESTING = False
##SWAGGER settings
SWAGGER_DOC_URL = '/api'
....
SERVER_NAME: the name and port number of the server.
JSON_SORT_KEYS : By default Flask will serialize JSON objects in a way that the keys are ordered.
add your client_id
and client_secret
into config file.
add your ES host
and ES port
into config file
$ python webapp/run.py
In flask, Default port is 5000
Swagger document page: http://127.0.0.1:5000/api
** Run with gunicorn **
In webapp/
$ gunicorn -w 4 -b 127.0.0.1:5000 run:app
- -w : number of worker
- -b : Socket to bind
$ docker build -t flask-example .
$ docker run -p 5000:5000 --name flask-example flask-example
In image building, the webapp folder will also add into the image
$ nosetests webapp/ --with-cov --cover-html --cover-package=app
- --with-cov : test with coverage
- --cover-html: coverage report in html format
Offical Website
Tutorial
- Version 2.3 : add dockerfile
- Version 2.2 : add ESDAO module
- Version 2.1 : add OAuth extension: FLASK-OAuth, and google oauth example
- Version 2.0 : add SQL ORM extension: FLASK-SQLAlchemy
- Version 1.1 : update nosetest
- Version 1.0 : basic flask-example with Flask-Restplus, Flask-Tesintg