wroc.py #32: microservices in flask
TRANSCRIPT
Aplikacja monolityczna
5
from app.users.models import UserSiteProfilefrom app.utils.cache import cache_key_userfrom app.sites.models import SiteProfilefrom app.sites.utils import site_termsfrom app.utils.users import get_homepage_urlfrom app.utils.views import ThemedFormView, ThemedTemplateViewfrom app.authentication import signalsfrom app.authentication.forms import AuthForm, EmailFormfrom app.authentication.idp import ( is_valid_idp, MultipleIDPError, saml_available, site_idp, user_idp_lookup)from app.authentication.loginchecks import ( check_account_expiration, get_site_login_errors, pre_login_checks)from app.saml.utils import site_specific_config_loaderfrom app.saml.views import _get_subject_id
Flask-Script
8
flask-script.readthedocs.io/en/latest/
from flask.ext.script import Manager, Shell, Serverfrom my_app.application import app
manager = Manager(app)manager.add_command( 'runserver', Server(host= '0.0.0.0', port=7000, use_debugger= True))
Flask-RESTful
9
flask-restful.readthedocs.io/en/0.3.5/
from flask_restful import Resource
class MyResource(Resource): def get(): return {'OK'}
@api.representation('application/xml' )def output_xml(data, code, headers= None): response = make_response(dicttoxml.dicttoxml(data), code) inject_headers(response, headers) return response
Marshallow & flask-marshallow
10
marshmallow.readthedocs.io/en/latest/index.html
ma = Marshmallow(app)
class CustomSchema(ma.Schema): id = ma.Int(dump_only= True) name = ma.Str(required= True) url = ma.Url(relative= True)
@app.route('/api/custom/<id>' )def users(): all_obj = Object.all() result = object_schema.dump(all_obj) return jsonify(result.data)
Flask-SQLAlchemy
11
flask-sqlalchemy.pocoo.org/2.1/
from flask.ext.sqlalchemy import SQLAlchemydb = SQLAlchemy(app)
class MyModel(db.Model): GROUP = 'group' USER = 'user' TYPES = [ (GROUP, GROUP), (USER, USER), ]
__tablename__ = 'my_model' id = db.Column(db.Integer, primary_key= True) model_type = db.Column(ChoiceType(TYPES)) model_value_id = db.Column(db.Integer, db.ForeignKey( 'model_value.id' )) value = db.Column(db.String( 1024))
def __init__(self, model_type, model_value_id, value): self.share_type = share_type self.rule_value_id = rule_value_id self.value = value
Flask-Migrate
12
flask-migrate.readthedocs.io/en/latest/
def upgrade(): op.create_table( 'my_model', sa.Column( 'id', sa.Integer(), nullable= False), sa.Column( 'model_type', sqlalchemy_utils.types.choice.ChoiceType(TYPES), nullable= True ), sa.PrimaryKeyConstraint( 'id'), sa.Column( 'value', sa.String(length= 1024), nullable=True), ) op.create_table( 'my_model_values' , sa.Column( 'id', sa.Integer(), nullable= False), sa.Column( 'model_value_id' , sa.Integer(), nullable= True), sa.Column( 'value', sa.String(length= 1024), nullable=True), sa.ForeignKeyConstraint([ 'model_value_id' ], ['my_model.id']), sa.PrimaryKeyConstraint( 'id') )
15
Cookiecutter cookiecutter.readthedocs.io/en/latest/
.└── flask_microservice ├── AUTHORS.rst ├── debian ├── docs │ ├── make.bat │ ├── Makefile │ └── source │ ├── authors.rst │ ├── conf.py │ ├── contributing.rst │ ├── history.rst │ ├── index.rst │ ├── readme.rst │ ├── technical_details.rst │ └── usage.rst
├── HISTORY.rst├── MANIFEST.in├── README.rst ├── requirements.txt ├── setup.cfg ├── setup.py ├── src │ ├── flask_microservice │ │ ├── application.py │ │ ├── default_config.ini │ │ ├── __init__.py │ │ └── manage.py │ └── tests │ ├── conftest.py │ └── test_flask_microservice.py └── tox.ini
16
Dh- virtualenv
dh-virtualenv.readthedocs.io/en/latest/
override_dh_auto_test:echo "SKIPPING TESTS"
%:dh $@ --with python-virtualenv
override_dh_virtualenv:dh_virtualenv \
--python /usr/bin/python2.7 \ --setuptools \ --preinstall pip==6.1.1 \ --preinstall setuptools==18.7.1 \ --preinstall wheel==0.26.0
override_dh_builddeb:dh_builddeb --destdir= ${DESTINATION_DPKG_DIR }